[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

File Handling in C

The document provides an overview of file handling in C, covering text and binary files, including how to open, close, read, and write to files. It details the syntax for file operations and includes examples for both text and binary file handling. Additionally, it explains command line arguments for passing input to the main function.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

File Handling in C

The document provides an overview of file handling in C, covering text and binary files, including how to open, close, read, and write to files. It details the syntax for file operations and includes examples for both text and binary file handling. Additionally, it explains command line arguments for passing input to the main function.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

File Handling in C

File Handling Basics

In C, files are used to store data permanently on disk. You can work with:

 Text files – store readable characters.


 Binary files – store raw binary data (efficient but not human-readable).

To work with files, include the header:

#include <stdio.h>

Opening and Closing Files

Syntax:

FILE *fp;
fp = fopen("filename.txt", "mode");
Mode Description
"r" Open for reading (file must exist).
"w" Open for writing (creates file or overwrites if exists).
"a" Open for appending.
"rb" Open binary file for reading.
"wb" Open binary file for writing.

To close a file:

fclose(fp);

Text File Operations

Function Description
fgetc(fp) Reads a single character.
fputc(char, fp) Writes a single character.
fgets(str, n, fp) Reads a string (line) from file.
fputs(str, fp) Writes a string to file.
✅ Example: Reading character-by-character

FILE *fp = fopen("data.txt", "r");


char ch;
while ((ch = fgetc(fp)) != EOF)
putchar(ch);
fclose(fp);

✅ Example: Writing a string

FILE *fp = fopen("data.txt", "w");


fputs("Hello, File Handling!", fp);
fclose(fp);

Binary File Operations

Function Description
fread(ptr, size, count, fp) Reads binary data.
fwrite(ptr, size, count, fp) Writes binary data.

✅ Example: Writing a structure to a binary file

struct Student {
int id;
char name[20];
};
struct Student s = {1, "John"};

FILE *fp = fopen("student.dat", "wb");


fwrite(&s, sizeof(s), 1, fp);
fclose(fp);

✅ Reading it back:

struct Student s;
FILE *fp = fopen("student.dat", "rb");
fread(&s, sizeof(s), 1, fp);
fclose(fp);

Command Line Arguments

They allow input to be passed to main() when the program starts.

Syntax:
int main(int argc, char *argv[])
Parameter Meaning
argc Argument count (number of arguments).
argv[] Argument vector (array of strings).

Example:

#include <stdio.h>

int main(int argc, char *argv[]) {


printf("Program name: %s\n", argv[0]);
if (argc > 1) {
printf("First argument: %s\n", argv[1]);
}
return 0;
}

Summary Table

Task Function Example


Open file fopen() fopen("file.txt", "r");
Close file fclose() fclose(fp);
Read character fgetc() fgetc(fp);
Write character fputc() fputc('A', fp);
Read string fgets() fgets(buf, 100, fp);
Write string fputs() fputs("text", fp);
Read binary fread() fread(&x, sizeof(x), 1, fp);
Write binary fwrite() fwrite(&x, sizeof(x), 1, fp);

You might also like