Govt. Exams
Entrance Exams
stdio.h (Standard Input/Output) contains all file handling functions like fopen(), fclose(), fread(), fwrite(), etc.
Third parameter is 'whence': SEEK_SET (beginning), SEEK_CUR (current position), SEEK_END (end). Second parameter specifies offset in bytes.
fopen() returns NULL pointer on failure. Proper practice is to check: if(fp == NULL) {...error handling...}. Returning -1 is for system calls like open().
fscanf() reads formatted data from file (like scanf() but from file). fgets() reads strings. fread() reads binary data. getc() reads single character.
SEEK_END moves pointer relative to end of file. Offset 0 means go to the exact end. SEEK_SET (beginning) and SEEK_CUR (current) are other origin options.
fprintf() writes formatted data to file (like printf() but to file). fscanf() reads formatted data. fwrite() writes binary data. fputs() writes strings.
fclose() terminates the file stream, flushes any buffered data to disk, and releases the file handle. It doesn't delete or clear the file.
"w+" opens file for reading and writing, truncating it if it exists, and creating it if it doesn't. "r+" requires file to exist. "a+" opens in append mode with read capability.
fopen() uses double quotes for string arguments in C. The first argument is filename and second is mode. Single quotes are for characters, not strings.
ftell() returns the current position (as a long integer) of the file pointer in the file. fseek() is used to change position, rewind() resets to beginning, and fgetpos() stores position in a structure.