150 likes | 291 Vues
This guide covers the fundamental concepts of file handling in C programming. A file is defined as a package of information with an associated name, serving various purposes such as data recording and executing processing commands. It introduces how to access files through opening, reading, writing, and closing. Key functions like `fopen`, `fclose`, `fscanf`, and `fprintf` are explained with examples for reading and writing data. Practical exercises are also provided, including counting lines in a file and comparing file contents.
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: • 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 • 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 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 data
Mode • 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 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("c:\\test.txt", "r"); // codes for accessing the file fclose(fp); • Do not use close();
Read from a File – fscanf • Read in data at certain format • int fscanf(FILE *fp, const char *format, ... ); • 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 read
Write 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 examples
Write 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 line
More I/O: puts char 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 line
Example – 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 • Procedure Open files (f1 and f2) appropriately Read characters from f1 and while not end of file do write character to f2 read next character from f1 Close files