Day 10 C-Programming
Day 10 C-Programming
File Types in C
Files in C are used for permanent storage of data. C supports two types of files:
File Operations in C
File Pointer
Syntax:
FILE *fp;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("File could not be opened\n");
}
fclose(fp);
Mode Description
Example:
Example:
fclose(fp);
Example:
char ch = fgetc(fp);
Example:
fputc('A', fp);
#include <stdio.h>
int main() {
FILE *file;
char content[] = "This is a sample text written into the file.\nThis is the second line.\n";
return 0;
}
Example:
Write a program that takes 30 numbers as input, writes them into a file, then separates even
and odd numbers into two different files named EVEN.txt and ODD.txt
#include <stdio.h>
int main() {
FILE *inputFile, *evenFile, *oddFile;
int numbers[30];
int i;
// Separate the even and odd numbers and write them to respective files
for (i = 0; i < 30; i++) {
if (numbers[i] % 2 == 0) {
fprintf(evenFile, "%d\n", numbers[i]);
} else {
fprintf(oddFile, "%d\n", numbers[i]);
}
}
return 0;
}
Example:
example of fseek, ftell, and rewind
#include <stdio.h>
int main() {
// Open a file for writing
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
// Move the file pointer 5 bytes ahead from the current position
fseek(file, 5, SEEK_CUR);
printf("File pointer moved to position: %ld\n", ftell(file)); // Prints 11
return 0;
}
Syntax:
Example:
#include <stdio.h>
char ch;
while ((ch = fgetc(src)) != EOF) {
fputc(ch, dest);
}
fclose(src);
fclose(dest);
printf("File copied successfully.\n");
return 0;
}
Example:
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee e = {1, "Alice", 50000.0};
Preprocessor Directives
a. Macros:
● Macro is a piece of code in a program that is replaced by the value of the macro.
● Macros are constants or functions defined using the #define directive.
Example:
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int main() {
printf("PI: %.2f\n", PI);
printf("Square of 5: %d\n", SQUARE(5));
return 0;
}
#include <stdio.h>
#define PI 3.14159
#define AREA_OF_CIRCLE(radius) (PI * (radius) * (radius))
int main() {
float radius;
return 0;
}
b. File Inclusion:
1. #include <filename.h>:
The use of this syntax is to include system header file in C program. It tells the compiler to look
for or find the file in the standard system directory.
2. #include “filename”:
This syntax is used to add a header file. It tells the compiler to first look for the file in the current
directory and then look in the system directory.
#include <stdio.h>
#include "myheader.h" // User-defined header file
___________________
Programs to practice(HW)
● Program 1: Create a text file and write data into it using fwrite().
● Program 2: Read data from a file using fread() and display it on the screen.
● Program 3: Append data to an existing file without overwriting using fopen() in append
mode.
● Program 4: Move the file pointer using fseek(), ftell(), and rewind(), and
display the current file pointer position at various stages.
● Program 5: Implement a program that uses fseek() to search for a specific word or
pattern in a file.
● Program 6: Read and display file content in reverse order using fseek().
3. File Opening Modes
● Program 7: Open a file in different modes ("r", "w", "a", "r+", "w+", "a+") and
handle errors for each mode.
● Program 8: Create a program that handles errors gracefully when trying to open a file
that doesn’t exist in "r" mode, or when trying to write in "r" mode.
● Program 9: Use fopen(), fclose(), fgetc(), and fputc() to read and write one
character at a time in a file.
● Program 10: Implement a program that reads from a file line-by-line using fgets() and
writes the content to another file.
● Program 11: Write a program that reads and writes a structured record (e.g., struct
type) to/from a binary file using fwrite() and fread().
● Program 12: Write a program that takes two numbers from the command line, adds
them, and displays the sum.
● Program 13: Create a program that takes a list of strings as command-line arguments
and prints each string.
● Program 14: Implement a program that takes a file name as a command-line argument,
reads the content of that file, and prints it.
● Program 15: Write a program that accepts a file name as a command-line argument,
opens the file, reads the content, and displays it.
● Program 16: Create a program that accepts file names from the command line, and
copies content from the source file to the destination file.
● Program 17: Implement a program that accepts a file name and a word from the
command line and counts how many times the word appears in the file.
● Program 18: Implement a program to write student records (name, roll number, marks)
into a file using struct and binary file operations (fwrite(), fread()).
● Program 19: Write a program to read and display student records from a file, and
update a student record (e.g., modify marks) in the file.
● Program 20: Create a program that writes multiple records (students) into a file, then
reads the file and displays the records in tabular format.
8. Preprocessor Directives: Macros and File Inclusion
● Program 21: Implement a program that uses macros to find the maximum of two
numbers.
● Program 22: Create a program with a header file (.h) that contains function prototypes
for file operations, and implement the functions in a separate .c file.
● Program 23: Write a program that includes an external header file with preprocessor
directives to define constants and use them in the program.
● Program 24: Implement a program that defines a macro for a factorial and uses it to
compute the factorial of a number.
9. (Mixed Topics)
● Program 25: Write a program that takes a list of students' names and grades from the
user, writes them to a file, and later reads and sorts the data in ascending order based
on grades.
● Program 26: Create a program to count the number of lines, words, and characters in a
file using fgetc() or fgets().
● Program 27: Write a program that reads integers from a file, computes their average,
and writes the result to a new file.