90 likes | 269 Views
Text files. Text file contains ASCII-characters New line and end-of-file are special characters in a text file Ex. 1 This is a text<newline>It has two lines<newline><end-of-file> Examples of predifined text files stdin, stdout, stderr (these are file pointers)
E N D
Text files • Text file contains ASCII-characters • New line and end-of-file are special characters in a text file Ex. 1 • This is a text<newline>It has two lines<newline><end-of-file> • Examples of predifined text files stdin, stdout, stderr (these are file pointers) • stderr always associated with the screen • stdin, stdout might be re-directed to other text files TDBA66, vt-04, Lecture Ch9
EOF EOF is defined in <stdio.h> and is returned from scanf() or fscanf() if end-of-file of the read file is detected Possible to check if EOF is read by e.g. for (status=scanf(”%d”, &num); status != EOF; status=scanf(”%d”, &num)) process(num); e.g. while (fscanf(infpt,”%d”, &num) != EOF) process(num); TDBA66, vt-04, Lecture Ch9
Declare file pointers • Open a file for reading or writing • -check if opening was ok • Use it/them • Close them before ending program • Ex. 1 • FILE *inf, *outf; • if ((inf=fopen(”filname1.txt”,”r”)) == NULL){ • fprintf(stderr,”Error when opening filname1.txt”); • exit(-1); • } • if ((outf=fopen(”filname2.txt”,”w”))== NULL){ • fprintf(stderr,”Error when opening filname2.txt”); • exit(-2); • } Using text files TDBA66, vt-04, Lecture Ch9
/* Makes a backup file. Repeatedly prompts for the name of a file to * back up until a name is provided that corresponds to an available * file. Then it prompts for the name of the backup file and creates * the file copy. */ #include <stdio.h> #define STRSIZ 80 intmain(void){ char in_name[STRSIZ], /* strings giving names */ out_name[STRSIZ]; /* of input and backup files */ FILE *inp, /* file pointers for input and */ *outp; /* backup files */ char ch; /* one character of input file */ int status; /* status of input operation */ /* Get the name of the file to back up and open the file for input*/ printf("Enter name of file you want to back up> "); for (scanf("%s", in_name); (inp = fopen(in_name, "r")) == NULL; scanf("%s", in_name)) { printf("Cannot open %s for input\n", in_name); printf("Re-enter file name> "); } TDBA66, vt-04, Lecture Ch9
/*Get name to use for backup file and open file for output */ printf("Enter name for backup copy> "); for (scanf("%s", out_name); (outp = fopen(out_name, "w")) == NULL; scanf("%s", out_name)) { printf("Cannot open %s for output\n", out_name); printf("Re-enter file name> "); } /* Make backup copy one character at a time */ for (status = fscanf(inp, "%c", &ch); status != EOF; status = fscanf(inp, "%c", &ch)) fprintf(outp, "%c", ch); /* Close files and notify user of backup completion */ fclose(inp); fclose(outp); printf("Copied %s to %s.\n", in_name, out_name); return(0); } TDBA66, vt-04, Lecture Ch9
Binary files • Binary files can not be created by an editor (as with text files) • A binary file is created by writing on it from a C-program • If a human should not read the file it saves time and memory to use binary files • The internal representation in bits ia written to a binary file • Contents of databases are typically binary TDBA66, vt-04, Lecture Ch9
Example Figure 10.3 (extended) Creating a Binary File of Integers and read them into a vector of integers FILE *binaryp, *inbinp; int i, list[255]; binaryp = fopen("nums.bin", "wb"); for (i = 2; i <= 500; i += 2) fwrite(&i, sizeof (int), 1, binaryp); fclose(binaryp); /* read 250 integers into a vector */ inbinp = fopen(”nums.bin”,”rb”); fread(list, sizeof(int), 250, inbinp); fclose(inbinp); ………… ………… TDBA66, vt-04, Lecture Ch9
Figure 10.4 Outline and Function main for Metals Database Inquiry Program /* * Displays all metals in the database that satisfy the search * parameters specified by the program user. */ #include <stdio.h>#include <string.h>#define MAX_DENSITY 20.0 /*maximum density (g/cm^3)*/#define MAX_MELT_PT 4000 /*maximum melting point(deg.C)*/#define MAX_TENS_MOD 350 /*maximum tensile modulus(GPa)*/#define MAX_DAYS 90 /* maximum days to delivery */#define STR_SIZ 80 /* number of characters in string*/typedef struct { /* metal structure type */ char name[STR_SIZ]; double density; /* g/cm^3 */ int melt_pt, /* melting point in degrees C */ tens_mod, /* tensile modulus in GPa */ days_to_deliv; /* days from order to delivery */} metal_t;typedef struct { /* search parameter bounds type */ char low_name[STR_SIZ], high_name[STR_SIZ]; double low_density, high_density; int low_melt_pt, high_melt_pt, low_tens_mod, high_tens_mod, low_days, high_days;} search_params_t; TDBA66, vt-04, Lecture Ch9
Fig. 10.4 cont. /* Insert prototypes of other functions needed.*/ /* * Prompts the user to enter the search parameters. */ search_params_t get_params(void); /* * Displays records of all metals in the inventory that satisfy search parameters. */ void display_match(FILE *databasep, /* input - file pointer to binary database file*/ search_params_t params);/* input - search parameter bounds*/ int main(void){ char inv_filename[STR_SIZ]; /*name of inventory file */ FILE *inventoryp; /* inventory file pointer */ search_params_t params; /* search parameter bounds */ /* Get name of inventory file and open it */ printf("Enter name of inventory file> "); scanf("%s", inv_filename); fflush(stdin); inventoryp = fopen(inv_filename, "rb"); /* Get the search parameters */ params = get_params(); /*Display all metals that satisfy the search parameters */ display_match(inventoryp, params); return(0); } TDBA66, vt-04, Lecture Ch9