text file input and output. overview text file buffered i/o functions fopen function demo of file...

8
Text File Input and Output

Upload: winifred-hodges

Post on 17-Dec-2015

246 views

Category:

Documents


1 download

TRANSCRIPT

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

Text File Input and Output

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

Overview

• Text File Buffered I/O Functions

• fopen Function

• Demo of File Write and Read

Page 3: 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 Functionsint 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)

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

fopen Function

• "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

FILE *fopen(const char *filename, const char *mode);

Opens the file with the filename filename, associates it with a stream, and returnsa pointer to the object controlling the stream. If the open fails, it returns a NULLpointer. The initial characters of mode determine how the program manipulatesthe stream and whether it interprets the stream as text or binary. The charactersof mode must be one of the following sequences:

(This is an abridged list)

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

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

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

Open a File in Text Write Mode

// Open file in text write modefileID = 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 filefprintf(fileID, "Integers from 1 to %d\n", MAX_NUMBER);for (i = 1; i <= MAX_NUMBER; i++) fprintf(fileID, "%5d", i);

// Close filefclose(fileID);

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

Open a File in Text Read Mode

// Open file in text read modefileID = 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 fileprintf("\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 filefclose(fileID);

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

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================================