PL FileOperation
PL FileOperation
Files
Places to read data from & write data to Including disk files & devices, e.g. monitor, printer A stream of bytes Most common streams: stdin (keyboard), stdout (monitor)
File Handling
High level
character-based, text file more programmer-friendly less efficient
Low level
raw memory format more efficient, work faster less programmer-friendly
Open a file:
fopen()
Write to a file:
fprintf(), fputs(), putc() , fputc()
Close a file:
fclose()
Others:
feof()
Open File
FILE *returnpointer; returnpointer = fopen("filename","mode");
or
FILE *returnpointer; char fname[15], mode[3]; ... returnpointer = fopen(fname,mode); Return Value On successful completion fopen() returns a pointer to the newly opened stream. In the event of error it returns NULL.
Open Mode
Value: Description
r: Open for reading only. w: Create for writing. If a file by that name already exists, it will be overwritten. a: Append; open for writing at end-of-file or create for writing if the file does not exist. r+: Open an existing file for update (reading and writing). w+: Create a new file for update (reading and writing). If a file by that name already exists, it will be overwritten. a+: Open for append; open (or create if the file does not exist) for update at the end of the file.
Write to File
fprintf() Format:
fprintf (fp,"string",variables);
Example:
int i = float x char ch fprintf 12; = 2.356; = 's'; (fp, "%d %f %c", i, x, ch);
Identical:
printf ("Hello world %d", 1); fprintf (stdout,"Hello world %d", 1);
Write to File
fprintf() returns the number of bytes output. In the event of error it returns EOF.
Write to File
Return Value
On success, fputc and putc return the character c. On error, it returns EOF.
Write to File
fputs()
char *str; int returnval; FILE *fp; // Open file for writing // ... returnval = fputs (str,fp);
Return Value
On success fputs returns a non-negative value. On error it returns a value of EOF.
FILE *fp; int n; // Open file for reading // ... n = fscanf (fp,"string",pointers);
Example:
int i; float x; char ch = 'x'; // Open file for reading // ... fscanf (fp, "%d %f %c", &i, &x, &ch);
Return Value
fscanf() returns the number of input fields successfully scanned, converted and stored. The return value does not include scanned fields that were not stored. If fscanf attempts to read at end-of-file, the return value is EOF. If no fields were stored, the return value is 0.
Return Value
On success fgetc returns the character read after converting it to an int without sign extension. On end-of-file or error it returns EOF.
fgets()
char *strbuff,*returnval; int n; FILE *fp; fp = fopen(data.dat,r); returnval = fgets(strbuff,n,fp);
Return Value
On success fgets returns the string pointed to by s; it returns NULL on end-of-file or error.
Others
feof()
FILE *fp; int outcome; // Open file for reading // ... while (!feof(fp)) { ch = getc(fp); }
Return Value
feof returns nonzero if an end-of-file indicator was detected on the last input operation on the named stream and 0 if end-of-file has not been reached.
Close File
int returncode; FILE *fp;
returncode = fclose (fp); if (fclose(fp) != 0) { printf ("File did not exist.\n"); error_handler(); }
Return Value
fclose returns 0 on success. It returns EOF if any errors were detected.
Data Hierarchy
Random Access
Usually variable-length records Accessed sequentially from first record to last Faster if you always access records in the same order e.g. tape drives, linkedlist
Normally fixed-length records Accessed directly without through other records Faster if you need to read or write records in a random order e.g. disk drive, array, airline reservation, banking