C File Examples: 1. C Program To Read Name and Marks of N Number of Students and Store Them in A File
C File Examples: 1. C Program To Read Name and Marks of N Number of Students and Store Them in A File
C File Examples: 1. C Program To Read Name and Marks of N Number of Students and Store Them in A File
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fclose(fptr);
return 0;
}
2. C program to read name and marks of n number of students from and
store them in a file. If the file previously exits, add the information to the
file.
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fptr = (fopen("C:\\student.txt", "a"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fclose(fptr);
#include <stdio.h>
struct student
{
char name[50];
int height;
};
int main(){
struct student stud1[5], stud2[5];
FILE *fptr;
int i;
fptr = fopen("file.txt","wb");
for(i = 0; i < 5; ++i)
{
fflush(stdin);
printf("Enter name: ");
gets(stud1[i].name);
Here is source code of the C Program to append the content of file at the end of another.
The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. /*
2. * C Program to Append the Content of File at the end of Another
3. */
4. #include <stdio.h>
5. #include <stdlib.h>
6.
7. main()
8. {
9. FILE *fsring1, *fsring2, *ftemp;
10. char ch, file1[20], file2[20], file3[20];
11.
12. printf("Enter name of first file ");
13. gets(file1);
14. printf("Enter name of second file ");
15. gets(file2);
16. printf("Enter name to store merged file ");
17. gets(file3);
18. fsring1 = fopen(file1, "r");
19. fsring2 = fopen(file2, "r");
20. if (fsring1 == NULL || fsring2 == NULL)
21. {
22. perror("Error has occured");
23. printf("Press any key to exit...\n");
24. exit(EXIT_FAILURE);
25. }
26. ftemp = fopen(file3, "w");
27. if (ftemp == NULL)
28. {
29. perror("Error has occures");
30. printf("Press any key to exit...\n");
31. exit(EXIT_FAILURE);
32. }
33. while ((ch = fgetc(fsring1)) != EOF)
34. fputc(ch, ftemp);
35. while ((ch = fgetc(fsring2) ) != EOF)
36. fputc(ch, ftemp);
37. printf("Two files merged %s successfully.\n", file3);
38. fclose(fsring1);
39. fclose(fsring2);
40. fclose(ftemp);
41. return 0;
42. }
C Program that Merges Lines Alternatively from 2 Files & Print
Result
This C Program merges lines alternatively from 2 files & print result.
Here is source code of the C Program to merge lines alternatively from 2 files & print result.
The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. /*
2. * C Program that Merges Lines Alternatively from 2 Files & Print Result
3. */
4. #include<stdio.h>
5. main()
6. {
7. char file1[10], file2[10];
8.
9. puts("enter the name of file 1"); /*getting the names of file to be
concatenated*/
10. scanf("%s", file1);
11. puts("enter the name of file 2");
12. scanf("%s", file2);
13. FILE *fptr1, *fptr2, *fptr3;
14. fptr1=fopen(file1, "r"); /*opening the files in read only
mode*/
15. fptr2=fopen(file2, "r");
16. fptr3=fopen("merge2.txt", "w+"); /*opening a new file in write,update
mode*/
17. char str1[200];
18. char ch1, ch2;
19. int n = 0, w = 0;
20. while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
21. {
22. if (ch1 != EOF) /*getting lines in alternately from two
files*/
23. {
24. ungetc(ch1, fptr1);
25. fgets(str1, 199, fptr1);
26. fputs(str1, fptr3);
27. if (str1[0] != 'n')
28. n++; /*counting no. of lines*/
29. }
30. if (ch2 != EOF)
31. {
32. ungetc(ch2, fptr2);
33. fgets(str1, 199, fptr2);
34. fputs(str1, fptr3);
35. if (str1[0] != 'n')
36. n++; /*counting no.of lines*/
37. }
38. }
39. rewind(fptr3);
40. while ((ch1 = fgetc(fptr3)) != EOF) /*countig no.of words*/
41. {
42. ungetc(ch1, fptr3);
43. fscanf(fptr3, "%s", str1);
44. if (str1[0] != ' ' || str1[0] != 'n')
45. w++;
46. }
47. fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n",
n, w - 1);
48. /*appendig comments in the concatenated file*/
49. fclose(fptr1);
50. fclose(fptr2);
51. fclose(fptr3);
52. }
Here is source code of the C Program to capitalize first letter of every word in a file. The C
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C Program to Capitalize First Letter of every Word in a File
3. */
4. #include <stdio.h>
5. #include <fcntl.h>
6. #include <stdlib.h>
7. int to_initcap_file(FILE *);
8.
9. void main(int argc, char * argv[])
10. {
11. FILE *fp1;
12. char fp[10];
13. int p;
14.
15. fp1 = fopen(argv[1], "r+");
16. if (fp1 == NULL)
17. {
18. printf("cannot open the file ");
19. exit(0);
20. }
21. p = to_initcap_file(fp1);
22. if (p == 1)
23. {
24. printf("success");
25. }
26. else
27. {
28. printf("failure");
29. }
30. fclose(fp1);
31. }
32.
33. /* capitalizes first letter of every word */
34. int to_initcap_file(FILE *fp)
35. {
36. char c;
37.
38. c = fgetc(fp);
39. if (c >= 'a' && c <= 'z')
40. {
41. fseek(fp, -1L, 1);
42. fputc(c - 32, fp);
43. }
44. while(c != EOF)
45. {
46. if (c == ' ' || c == '\n')
47. {
48. c = fgetc(fp);
49. if (c >= 'a' && c <= 'z')
50. {
51. fseek(fp, -1L, 1);
52. fputc(c - 32, fp);
53. }
54. }
55. else
56. {
57. c = fgetc(fp);
58. }
59. }
60. return 1;
61. }
Here is source code of the C Program to copy a file into another file. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C Program to Copy a File into Another File
3. */
4. #include <stdio.h>
5.
6. void main(int argc,char **argv)
7. {
8. FILE *fp1, *fp2;
9. char ch;
10. int pos;
11.
12. if ((fp1 = fopen(argv[1],"r")) == NULL)
13. {
14. printf("\nFile cannot be opened");
15. return;
16. }
17. else
18. {
19. printf("\nFile opened for copy...\n ");
20. }
21. fp2 = fopen(argv[2], "w");
22. fseek(fp1, 0L, SEEK_END); // file pointer at end of file
23. pos = ftell(fp1);
24. fseek(fp1, 0L, SEEK_SET); // file pointer set at start
25. while (pos--)
26. {
27. ch = fgetc(fp1); // copying file character by character
28. fputc(ch, fp2);
29. }
30. fcloseall();
31. }