File operations in C
Wed, 20 Nov 2024
File handling refers to working with files for tasks like:
FILE *
pointer is used to access a file.fopen()
function.FILE *file = fopen("file_name.extension", "mode")
mode
specifies how the file should be opened. Some arer
NULL
if file can't be opened or doesn't exist.w
a
w
, it doesn't overwrite the file content.r+
w+
a+
a
.rb
, wb
, rb+
, wb+
data.txt
)ASDFGH
QWERTY
#include<stdio.h>
int main(){
FILE* file = fopen("files/data.txt", "r");
if(file == NULL) { printf("File not found\n"); return 1; }
char ch;
while( (ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
fclose(file);
return 0;
}
fgetc
takes the file pointer and returns a char or EOF
at that position.ASDFGH
QWERTY
File content (data.txt
)
ABC CBA
123
DEF
321
Code
#include<stdio.h>
int main(){
FILE* file = fopen("files/data.txt", "r");
if(file == NULL) { printf("File not found\n"); return 1; }
char line[50];
int lineNo = 1;
while( fgets(line, 50, file) ) {
printf("%d: %s", lineNo, line);
lineNo++;
}
fclose(file);
return 0;
}
fgets
fgets(char * destination, int size, FILE * fptr);
size - 1
character from that line.Output
1: ABC CBA
2: 123
3: DEF
4: 321
scanf
function using fscanf
function.fscanf
is similar to scanf
, but it takes the file ptr as parameter followed by others.int fscanf(FILE *ptr, const char *format, ...);
data.txt
) contains roll name age in each line.1907002 Sina 21
1907030 Abdullah 22
1907046 Saif 23
#include<stdio.h>
int main(){
FILE* file = fopen("files/data.txt", "r");
if(file == NULL) { printf("File not found\n"); return 1; }
int roll, age;
char name[50];
printf("%-10s %-20s %-5s\n", "Roll", "Name", "Age");
while ( fscanf(file, "%d %s %d", &roll, name, &age) != EOF) {
printf("%-10d %-20s %-5d\n", roll, name, age);
}
fclose(file);
return 0;
}
Roll Name Age
1907002 Sina 21
1907030 Abdullah 22
1907046 Saif 23
fprintf()
or fputs()
to write to a file.#include<stdio.h>
int main(){
FILE* file = fopen("files/content.txt", "w");
if(file == NULL) { printf("Error\n"); return 1; }
fprintf(file, "%d %s %d\n", 1, "John", 20);
fprintf(file, "%d %s %d\n", 2, "Doe", 25);
fclose(file);
return 0;
}
content.txt
)1 John 20
2 Doe 25
3 Sun 53
at the end of the previos file(content.txt
). Code#include<stdio.h>
int main(){
FILE* file = fopen("files/content.txt", "a");
if(file == NULL) { printf("Error\n"); return 1; }
fprintf(file, "%d %s %d\n", 3, "Sun", 53);
fclose(file);
return 0;
}
1 John 20
2 Doe 25
3 Sun 53
#include<stdio.h>
int main(){
FILE* file = fopen("files/content.txt", "r+");
if(file == NULL) { printf("Error\n"); return 1; }
int roll, age;
char name[50];
FILE *temp = fopen("files/temp.txt", "w");
while ( fscanf(file, "%d %s %d", &roll, name, &age) != EOF) {
int rem = age % 10;
if(rem < 5) age -= rem;
else age += (10 - rem);
// age = ((age + 5) / 10) * 10;
fprintf(temp, "%d %s %d\n", roll, name, age);
}
fclose(file);
fclose(temp);
remove("files/content.txt");
rename("files/temp.txt", "files/content.txt");
return 0;
}
fseek
)#include<stdio.h>
int getNumLength(int num){
int length = 0;
while( num > 0 ){
num /= 10;
length++;
}
return length;
}
int main(){
FILE* file = fopen("files/content.txt", "r+");
if(file == NULL) { printf("Error\n"); return 1; }
int roll, age;
char name[50];
while ( fscanf(file, "%d %s %d", &roll, name, &age) != EOF) {
int oldAge = age;
age = ((age + 5) / 10) * 10;
int numLength = getNumLength(age);
int newNumLength = getNumLength(oldAge);
// moving the file pointer to the correct position
int position = ftell(file) - numLength;
fseek(file, position, SEEK_SET);
// writing the new age
fprintf(file, "%d", age);
// moving the file pointer to the next line
position += newNumLength;
fseek(file, position, SEEK_SET);
}
fclose(file);
return 0;
}
1 John 20
2 Doe 30
3 Sun 50
fclose()
when done.fclose(file);
\n
to \r\n
on Windows).1234
in binary format stores its actual 4-byte
binary representation, not its ASCII text equivalent.We can do it using fwrite
function.
Code:
#include<stdio.h>
int main(){
FILE* file = fopen("files/content.bin", "wb");
if(file == NULL) { printf("Error\n"); return 1; }
int arr[5] = {1, 2, 3, 4, 5};
fwrite(arr, sizeof(int), 5, file); // 5 is the len of the array
fclose(file);
return 0;
}
File content(content.bin
)
But if we read the array using code like this:
#include<stdio.h>
int main(){
FILE* file = fopen("files/content.bin", "rb");
if(file == NULL) { printf("Error\n"); return 1; }
int arr[5];
fread(arr, sizeof(int), 5, file);
for(int i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
printf("\n");
fclose(file);
return 0;
}
Output:
1 2 3 4 5
fwrite
.#include<stdio.h>
struct Student{
char name[20];
int age;
float marks;
};
int main(){
struct Student sami = {"Sami", 21, 90.5};
struct Student panda = {"Panda", 22, 85.5};
FILE* file = fopen("files/student.bin", "wb");
if(file == NULL) { printf("Error\n"); return 1; }
fwrite(&sami, sizeof(struct Student), 1, file);
fwrite(&panda, sizeof(struct Student), 1, file);
fclose(file);
return 0;
}
student.bin
)慓業 䊵慐摮a 䊫
#include<stdio.h>
struct Student{
char name[20];
int age;
float marks;
};
int main(){
struct Student sami, panda;
FILE* file = fopen("files/student.bin", "rb");
if(file == NULL) { printf("Error\n"); return 1; }
fread(&sami, sizeof(struct Student), 1, file);
fread(&panda, sizeof(struct Student), 1, file);
printf("%s %d %.2f\n", sami.name, sami.age, sami.marks);
printf("%s %d %.2f\n", panda.name, panda.age, panda.marks);
fclose(file);
return 0;
}
Sami 21 90.50
Panda 22 85.50
That's the end of our C journey. Welcome to the computer world.
"The only way to do great work is to love what you do."
— Steve Jobs
This is a comment 1.
This is a comment 2.