problem solving using c full course paart 4
problem solving using c full course paart 4
A file is a collection of data that is stored on a storage medium such as a hard disk, solid-state drive
(SSD), USB flash drive, or other storage devices. Files are used to store various kinds of data, such
as text, images, videos, and application data. Files are typically organized by their names and
extensions (e.g., .txt, .bin, .exe). A file can be opened, read, written, or modified by a
computer program or operating system. In C, files are represented as pointers to the FILE structure.
2. Reading from a File: Files can be read character by character or in blocks. Common
functions for reading include:
• fgetc() – Reads a single character from a file.
• fgets() – Reads a line from the file.
• fread() – Reads a block of data.
3. Writing to a File: Data can be written using the following functions:
• fputc() – Writes a single character to a file.
• fputs() – Writes a string to a file.
• fwrite() – Writes a block of data to a file.
4. Closing a File: Once the operations on a file are completed, the file should be closed using
the fclose() function to release resources. Example:
c
Copy code
fclose(file);
Example:
c
Copy code
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number); // Reads an integer from the user
printf("You entered: %d\n", number); // Prints the entered number
return 0;
}
3. rewind(): Resets the file pointer to the beginning of the file. Example:
c
Copy code
rewind(file); // Moves the pointer to the start of the file
Example:
c
Copy code
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file) {
fputc('A', file); // Writes a character 'A' to the file
fclose(file);
}
return 0;
}
Example:
c
Copy code
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Total arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
12. Write a C Program to Display the Count of Characters, Words, and Lines in a File
c
Copy code
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (!file) {
printf("File not found!\n");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // Display the content
}
fclose(file);
return 0;
}
if (!source || !destination) {
printf("File error!\n");
return 1;
}
char ch;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination); // Copy character by character
}
fclose(source);
fclose(destination);
return 0;
}
15. Write a C Program to Append One File to Another File
c
Copy code
#include <stdio.h>
int main() {
FILE *source = fopen("source.txt", "r");
FILE *destination = fopen("destination.txt", "a");
if (!source || !destination) {
printf("File error!\n");
return 1;
}
char ch;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination); // Append content to the destination
}
fclose(source);
fclose(destination);
return 0;
}
int main() {
FILE *file = fopen("example.txt", "r+");
if (!file) {
printf("File not found!\n");
return 1;
}
char buffer[MAX];
int n = 5; // Number of characters to reverse
fread(buffer, sizeof(char), n, file);
reverse(buffer, n);
fseek(file, 0, SEEK_SET); // Move pointer back to the start
fwrite(buffer, sizeof(char), n, file);
fclose(file);
return 0;
}