370 likes | 698 Views
Files in C. Prepared by: Careene McCallum-Rodney. Content to be covered. Introduction File pointer Opening Files Closing files Writing to files Using Append Using Write Reading from files. Introduction.
E N D
Files in C Prepared by: Careene McCallum-Rodney
Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files
Introduction • It is important that you create a folder for your IA, as the source file and text files need to be in close proximity.
Important - ONE • Each 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;
Important - TWO • To have access to a file, you must open it. • Three items are needed when opening a file • Name of the text File • Mode you are opening the file in (ie, append, read or write) • The pointer variable that will point to the address of the file. fopen(“StudentInfo.txt”, “r”);
Important - TWO fopen(“StudentInfo.txt”, “r”); Mode its being opened in ieread mode, in this case Used to open the file Name of the file • The fopen() method will either return : • NULL if file not found OR • An address if file is found
Important - TWO • The result of fopen() is to be stored in the pointer variable. • For example: studentInfoPtr = fopen(“StudentInfo.txt”, “r”);
Important - THREE • When 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);
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
Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files
Writing to file • You 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.
Writing to file • When 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
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);
Example of APPENDing to file As soon as data is received, it is appended. “end” in sname will stop the loop
Example of APPENDing to file • Using the system to add student info
Example of APPENDing to file BEFORE AFTER
Example of APPENDING to file • Let’s add another record and see the effects.
Example of APPENDing to file AFTER BEFORE
Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files
Writing to file • Let’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.
Writing to file • The file that will store this info is:
Writing to file • The file currently has 4, because we now have 4 records in the file:
Writing to file • writeNumStud () method will overwrite the number of students in the file to the new amount.
Writing to file This function calls on writeNumStudents() , after “end” was entered. sindex increases by one, after a student is added The call
Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files
Reading from file • Consider the following files and their content.
Reading from file • The aim is to read: • number of students into an integer variable • 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.