Files in C
description
Transcript of Files in C
![Page 1: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/1.jpg)
Files in CPrepared by: Careene McCallum-
Rodney
![Page 2: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/2.jpg)
Content to be coveredIntroduction
◦File pointer◦Opening Files◦Closing files
Writing to files◦Using Append◦Using Write
Reading from files
![Page 3: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/3.jpg)
IntroductionIt is important that you create a
folder for your IA, as the source file and text files need to be in close proximity.
![Page 4: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/4.jpg)
Important - ONEEach file must have its own “file
pointer”. ◦You need special variables to hold
the address of the files.◦You declare them when needed◦Declared as:
FILE *studentnumPtr;FILE *studentInfoPtr;
![Page 5: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/5.jpg)
Important - TWOTo have access to a file, you must
open it.◦Three items are needed when
opening a file1. Name of the text File2. Mode you are opening the file in (ie,
append, read or write)3. The pointer variable that will point to
the address of the file.
fopen(“StudentInfo.txt”, “r”);
![Page 6: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/6.jpg)
Important - TWOfopen(“StudentInfo.txt”, “r”);
Used to open the file Name of the fileMode its being opened inie read mode, in this case
The fopen() method will either return :• NULL if file not found OR
• An address if file is found
![Page 7: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/7.jpg)
Important - TWOThe result of fopen() is to be stored
in the pointer variable.
For example:
studentInfoPtr = fopen(“StudentInfo.txt”, “r”);
![Page 8: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/8.jpg)
Important - THREEWhen you are finished using an
opened file, it is important to CLOSE it.◦File is closed using the fclose() method
and file pointer.◦For example:
fclose(studentInfoPtr);
![Page 9: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/9.jpg)
Example
What is the name of the file to be opened?
What mode is file opened in?
What is the name of the file pointer?
studentInfo.txt
append
studentPtr
![Page 10: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/10.jpg)
Content to be coveredIntroduction
◦File pointer◦Opening Files◦Closing files
Writing to files◦Using Append◦Using Write
Reading from files
![Page 11: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/11.jpg)
Writing to fileYou can write to a file using the
APPEND or WRITE mode.
Append mode◦This simply adding to the end of the file
without deleting what was there before
Write mode◦This is erasing what is currently in the
file and then storing new data to file.
![Page 12: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/12.jpg)
Writing to fileWhen writing to file, use the fprintf() method.
If you are writing the name and age of a student to the file, you would do the following:
fprintf (studentPtr, “%s\n%f\n”, sname, sage);
Name of file pointer Writing a string then in a new line writing a float
Writing the name of student and then the age of student
![Page 13: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/13.jpg)
Writing to file If you were using our lower6 structure array, you
would do the following:
fprintf(studentPtr, “%s\n%f\n”, lower6[i].sname, lower6[i].sage);
![Page 14: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/14.jpg)
Example of APPENDing to file
“end” in sname will stop the loop
As soon as data is received, it is appended.
![Page 15: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/15.jpg)
Example of APPENDing to fileUsing the system to add student
info
![Page 16: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/16.jpg)
Example of APPENDing to file
BEFORE AFTER
![Page 17: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/17.jpg)
Example of APPENDING to fileLet’s add another record and see
the effects.
![Page 18: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/18.jpg)
Example of APPENDing to file
BEFORE AFTER
![Page 19: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/19.jpg)
Content to be coveredIntroduction
◦File pointer◦Opening Files◦Closing files
Writing to files◦Using Append◦Using Write
Reading from files
![Page 20: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/20.jpg)
Writing to fileLet’s consider a scenario.
The system will like to keep a track of the number of students in the database, so that the maximum number of space available is NOT exceeded.
![Page 21: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/21.jpg)
Writing to fileThe file that will store this info is:
![Page 22: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/22.jpg)
Writing to fileThe file currently has 4, because
we now have 4 records in the file:
![Page 23: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/23.jpg)
Writing to filewriteNumStud () method will overwrite the
number of students in the file to the new amount.
![Page 24: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/24.jpg)
Writing to fileThis function calls on writeNumStudents() , after
“end” was entered.
The call
sindex increases by one, after a student is added
![Page 25: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/25.jpg)
Let’s add ONE more student
![Page 26: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/26.jpg)
Before
![Page 27: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/27.jpg)
After
![Page 28: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/28.jpg)
Content to be coveredIntroduction
◦File pointer◦Opening Files◦Closing files
Writing to files◦Using Append◦Using Write
Reading from files
![Page 29: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/29.jpg)
Reading from fileConsider the following files and their
content.
![Page 30: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/30.jpg)
Reading from file The aim is to read:
1. number of students into an integer variable
2. student information into a structure array
We will use the number of students read to determine the number of times we should read from the file.
![Page 31: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/31.jpg)
Read number of students from file
![Page 32: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/32.jpg)
Read student information from file
![Page 33: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/33.jpg)
Modify Menu Case Structure
![Page 34: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/34.jpg)
Display will show
![Page 35: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/35.jpg)
Delete method
![Page 36: Files in C](https://reader035.fdocuments.net/reader035/viewer/2022062521/56815e47550346895dccbc2b/html5/thumbnails/36.jpg)
Append a record method