A Complete Guide - C Programming File Handling Basics FILE fp
Online Code run
Step-by-Step Guide: How to Implement C Programming File Handling Basics FILE fp
Step-by-Step Guide to File Handling in C
Step 1: Including Necessary Header File
To perform file handling operations in C, you need to include the standard I/O library header file:
#include <stdio.h>
Step 2: Opening a File
Use the fopen
function to open a file. The fopen
function returns a FILE
pointer which points to the file:
FILE *fp;
fp = fopen("filename.txt", "mode");
"filename.txt"
is the name of the file."mode"
specifies the purpose of opening the file (e.g., reading, writing).
Common modes are:
"r"
: Open for reading (the file must exist)."w"
: Open for writing (create a new file; if it exists, it will be overwritten)."a"
: Open for appending (create a new file if it does not exist; if it exists, the file pointer will be placed at the end of the file, and data will be written to the end)."rb"
,"wb"
,"ab"
: Binary versions of the above options.
Example:
#include <stdio.h> int main() { FILE *fp; fp = fopen("example.txt", "w"); if (fp == NULL) { perror("Error opening file"); return(-1); } fclose(fp); return 0;
}
Step 3: Writing to a File
Use fprintf
, fputc
, fwrite
, etc., to write to a file.
Using fprintf
:
#include <stdio.h> int main() { FILE *fp; fp = fopen("example.txt", "w"); if (fp == NULL) { perror("Error opening file"); return(-1); } fprintf(fp, "Hello, World!\n"); fclose(fp); return 0;
}
Using fputc
:
#include <stdio.h> int main() { FILE *fp; fp = fopen("example.txt", "w"); if (fp == NULL) { perror("Error opening file"); return(-1); } fputc('H', fp); fputc('e', fp); fputc('l', fp); fputc('l', fp); fputc('o', fp); fclose(fp); return 0;
}
Using fwrite
:
#include <stdio.h> int main() { FILE *fp; char data[100] = "Hello, World!\n"; fp = fopen("example.txt", "w"); if (fp == NULL) { perror("Error opening file"); return(-1); } fwrite(data, 1, sizeof(data), fp); fclose(fp); return 0;
}
Step 4: Reading from a File
Use fscanf
, fgetc
, fgets
, fread
, etc., to read from a file.
Using fscanf
:
#include <stdio.h> int main() { FILE *fp; char buffer[100]; fp = fopen("example.txt", "r"); if (fp == NULL) { perror("Error opening file"); return(-1); } while (fscanf(fp, "%s", buffer) == 1) { printf("%s ", buffer); } fclose(fp); return 0;
}
Using fgetc
:
#include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("example.txt", "r"); if (fp == NULL) { perror("Error opening file"); return(-1); } while ((ch = fgetc(fp)) != EOF) { putchar(ch); } fclose(fp); return 0;
}
Using fgets
:
#include <stdio.h> int main() { FILE *fp; char buffer[100]; fp = fopen("example.txt", "r"); if (fp == NULL) { perror("Error opening file"); return(-1); } while (fgets(buffer, sizeof(buffer), fp) != NULL) { printf("%s", buffer); } fclose(fp); return 0;
}
Using fread
:
#include <stdio.h> int main() { FILE *fp; char buffer[100]; fp = fopen("example.txt", "r"); if (fp == NULL) { perror("Error opening file"); return(-1); } if (fread(buffer, 1, sizeof(buffer), fp) > 0) { printf("%s", buffer); } fclose(fp); return 0;
}
Step 5: Closing a File
Always close the file after completing file operations using fclose
:
fclose(fp);
Complete Example
Here is a complete example that writes to a file and then reads from it:
Login to post a comment.