Files in C

36
Files in C Prepared by: Careene McCallum- Rodney

description

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. - PowerPoint PPT Presentation

Transcript of Files in C

Page 1: Files in C

Files in CPrepared by: Careene McCallum-

Rodney

Page 2: Files in C

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

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

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

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

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

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

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

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

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

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

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

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

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

Example of APPENDing to fileUsing the system to add student

info

Page 16: Files in C

Example of APPENDing to file

BEFORE AFTER

Page 17: Files in C

Example of APPENDING to fileLet’s add another record and see

the effects.

Page 18: Files in C

Example of APPENDing to file

BEFORE AFTER

Page 19: Files in C

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

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

Writing to fileThe file that will store this info is:

Page 22: Files in C

Writing to fileThe file currently has 4, because

we now have 4 records in the file:

Page 23: Files in C

Writing to filewriteNumStud () method will overwrite the

number of students in the file to the new amount.

Page 24: Files in C

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

Let’s add ONE more student

Page 26: Files in C

Before

Page 27: Files in C

After

Page 28: Files in C

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

Reading from fileConsider the following files and their

content.

Page 30: Files in C

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

Read number of students from file

Page 32: Files in C

Read student information from file

Page 33: Files in C

Modify Menu Case Structure

Page 34: Files in C

Display will show

Page 35: Files in C

Delete method

Page 36: Files in C

Append a record method