What is a FileAccess FilesFile PointerOpen a FileModeExampleClose a FileRead from a File – fscanfRead from a File – getc, fgetsWrite to a File – fprintfWrite to a File – putc, and fputsMore I/O: putsExample – IExample – IIExample – IIIWhat is a File•A file is a package of information with a name attached to it. •Files are used for various purposes: –Files can record data, such as text or numbers. –Some files record ways to perform various processing procedures on data. These are referred to as programs or commands. •Conceptually, a file is a sequence of characters, which resides somewhere on a disk.Access Files•To access a file–Open–Read / Write–CloseYour c codeA data fileRead/writeFile 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 fileOpen a File•Must open a file before access it –FILE *fopen(const char *filename, const char *mode);–Connect a file pointer to a specified file•The file now can be accessed via this file pointer, not others•File is accessed sequentially–Position information stored in FILE structure –Like playing audio cassette–Great for dumping and retrieving dataMode•r - open for reading •w - open for writing (file need not exist) •a - open for appending (file need not exist) •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•In the filename, use \\ rather than \ –FILE *fp; –fp=fopen("c:\\test.txt", "r"); •If file does not exist, fopen returns a null pointerClose a File•Use function fclose–int fclose(FILE *a_file); –fclose returns zero if the file is closed successfully.•ExampleFILE *fp = fopen("c:\\test.txt", "r"); // codes for accessing the filefclose(fp);•Do not use close();Read from a File – fscanf•Read in data at certain format–int fscanf(FILE *fp, const char *f ormat, ... ); –If success, return the number of successfully matched and assigned input items. –E.g.•n = fscanf(fp, "%d%f%s", &i, &x, name);Read from a File – getc, fgets•gets: read in a single character from a file–char getc(FILE *fp)–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•fgets: read in a string with a specific length from a file–fgets(char *string, int n, FILE * stream);–Reading stops when•A newline character•n-1 characters have been readWrite to a File – fprintf•One common way: use function fprintf–int fprintf (FILE * stream , const char * format [ , argument , ...] ); –Similar to printf–E.g•fprintf(fp, "All numbers are there\n“);•More examplesWrite to a File – putc, and fputs•Writes a character to a file. –int putc(int c, FILE *fp); •Writes a string–int fputs(const char *s, FILE *fp); –E.g.•fputs(“good”, fp);–no conversion specifications –always ends with new lineMore I/O: putschar str[] = “Clock strikes 12”;printf(“The message: %s\n”, str);•we can put the length in the conversion specifications•Another way: puts(str); –no conversion specifications, –always ends with new lineExample – I•Write a program to count the number of lines and characters in a file.Example – II•Write a program to compare two files specified by the user, displaying a message indicating whether the files are identical or different.Example – III•Write a file copy program•ProcedureOpen files (f1 and f2) appropriatelyRead characters from f1 andwhile not end of file dowrite character to f2read next character from f1Close
View Full Document