FILE HANDLING IN C
File Handling in C:
File handling in C is the process in which we create, open, read, write, and close operations on a file.
a) Creating a new file.
b) Opening an existing file.
c) Reading from file.
d) Writing to a file.
e) Moving to a specific location in a file.
f) Closing a file.
a) Creating a new file.
b) Opening an existing file.
c) Reading from file.
d) Writing to a file.
e) Moving to a specific location in a file.
f) Closing a file.
A file pointer is a reference to a particular position in the opened file.
It is used in file handling to perform all file operations such as read, write, close, etc.
Access_mode: Specifies for what operation the file is being opened.
r --> opens for reading in text mode
w --> opens for writing in text mode.
a --> opens only in the append mode.
r+ --> opens for reading in text mode.
w+ --> contents are overwritten.
a+ --> opens the file in both reading and append mode.
Write to a File:
The file write operations can be performed by the functions fprintf() and fputs().
fprintf() --> Similar to printf(), this function uses formatted string and variable arguments list to print output to the file.
fputs() --> Prints the whole line in the file and a newline at the end.
fputc() --> Prints a single character into the file.
fputw() --> Prints a number to the file.
fwrite() --> This function writes the specified number of bytes to the binary file.
Reading From a File:
The file read operation in C can be performed using functions fscanf() or fgets().
fscanf() --> Use formatted string and variable arguments list to take input from a file.
fgets() --> Input the whole line from the file.
fgetc() --> Reads a single character from the file.
fgetw() --> Reads a number from a file.
fread() --> Reads the specified bytes of data from a binary file.
Closing a File:
The fclose() function is used to close the file.
EOF indicates the end of the file.
Move File Pointer:
File pointer generally points to the position according to the mode or last read/write operation. We can manually move this pointer to any position in the file using fseek() function.
fseek() --> It is used to set the position of a file pointer to a mentioned location.
ftell() --> It is used to return the current position of a file pointer.
rewind() --> It is used to set the file pointer to the beginning of a file.
Example:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
FILE* fp;
char CArr[100];
char ctok[20][100];
char* p;
int c;
int i;
fp = fopen("ABC.DAT", "w");
fprintf(fp, "%s\n", "FILE|EDIT|SEARCH");
fprintf(fp, "%s\n", "RUN|COMPILE|DEBUG");
fprintf(fp, "%s", "PROJECT|OPTIONS|WINDOW");
fclose(fp);
fp = fopen("ABC.DAT", "r");
c = 0;
while (!feof(fp))
{
fscanf(fp, "%s", CArr);
p = strtok(CArr, "|");
while (p != NULL)
{
strcpy_s(ctok[c], sizeof(p), p);
c++;
p = strtok(NULL, "|");
}
}
printf("\nc = %d", c);
printf("\n\n TOKENIZED STRINGS \n");
for (int i = 0; i < c; i++)
printf("\n%s", ctok[i]);
return 0;
}