1 / 15

What is a File

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.

fathia
Download Presentation

What is a File

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. Access Files • To access a file • Open • Read / Write • Close Read/write A data file Your c code

  3. 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

  4. 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

  5. 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)

  6. 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

  7. 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();

  8. 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);

  9. 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

  10. 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

  11. 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

  12. 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

  13. Example – I • Write a program to count the number of lines and characters in a file.

  14. Example – II • Write a program to compare two files specified by the user, displaying a message indicating whether the files are identical or different.

  15. 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

More Related