🔸Files
C views a file as a continuous sequence of bytes each byte can be read individually corresponds to the file structure in the Unix environment.
Last updated
C views a file as a continuous sequence of bytes each byte can be read individually corresponds to the file structure in the Unix environment.
Last updated
this is a full list of all native C API functions for handling files (including I/O functions). most of these are just wrappers for more simple functions which are also included in the list. to have a better understanding of each function and the differences between its a good practice to try all of them in different forms and situations.
in this section i have gathered some of the most used and most important C file functions that you will face a lot.
File handling functions | Description |
---|---|
the second arg is a character string that represents the file mode and specifies what you want to do with the file, a file mode specification is a character string between double quotes
example:
the integer that is returned will be 0 if the name change was successful nd nonzero otherwise
the file must not be open while renaming
flose() accepts a file pointer as an integer returns EOF (int) if an error occurs
if EOF was 0 the operation was successful
remove() declared in stdio.h header
function getc() is available as well, it gets an argument of type FILE* and returns the character read as type int virtually identical to fgetc() only difference between them is that getc() may be implemented as a macro whereas fgetc() is a function
we can read the content of a file again when necessary the rewind() function positions to the file that is specified by the file pointer argument at the beginning
the function reads a string into the memory area pointed to by str, from the file specified by stream , characters are read until either a '\n' is read or nchars-1 characters have been read from the stream whichever occurs first.
if a newline character is read its retained in the string, a '\0' character will be appended to the end of the string
if there is no error fgets() returns the pointer, str if there is an error, NULL is returned reading EOF causes NULL to be returned
the first argument to this function is the format a C string that contains one or more of the following items: • white space character • non-white space character • format specifiers • usage is similar to scanf but from a file
the simplest write operation is fputc() writes a single character to a text file
first arg is the character to write, second one is the file pointer returns the character that was written if successful returns EOF if failure
first argument is a string pointer and second is a file pointer
will write characters from a string until it reaches a '\0' character. doesnt write the null terminator character to the file can complicate reading back variable-length strings from a file that have been written by fputs()
expecting to write a line of text that has a newline character at the end
the first arg is file pointer, second is the format a C string that contains one or more of these items:
• whites paces
• non-whites paces
• format specifiers
usage is similar to printf but to a file if successful the total number of characters written is returned otherwise a negative number is returned
The C library function int fseek(FILE *stream, long int offset, int whence) sets the file position of the stream to the given offset.
example read the whole file:
fopen ()
fopen () function creates a new file or opens an existing file.
fclose ()
fclose () function closes an opened file.
getw ()
getw () function reads an integer from file.
putw ()
putw () functions writes an integer to file.
fgetc ()
fgetc () function reads a character from file.
fputc ()
fputc () functions write a character to file.
gets ()
gets () function reads line from keyboard.
puts ()
puts () function writes line to o/p screen.
fgets ()
fgets () function reads string from a file, one line at a time.
fputs ()
fputs () function writes string to a file.
feof ()
feof () function finds end of file.
fgetchar ()
fgetchar () function reads a character from keyboard.
fprintf ()
fprintf () function writes formatted data to a file.
fscanf ()
fscanf () function reads formatted data from a file.
fputchar ()
fputchar () function writes a character onto the output screen from keyboard input.
fseek ()
fseek () function moves file pointer position to given location.
SEEK_SET
SEEK_SET moves file pointer position to the beginning of the file.
SEEK_CUR
SEEK_CUR moves file pointer position to given location.
SEEK_END
SEEK_END moves file pointer position to the end of file.
ftell ()
ftell () function gives current position of file pointer.
rewind ()
rewind () function moves file pointer position to the beginning of the file.
getc ()
getc () function reads character from file.
getch ()
getch () function reads character from keyboard.
getche ()
getche () function reads character from keyboard and echoes to o/p screen.
getchar ()
getchar () function reads character from keyboard.
putc ()
putc () function writes a character to file.
putchar ()
putchar () function writes a character to screen.
printf ()
printf () function writes formatted data to screen.
sprinf ()
sprinf () function writes formatted output to string.
scanf ()
scanf () function reads formatted data from keyboard.
sscanf ()
sscanf () function Reads formatted input from a string.
remove ()
remove () function deletes a file.
fflush ()
fflush () function flushes a file.