1 / 8

Text File Input and Output

Text File Input and Output. Overview. Text File Buffered I/O Functions fopen Function Demo of File Write and Read. Text File Buffered I/O Functions.

lahela
Download Presentation

Text File Input and Output

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. Text File Input and Output

  2. Overview • Text File Buffered I/O Functions • fopen Function • Demo of File Write and Read

  3. Text File Buffered I/O Functions int fclose(FILE *stream);int feof(FILE *stream);int ferror(FILE *stream);int fflush(FILE *stream);int fgetc(FILE *stream);int fgetpos(FILE *stream, fpos_t *pos);char *fgets(char *s, int n, FILE *stream);FILE *fopen(const char *filename, const char *mode);int fprintf(FILE *stream, const char *format, ...);int fputc(int c, FILE *stream);int fputs(const char *s, FILE *stream);size_t fread(void *ptr, size_t size, size_t nelem, FILE *stream);int fscanf(FILE *stream, const char *format, ...);int fseek(FILE *stream, long offset, int mode);int fsetpos(FILE *stream, const fpos_t *pos);long ftell(FILE *stream);size_t fwrite(const void *ptr, size_t size, size_t nelem, FILE *stream); (Prototypes are in stdio.h)

  4. fopen Function FILE *fopen(const char *filename, const char *mode); Opens the file with the filename filename, associates it with a stream, and returns a pointer to the object controlling the stream. If the open fails, it returns a NULL pointer. The initial characters of mode determine how the program manipulates the stream and whether it interprets the stream as text or binary. The characters of mode must be one of the following sequences: • "r" -- to open an existing text file for reading • "w" -- to create a text file or to open and truncate an existing text file, for writing • "rb" -- to open an existing binary file for reading • "wb" -- to create a binary file or to open and truncate an existing binary file, for writing (This is an abridged list)

  5. Demo of File Write and Read #include <stdio.h> #include <stdlib.h> #define MAX_NUMBER 50 #define MAX_SIZE 200 int main(void) { const char fileName[] = "numbers.dat"; FILE *fileID; int i; char buffer[MAX_SIZE]; int value; int readCount; // Open file in text write mode // Write title and values to numbers file // Close file // Open file in text read mode // Read title and values from numbers file // Close file return 0; } // End main

  6. Open a File in Text Write Mode // Open file in text write mode fileID = fopen(fileName, "w"); if (fileID == NULL) { fprintf(stderr, "Error: Could not open %s in write mode\n", fileName); exit(1); } // End if // Write title and values to numbers file fprintf(fileID, "Integers from 1 to %d\n", MAX_NUMBER); for (i = 1; i <= MAX_NUMBER; i++) fprintf(fileID, "%5d", i); // Close file fclose(fileID);

  7. Open a File in Text Read Mode // Open file in text read mode fileID = fopen(fileName, "r"); if (fileID == NULL) { fprintf(stderr, "Error: Could not open %s in read mode\n", fileName); exit(1); } // End if // Read title and values from numbers file printf("\n=== CONTENTS OF NUMBERS FILE ===\n"); fgets(buffer, MAX_SIZE, fileID); printf("%s\n", buffer); while (!feof(fileID)) { readCount = fscanf(fileID, "%d", &value); printf("%5d", value); } // End while printf("\n================================\n"); // Close file fclose(fileID);

  8. Output from Demo Program === CONTENTS OF NUMBERS FILE === Integers from 1 to 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ================================ 

More Related