File Directed Input Output Console based Input/Output • Console oriented – use terminal (keyboard/screen) • scanf( scanf(“%d” %d ,&i) – &i) read data from keyboard read data from keyboard • printf( printf(“%d”,i) %d ,i) – print data to monitor print data to monitor • Suitable for small volumes of data • Data are lost when program terminated 1
File Directed Input Output • Some programs expect the same set of data to be fed as input every time it is run. – Cumbersome. – Better if the data are kept in a file, and the program reads f from the file. h fl
• Programs generating large volumes of output. Programs generating large volumes of output – Difficult to view on the screen. – Better to store them in a file for later viewing/ processing g/ p g
2
Real‐life applications • Large data volumes • For example, physical experiments (CERN collider), human genome, population records, etc. • Need for flexible approach to store/retrieve data • These store/retrieve data operations necessitate the concept of files of files
3
Files • Files are places on disks where group of related data is stored. – Examples Examples are a C program, executable files, word files, powerpoint are a C program executable files word files powerpoint files, etc.
• High‐level programming languages support file operations – – – – –
Naming Opening Reading Writing Closing
4
Defining and opening file • To store data file in secondary memory (disk) one must specify to OS: to OS: – Filename (e.g. sort.c, input.data) – Data structure (e.g. FILE) – Purpose (e.g. reading, writing, appending)
5
Filename • Filename consists of any string of characters that make up a valid filename for the OS valid filename for the OS
• Filename might contain two parts: – Primary (given name) – Optional period with extension (to indicate type) O ti l i d ith t i (t i di t t )
• Examples: a.out, prog.c, temp, text.out, cgpa.txt, data.in, data.out, etc. 6
General syntax for operating with files FILE *file_pointer_name; Example: FILE *fp; Example: FILE fp; //*variable variable fp is pointer to type FILE fp is pointer to type FILE*// file_pointer_name = fopen(“filename”, “mode”); Example: fp = fopen (“input.txt”, “r”); /* /*opens file with name input.txt , assigns identifier to fp */ fil i h i i id ifi f */
• fp – contains all information about file – Communication link between system and program
• Mode can be – r open file for reading only; make sure the file exists. – w open file for writing only; make sure any existing file not overwritten. – a open file for appending (adding) data; make sure about the existing output
7
Different modes • Writing mode – if file already exists then contents are deleted, – else new file with specified name created
• Appending mode – if file already exists then file opened with contents safe if file already exists then file opened with contents safe – else new file created
• Reading mode – if file already exists then opened with contents safe – else error occurs. //File operation with programmer //given filenames FILE *fp1, *fp2; fp1 = fopen(“data” fp1 = fopen( data , ”r”); r ); fp2= fopen(“results”, “w”);
//File operations with user given filename FILE *empl ; char filename[25]; printf(“\nEnter printf( \nEnter input Filename: input Filename:”); ); scanf (“%s”, filename); empl = fopen (filename, “r”) ; 8
File Modes The types that may be used with fopen function are: Type "r" “ " “w" "a" "r+" "w+" "a+"
Meaning Open for reading. The file must already exist O Open for writing. It will be created if it does not exist f iti It ill b t d if it d t it Open for append (material to the end of existing file or a new file will be created or a new file will be created Open for both reading and writing. File must exist Open for both reading and writing. If the file exists its p g g contents are overwritten Open for both reading and appending. If the file does not exist it will be created 9
Closing a file • Several Several files may be opened at the same time. files may be opened at the same time • All files must be closed as soon as all operations on them are completed as follows: fclose(file_pointer1); fclose(file_pointer2); ………… • Ensures – Ens Ensures that all file data stored in memory buffers are properly written res that all file data stored in memor b ffers are properl ritten to the file. – All outstanding information associated with file flushed out from buffers – All links to file broken – Accidental misuse of file prevented
• If want to change mode of file, then first close and open again 10
3 Mandatory steps for file operations FILE *p1, *p2; FILE *p1 *p2; //step1: declaring a file pointer //step1: declaring a file pointer p1 = fopen(“INPUT.txt”, “r”); //step2: linking a file pointer p2 =fopen(“OUTPUT.txt”, “w”); …….. …….. fclose(p1); //step3: unlinking a file pointer fclose(p2); (p )
• A pointer can be reused after closing [unlinking] [with the same file or with any other file] 11
Input/Output operations on files C C provides several different functions for reading/writing: id l diff t f ti f di / iti • • • • • •
getc() – read a character getc() read a character putc() – write a character fprintf() – write set of data values with directed formatting fscanf() – read set of data values from directed formatting getw() – read integer putw() – write integer
12
fscanf() and fprintf() • •
similar to scanf() and printf() in addition provide file‐pointer
fscanf ( file_pointer, “control string”, &variables ) ; fprintf ( file_pointer, “control string”, variables/expression/constants ) ;
•
For example, given the following – file‐pointer f1 (points to file opened in write mode) file‐pointer f1 (points to file opened in write mode) – file‐pointer f2 (points to file opened in read mode) – integer variable i – float variable f
fscanf ( f2, “%d %f”, &i, &f ) ; fprintf ( f1, “%d %f\n”, i, f); •
fscanf returns EOF when end‐of‐file reached
13
5 steps to be included for file operations FILE *p1, *p2; FILE *p1 *p2; //step1: declaring file pointers //step1: declaring file pointers p1 = fopen(“INPUT.txt”, “r”); //step2: opening files p2 =fopen(“OUTPUT.txt”, “w”); …….. …….. fscanf ( p1, “%d”, &variable); //step3: getting inputs fprintf (p2, “%d”, variable);
//step4: printing outputs
fclose(p1); fclose(p2);
//step5: closing file pointers
14
Example :: Sorting GPA from an input file p g p #include #define SIZE 10 int main( ) { int i, k=0; float temp, cgpa[SIZE]; FILE *input, *output; input=fopen("cgpa input=fopen( cgpa.txt txt", "r"); r ); output=fopen("merit.txt", "w"); for(i=0;i