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: Data files Contain data such as text or numbers Programs Contain instructions / commands

mckile
Télécharger la présentation

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: • 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.

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

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

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

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

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

  7. Example FILE *fp; fp=fopen("test.txt", "r"); • If file does not exist, fopen returns a null pointer

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

  9. Read from a File – getc • getc: read in a single character from a file • char getc(FILE *fp)

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

  11. Write to a File – putc • Writes a character to a file. • int putc(int c, FILE *fp);

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

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

  14. Example – III • Write a program to write a string to a file. • Just print out the array character by character until when?

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

More Related