6. chapter v

7
Disk File Input and Output

Transcript of 6. chapter v

Page 1: 6. chapter v

Disk File Input and Output

Page 2: 6. chapter v

• A file can refer to a disk file, a terminal, a printer, or a tape drive.• Stream is data flow you transfer from your program to a file, or vice versa.• buffer is a memory area, which is temporarily used to store data before it is sent to

its destination.• Step of file

– Opening a File FILE *fopen(char *filename,char *mode);

– Closing a File fclose(*pfile);

Note:• "w" Open text file for write operations. If the file exists, its current contents are

discarded.• "a" Open a text file for append operations. All writes are to the end of the file.• "r" Open a text file for read operations.

Page 3: 6. chapter v

Writing to a Text File

Page 4: 6. chapter v

Reading from a Text File

Page 5: 6. chapter v

#include <stdio.h>#include <string.h>#include <stdlib.h>const int LENGTH = 80; /* Maximum input length */int main(void){ char mystr[LENGTH]; /* Input string */ int lstr = 0; /* Length of input string */ int mychar = 0; /* Character for output */ FILE *pfile = NULL; /* File pointer */ char *filename = "C:\\myfile.txt"; printf("\nEnter string of less than 80 characters:\n"); fgets(mystr, LENGTH, stdin); /* Read in a string */ /* Create a new file we can write */ if(!(pfile = fopen(filename, "w"))) { printf("Error %s for writing. ", filename); exit(1); } lstr = strlen(mystr); for(int i = lstr-1 ; i >= 0 ; i--) fputc(mystr[i], pfile); /* Write string to file */ fclose(pfile); /* Close the file */

/* Open the file for reading */ if(!(pfile = fopen(filename, "r"))){ printf("Error opening %s for reading. Program terminated.", filename); exit(1); } /* Read a character from the file and display it */ while((mychar = fgetc(pfile)) != EOF) putchar(mychar); /* Output character from the file */ putchar('\n'); /* Write newline */ fclose(pfile); /* Close the file */ remove(filename); /* Delete the physical file */ return 0;}

Page 6: 6. chapter v

/* 21L02.c: Reading and writing one character at a time */#include <stdio.h>enum {SUCCESS, FAIL};void CharReadWrite(FILE *fin, FILE *fout);main(void){ FILE *fptr1, *fptr2; char filename1[]= "outhaiku.txt"; char filename2[]= "haiku.txt"; int reval = SUCCESS; if ((fptr1 = fopen(filename1, "w")) == NULL){ printf("Cannot open %s.\n", filename1); reval = FAIL; } else if ((fptr2 = fopen(filename2, "r")) == NULL){ printf("Cannot open %s.\n", filename2); reval = FAIL; } else { CharReadWrite(fptr2, fptr1); fclose(fptr1); fclose(fptr2); } return reval;}

/* function definition */void CharReadWrite(FILE *fin, FILE *fout){ int c; while ((c=fgetc(fin)) != EOF){ fputc(c, fout); /* write to a file */ putchar(c); /* put the character on the screen */ }}

Page 7: 6. chapter v

/* 21L03.c: Reading and writing one line at a time */#include <stdio.h>enum {SUCCESS, FAIL, MAX_LEN = 81};void LineReadWrite(FILE *fin, FILE *fout);main(void){ FILE *fptr1, *fptr2; char filename1[]= "outhaiku.txt"; char filename2[]= "haiku.txt"; int reval = SUCCESS; if ((fptr1 = fopen(filename1, "w")) == NULL){ printf("Cannot open %s for writing.\n", filename1); reval = FAIL; } else if ((fptr2 = fopen(filename2, "r")) == NULL){ printf("Cannot open %s for reading.\n", filename2); reval = FAIL; } else { LineReadWrite(fptr2, fptr1); fclose(fptr1); fclose(fptr2); }

return reval;}/* function definition */void LineReadWrite(FILE *fin, FILE *fout){ char buff[MAX_LEN]; while (fgets(buff, MAX_LEN, fin) != NULL){ fputs(buff, fout); printf("%s", buff); }}