0% found this document useful (0 votes)
47 views

PL FileOperation

This document summarizes file operations in C/C++. It discusses that files are places to read and write data, including disk files and devices. It outlines high-level and low-level file handling, and describes functions for opening, writing, reading, and closing files at the high level like fopen(), fprintf(), fscanf(), and fclose(). It also provides details on modes for opening files and return values for the different file functions. Finally, it distinguishes between sequential access files that are accessed sequentially from first to last record versus random access files that allow direct access to records.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

PL FileOperation

This document summarizes file operations in C/C++. It discusses that files are places to read and write data, including disk files and devices. It outlines high-level and low-level file handling, and describes functions for opening, writing, reading, and closing files at the high level like fopen(), fprintf(), fscanf(), and fclose(). It also provides details on modes for opening files and return values for the different file functions. Finally, it distinguishes between sequential access files that are accessed sequentially from first to last record versus random access files that allow direct access to records.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

File Operation

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

High Level File Operation

Open a file:
fopen()

Write to a file:
fprintf(), fputs(), putc() , fputc()

Read from a file:


fscanf(), fgets(), getc(), fgetc()

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() Return Value

fprintf() returns the number of bytes output. In the event of error it returns EOF.

Write to File

putc() and fputc()


FILE *fp; char ch; int returncode; // Open file for writing // ... returncode = fputc(ch,fp); returncode = putc(ch,fp);

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.

Read from File


fscanf() Format:

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);

Read from File

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.

Read from File

getc() and fgetc()


FILE *fp; char ch; /* open file */ fp = fopen(data.dat,r); ch = getc(fp); ch = fgetc(fp);

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.

Read from File

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

Sequential Access File vs Random Access File


Sequential Access

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

Sequential Access File vs Random Access File

Sequential Access File Example


SequentialFile.cpp

You might also like