150 likes | 269 Views
What is a File. A file is a package of information with a name attached to it. Files are used for various purposes: Data files Contain data such as text or numbers Programs Contain instructions / commands
E N D
What is a File • A file is a package of information with a name attached to it. • Files are used for various purposes: • Data files • Contain data such as text or numbers • Programs • Contain instructions / commands • Conceptually, a file is a sequence of characters, which resides somewhere on a disk.
Data Files • Allow you to get information from the user • Initialize values for variables in a program • Save the state of a program • Output results
Access Files • To access a file • Open • Read / Write • Close Read/write A data file Your c code
File Pointer • A new data-type in C to communicate with files • Defined in stdio.h • Written as FILE * • E.g. a file pointer called output_file is declared in a statement like • FILE *output_file; • File pointer holds disk location of the disk file
Open a File • Must open a file before you access it • FILE *fopen(const char *filename, const char *mode); • Connect a file pointer to a specified file name • The file now can be accessed via this file pointer • File is accessed sequentially • Position information stored in FILE structure • Like playing audio cassette • Used for dumping and retrieving data
Mode • r - open for reading (Must exist) • w - open for writing • file need not exist • file is erased if it does • a - open for appending • file need not exist • adds to end if it does • r+ - open for reading and writing - start at beginning • w+ - open for reading and writing (overwrite file) • a+ - open for reading and writing (append if file exists)
Example FILE *fp; fp=fopen("test.txt", "r"); • If file does not exist, fopen returns a null pointer
Close a File • Use function fclose • int fclose(FILE *a_file); • fclose returns zero if the file is closed successfully. • Example FILE *fp = fopen("test.txt", "r"); // codes for accessing the file fclose(fp); • Don’t use close(fp);
Read from a File – getc • getc: read in a single character from a file • char getc(FILE *fp)
When to stop reading? • If at the end of the file, getc returns EOF – a special value to indicate that the end of file has been reached • Normally -1 is used for EOF • int feof(FILE *fp) test end-of-file indicator • Returns 0 if not end of a file • Non-zero if yes
Write to a File – putc • Writes a character to a file. • int putc(int c, FILE *fp);
Example – I • Write a program to count the number of lines and characters in a file. • What characters need to be considered? • How do we count the number of lines?
Example – II • Write a program to compare two files specified by the user, displaying a message indicating whether the files are identical or different. • Two cases • Current letters being compared are the same • Check the next letters until when? • Current letter being compared are different • We can stop
Example – III • Write a program to write a string to a file. • Just print out the array character by character until when?
Example – IV • Write a file copying program • Procedure Open files (f1 and f2) appropriately while not end of file for f1 Read characters from f1 and write character to f2 Close files