File Handling Functions
C provides a set of functions to do operations on file. Moreover, These functions are known as file handling functions. Each function is used for some particular purpose.
fopen() (Open file)
- fopen used to open a file for operation.
- Two arguments should supply to fopen function,
- Moreover, Filename or full path of file to opened
- File opening mode which indicates which type of operations are permitted on file.
- Also, If a file opened successfully, it returns the pointer to file else NULL.
Example: fp = fopen(“Prog.c”,”r”); // Moreover, File name prog.c and it is opened for reading only.
fclose() (Close file)
- Opened files must closed when operations are over.
- Also, The function fclose used to close the file i.e. indicate that we finished processing this file.
- Moreover, To close a file, we have to supply file pointer to the fclose function.
- If the file closed successfully then it returns 0 else EOF.
fprintf() (Write formatted output to file)
- The fprintf function prints information in the file according to the specified format.
- fprintf() works just like printf(), the only difference is we have to pass the file pointer to the function.
- Moreover, It returns the number of characters outputted, or a negative number if an error occurs.
Example: fprintf(fp ”Sum = %d” sum);
fscanf() (Read formatted data from file)
- The function fscanf() reads data from the given file.
- Moreover, It works in a manner exactly like scanf(), the only difference is we have to pass the file pointer to the function.
- Also, If reading is succeeded then it returns the number of variables that are actually assigned values, or EOF if an error occurred.
Example: fscanf(fp, ”%d”, &sum);
fseek() (Reposition file position indicator)
- Sets the position indicator associated with the file pointer to a new position defined by adding offset to a reference position specified by origin.
- Moreover, You can use fseek() to move beyond a file, but not before the beginning.
- fseek() clears the EOF flag associated with that file.
- Also, We have to supply three arguments, file pointer, how many characters, from which location.
- It returns zero upon success, non‐zero on failure.
- The origin value should have one of the following values
rewind()(Set position indicator to the beginning)
- Sets the position indicator associated with a file to the beginning of the file.
- Moreover, A call to rewind is equivalent to fseek (fp, 0, SEEK_SET);
- On file open for update (read+write), a call to rewind allows switching between reading and writing.
Example: rewind (fp);
getc() (Get character from file)
- a getc function returns the next character from file or EOF if the end of file is reached.
- Also, After reading a character, it advances position in a file by one character.
- getc is equivalent to getchar().
- fgetc is identical to getc.
Example: ch = getc(fp);
putc() (Write character to file)
- putc writes a character to the file and advances the position indicator.
- Moreover,b After reading a character, it advances position in the file by one character.
- putc is equivalent to putchar().
- fgetc is identical to putc.
Example: putc(ch, fp);
getw() (Get integer from file)
- a getw function returns the next int from the file. If the error occurs then EOF returned.
Example: i = getw(fp);
putw() (Write integer to file)
- putw function writes integer to file and advances indicator to next position.
- Moreover, It succeeded then returns the same integer otherwise EOF returned.
Example: putw(I, fp);