0% found this document useful (0 votes)
4 views

C File Handling Programs

The document provides ten beginner-level C programs focused on file handling operations. These programs cover creating, reading, writing, appending, counting characters and words, copying, renaming, deleting files, and reading integers line by line. Each program includes code snippets and demonstrates basic file operations using standard C library functions.

Uploaded by

techstack901
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C File Handling Programs

The document provides ten beginner-level C programs focused on file handling operations. These programs cover creating, reading, writing, appending, counting characters and words, copying, renaming, deleting files, and reading integers line by line. Each program includes code snippets and demonstrates basic file operations using standard C library functions.

Uploaded by

techstack901
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10 Beginner File Handling Programs in C

1. Create and Write to a File


#include <stdio.h>

int main() {
FILE *fp = fopen("file1.txt", "w");
if (fp != NULL) {
fprintf(fp, "Hello, World!");
fclose(fp);
}
return 0;
}

2. Read from a File


#include <stdio.h>

int main() {
char ch;
FILE *fp = fopen("file1.txt", "r");
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF)
putchar(ch);
fclose(fp);
}
return 0;
}

3. Append to a File
#include <stdio.h>

int main() {
FILE *fp = fopen("file1.txt", "a");
if (fp != NULL) {
fprintf(fp, "\nAppended line.");
fclose(fp);
}
return 0;
}
4. Count Characters in a File
#include <stdio.h>

int main() {
char ch;
int count = 0;
FILE *fp = fopen("file1.txt", "r");
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF)
count++;
fclose(fp);
printf("Total characters: %d\n", count);
}
return 0;
}

5. Copy File Content to Another File


#include <stdio.h>

int main() {
char ch;
FILE *src = fopen("file1.txt", "r");
FILE *dest = fopen("copy.txt", "w");
if (src != NULL && dest != NULL) {
while ((ch = fgetc(src)) != EOF)
fputc(ch, dest);
fclose(src);
fclose(dest);
}
return 0;
}

6. Count Words in a File


#include <stdio.h>
#include <ctype.h>

int main() {
FILE *fp = fopen("file1.txt", "r");
int count = 0;
char ch, prev = ' ';
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF) {
if (isspace(ch) && !isspace(prev))
count++;
prev = ch;
}
if (!isspace(prev)) count++;
fclose(fp);
printf("Total words: %d\n", count);
}
return 0;
}

7. Rename a File
#include <stdio.h>

int main() {
if (rename("file1.txt", "renamed.txt") == 0)
printf("File renamed successfully.\n");
else
perror("Error renaming file");
return 0;
}

8. Delete a File
#include <stdio.h>

int main() {
if (remove("file_to_delete.txt") == 0)
printf("File deleted successfully.\n");
else
perror("Error deleting file");
return 0;
}

9. Write and Read Integer Values


#include <stdio.h>

int main() {
FILE *fp = fopen("numbers.txt", "w");
if (fp != NULL) {
for (int i = 1; i <= 5; i++)
fprintf(fp, "%d\n", i);
fclose(fp);
}
fp = fopen("numbers.txt", "r");
int num;
while (fscanf(fp, "%d", &num) != EOF)
printf("%d ", num);
fclose(fp);
return 0;
}

10. Read Line by Line from a File


#include <stdio.h>

int main() {
char line[256];
FILE *fp = fopen("file1.txt", "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp))
printf("%s", line);
fclose(fp);
}
return 0;
}

You might also like