In this article we’ll talk about operating file functions in C, opening a file, closing a file, the getw and putw functions, the fprintf and fscanf functions, random access to files and the fseek function.
Functions for operating files:
Function | Operation |
fopen() | Create a new file Open an existing file |
fclose() | Closes a file in use |
fgetc() | Reads a character from a file |
fgets() | Reads a string from a file |
putc() | Write a character to a file |
fprintf() | Writes a set of data values to a file |
fscanf() | Reads the file at a defined size |
getw() | Reads an integer from a file |
putw() | Write an integer to the file |
fseek() | Point to a defined position in a file |
ftell() | Shows the current position in bytes |
rewind() | Points to the beginning of the file |
fopen()
To create or open a file or for reading and/or writing:
FILE *arq; arq = fopen("nome-do-arquivo","modo");
In“mode” we use a parameter that defines which operations will be performed on the file such as reading and/or writing, such parameters are defined as:
A – Open for reading.
R+ – Read and write.
W – Write, if a file already exists it will be subscribed, otherwise a new one will be created.
W+ – Write and read, if a file already exists it will be subscribed, otherwise a new one will be created.
RB – Reading from a binary file.
RB+ – Reading and writing to a binary file.
WB – Write to a binary file, if a file already exists it will be subscribed, otherwise a new one will be created.
WB+ – Write and read, if a file already exists it will be subscribed, otherwise a new one will be created.
fclose()
A file opened using the fopen() function can be closed with the fclose() function:
FILE *arq; arq = fopen("masterdaweb.txt", "r"); fclose(arq); // Fecha o arquivo
fgetc()
The fgetc() function reads a single character from a file.
fgetc(arq);
The example above reads a character from the arq file.
fgets()
The fgets() function reads to the end of a line if the length defined in the function parameter is sufficient, otherwise it reads the length defined.
char nome[100]; fgets(nome, sizeof(nome), arq);
The example above stores what was read in the variable name.
putc()
Writes a character to the file.
putc ('a', arq);
The example above writes the letter‘a‘ to the file arq.
getw() and putw()
They are similar to the GETC and PUTC functions and are used to read and write integer values:
FILE *arq; arq = fopen("masterdaweb.txt","w+"); putw(inteiro,arq); getw(arq);
fprintf() and fscanf()
The fprintf and fscanf functions are identical to the printf and scanf functions, the difference being that they are used in files.
fprintf(arq,%s%d%f”,nome,idade,7.5); fscanf(arq,"%s%d”,nome,&idade");
fseek()
Points to a position counted in bytes from a given position:
fseek(arq, 50, SEEK_SET);
The example above points to the 50-byte position counted from SEEK_SET (start of file).
SEEK_SET – Start of file.
SEEK_CUR – Current position.
SEEK_END – End of file.
ftell()
Shows the current position in the file, counted from the start of the file.
ftell(arq);
rewind()
Points to the beginning of the file.
rewind(arq);