1 / 36

Files in C

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.

tangia
Download Presentation

Files in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files in C Prepared by: Careene McCallum-Rodney

  2. Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files

  3. 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.

  4. 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;

  5. 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”);

  6. 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

  7. Important - TWO • The result of fopen() is to be stored in the pointer variable. • For example: studentInfoPtr = fopen(“StudentInfo.txt”, “r”);

  8. 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);

  9. 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

  10. Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files

  11. 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.

  12. 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

  13. 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);

  14. Example of APPENDing to file As soon as data is received, it is appended. “end” in sname will stop the loop

  15. Example of APPENDing to file • Using the system to add student info

  16. Example of APPENDing to file BEFORE AFTER

  17. Example of APPENDING to file • Let’s add another record and see the effects.

  18. Example of APPENDing to file AFTER BEFORE

  19. Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files

  20. 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.

  21. Writing to file • The file that will store this info is:

  22. Writing to file • The file currently has 4, because we now have 4 records in the file:

  23. Writing to file • writeNumStud () method will overwrite the number of students in the file to the new amount.

  24. Writing to file This function calls on writeNumStudents() , after “end” was entered. sindex increases by one, after a student is added The call

  25. Let’s add ONE more student

  26. Before

  27. After

  28. Content to be covered • Introduction • File pointer • Opening Files • Closing files • Writing to files • Using Append • Using Write • Reading from files

  29. Reading from file • Consider the following files and their content.

  30. 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.

  31. Read number of students from file

  32. Read student information from file

  33. Modify Menu Case Structure

  34. Display will show

  35. Delete method

  36. Append a record method

More Related