File handling in C is the process of creating, reading, writing, and manipulating documents.
File handling in c:
C provides a set of standard library functions to manage documents.
File handling in C is essential because it allows programs to store data permanently, even after the program ends.
In C, variables and data stored in memory are temporary, meaning they are lost once the program completes execution.
File handling enables data persistence, allowing programs to read, write, and manage data on storage devices like hard drives or SSDs.
Here are the key concepts and functions involved in document handling in C:
File Pointer:
FILE *documentPtr;
Opening a Document
FILE *fopen(const char *documentname, const char *mode);
#include<stdio.h>
int main() {
// Open the document for writing using double backslashes or forward slashes
FILE *fileptr = fopen(“C:/Users/Inpixon 1/Documents/C_Program/hello.txt”, “w”);
if (fileptr == NULL) {
printf(“Document is not opened\n”);
return 1; // Exit if document could not be opened
}
// Write data to the document
fprintf(fileptr,”abc\n”);
// Close the document after writing
fclose(fileptr);
// Open the document for reading
FILE *in = fopen(“C:/Users/Inpixon 1/Documents/C_Program/hello.txt”, “r”);
if (in == NULL) {
printf(“Document could not be opened for reading\n”);
return 1; // Exit if document could not be opened
}
// Read data from the document
char buffer[100];
if (fgets(buffer, sizeof(buffer), in) != NULL) {
printf(“%s”, buffer);
}
// Close the document after reading
fclose(in);
return 0;
}
1.Text Documents(FILE)
fprintf() and fscanf() are used to write and read text data to and from a document.
In C, document operations allow you to interact with documents for reading, writing, and managing data. These operations are carried out using standard library functions provided in the <stdio.h> header. Here are the essential document operations in C:
Fopen() function returns a document pointer (FILE *), which is then used for all further operations on the document.
Syntax:
FILE *fopen(const char *documentname, const char *mode);
Reading from a File
There are several different functions to read data from a document:
char ch = fgetc(document);
char str[100];
fgets(str, 100, document);
Example:
char ch;
while ((ch = fgetc(document)) != EOF) {
putchar(ch); // Output character to the screen
}
Writing to a File
Functions to write a data:
fputc(‘A’, document);
fputs(“Hello, World!\n”, document);
fprintf(document, “Age: %d\n”, 25);
size_t fwrite(const void *ptr, size_t size, size_t count,FILE *document);
Example:
FILE *document = fopen(“output.txt”, “w”);
if (document == NULL) {
printf(“Error opening document for writing.\n”);
return;
}
fprintf(document, “This is a test.\n”);
fclose(document);
Closing a File
After performing the necessary operations on a document, it is important to close it to release resources.
Syntax:
int fclose(FILE *document);
Example:
fclose(document);
File Positioning
File pointers are used to keep track of the current location in the document. You can manipulate this pointer using the following functions:
long position = ftell(document);
fseek(document, 0, SEEK_SET); // Move to the beginning of the document
fseek(document, 0, SEEK_END); // Move to the end of the document
fseek(document, -10, SEEK_END); // Move 10 bytes back from the end
rewind(document);
Error Handling
You can check for errors during document operations using ferror() and clear errors with clearerr().
if (ferror(document)) {
printf(“Error reading document.\n”);
}
clearerr(document);
Checking End of Document (EOF)
if (feof(document)) {
printf(“End of document reached.\n”);
}
Example Program
Here’s an example that demonstrates the use of various document operations:
#include <stdio.h>
int main() {
FILE *document = fopen(“example.txt”, “w”);
if (document == NULL) {
printf(“Error opening document.\n”);
return 1;
}
// Writing to document
fprintf(document, “Hello, World!\n”);
fclose(document);
// Reading from document
document = fopen(“example.txt”, “r”);
if (document == NULL) {
printf(“Error opening document.\n”);
return 1;
}
char ch;
while ((ch = fgetc(document)) != EOF) {
putchar(ch);
}
fclose(document);
return 0;
}
Function | Description | Mode |
fopen() | Opens a document | Read, Write, Append |
fclose() | Closes the document | All modes |
fgetc() | Reads a single character | Read |
fgets() | Reads a string | Read |
fputc() | Writes a single character | Write |
fputs() | Writes a string | Write |
fprintf() | Writes formatted text | Write |
fread() | Reads binary data | Read |
fwrite() | Writes binary data | Write |
ftell() | Returns the current document pointer position | All modes |
fseek() | Moves the document pointer to a specific location | All modes |
rewind() | Resets the document pointer to the beginning | All modes |
feof() | Checks if the end of the document is reached | All modes |
ferror() | Checks for document errors | All modes |
Indian Institute of Embedded Systems – IIES