C: Files - Applications resolved
1) Make a program that reads using items before entering, character by character from the keyboard to the meeting character '$' and pass them to a file whose name is passed from the keyboard or specified in the command line parameters. (write to file)
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *pf; //I declared pointer variable file char c,*name; printf("Enter the file name working: "); name=(char*)malloc(20*sizeof(char)); //in C standard was not necessary to write type, ie char* gets(name); if ((pf=fopen("C:\\test.txt", "w"))==NULL) //open or create a file for writing (possibly overwriting) //with 'a' instead of 'w' we created an addition to the existing contents of the file at the time { printf("Can not open file.\n"); getch(); exit(1); //forced out of the program with dissatisfied demand } puts("Write a text. The character '$' ends:"); do { c=getchar(); //in "c" enter a character read from the keyboard putc(c,pf); //it could be fputc(c,pf); } while (c !='$'); getchar(); fclose(pf); }
2) Writing to a file.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { char c; FILE *stream; /*declaring stream (stream data)*/ stream = fopen("C:\\test.txt", "w"); /*opening file*/ puts("Write a text. The character '$' ends:"); do { c=getchar(); //in "c" enter a character read from the keyboard putc(c,stream); //it could be fputc(c,pf); } while (c !='$'); getchar(); fclose(stream); }
3) Write a program to read from a file and display any ASCII character and display the content on screen.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *pf; /*declaring stream (stream data)*/ pf = fopen("C:\\test.txt", "r"); /*opening file*/ char c,*name; if(pf == NULL) { printf ("Eroare"); getch(); exit(1); } printf("Enter the file name working: "); name=(char*)malloc(20*sizeof(char)); gets(name); c=fgetc(pf); //read character from file or c=getc(pf) while (c!=EOF) // test if it has reached the end of file { putchar(c); //displays an on-screen character c=fgetc(pf); } fclose(pf); getchar(); }
4) Reading from a file to a string of character with the function fread / fgets.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { char s; FILE *stream; /*declaring stream (stream data)*/ stream = fopen("D:/test.txt", "r"); /*opening file*/ if(stream == NULL) { printf ("Can not open file.\n"); getch(); exit(1); } fread(&s, sizeof(char),100,stream); //or fgets(&s,100,stream); printf("%s", &s); getchar(); fclose(stream); //close }
5) To achieve a program that copies the contents of a file on the disk to another whose name is specified in the program.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { char string[256]; FILE *read, *writing; read = fopen("C:\\test.txt", "r"); writing = fopen("D:/test.txt", "w"); if(read == NULL) { printf ("Can not open file for reading.\n"); getch(); exit(1); } fgets(string,sizeof(string),read); /*read a string until it reaches the length characters read from the file specified */ fputs(string,writing); /*write the string to the file for writing*/ printf("Copied text is: %s", string); fclose(read); fclose(writing); getchar(); }
6) Indicating the number of bytes display content of specified byte remainder of the file from the specified byte.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { char s; FILE *stream; /*declaring stream (stream data)*/ stream = fopen("D:/test.txt", "r"); /*opening file*/ fseek (stream, (3)-1, SEEK_SET); if(stream == NULL) { printf ("Can not open file.\n"); getch(); exit(1); } printf("\nElement at position 3 is: %c.\n" , fgetc(stream)); puts("\nThe rest of the file from the above mentioned position is:"); while (!feof(stream)) //To test has reached the end file { printf("%c",fgetc(stream)); } getchar(); fclose(stream); //close }
7) Write a program that does calculations and displays them in a file using fprintf function.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *pf; pf = fopen("D:/test.txt", "r"); int quantity=20; float price=2378.64; if(pf == NULL) { printf ("error"); getch(); exit(1); } fprintf(pf, "\tTotaling:\n"); //displays the file fprintf(pf, "\t\tMoney: %14.2f\n",quantity*price); //displays the file printf("\tTotaling:\n"); //displayed on the screen printf("\t\tMoney: %14.2f\n",quantity*price); //displayed on the screen fclose(pf); getchar(); }
8) In the file "D :/ test.txt" is more numbers separated by spaces between them. Let them sort the vector elements using merge (direct selection or glasses), using an auxiliary file first file will be deleted and the new file will be called the old.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int n,i=0,j=0,temp,v[50]; FILE *f,*g; if ((f=fopen("D:\\test.txt", "r"))==NULL) printf("Error opening file for reading"); else if ((g=fopen("C:\\numbers.txt","w"))==NULL) { printf("Error opening file for writing.txt"); fclose(f); } else { while(!feof(f)) { fscanf(f,"%d",&v[i]); i++; } n=i; // final value of 'i' is the number of elements of the vector //sort elements from file for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if (v[j]<v[i]) { temp=v[i]; v[i]=v[j]; v[j]=temp; } //display elements from file for(i=0;i<n;i++) fprintf(g,"%5d",v[i]); fclose(f); remove("D:\\test.txt"); fclose(g); rename("C:\\numbers.txt", "C:\\test.txt"); printf("\nThe operation was performed"); } getchar(); }
9) It reads a text file containing integers separated by space or ENTER. Check the minimum and maximum of the existing values in that file and the number string amplitude (absolute difference between extremes).
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<limits.h> void main() { FILE *stream; /*declaring stream (stream data)*/ stream = fopen("D:\\test.txt", "r"); /*opening file*/ if(stream == NULL) { printf ("Eroare"); getch(); exit(1); } int x; //variable that holds the numbers read int maximum=-INT_MAX; //consider the smallest integer maximumum int minimum=INT_MAX; //consider the largest integer minimumum while(!feof(stream)) //as long as it's not end of file { fscanf(stream,"%d",&x); //read the sequence and compares them with "x" if (x>maximum) maximum=x; else if (x<minimum) minimum=x; } fclose(stream); printf("maximumum of the file is: %d\n",maximum); printf("minimumum of the file is: %d\n",minimum); printf("The amplitude of the file is: %d\n",maximum-minimum); getchar(); }
10) It reads a text file containing integers separated by space or ENTER. Check sum even numbers and odd numbers the sum of existing values in that file.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *stream; /*declaring stream (stream data)*/ stream = fopen("D:\\test.txt", "r"); /*opening file*/ if(stream == NULL) { printf ("Eroare"); getch(); exit(1); } int x; //variable that holds the numbers read int OddSum=0; int EvenSum=0; while(!feof(stream)) //as long as it's not end of file { fscanf(stream,"%d",&x); //read the sequence and compares them with x if(x % 2 == 0) EvenSum=EvenSum + x; if(x % 2 != 0) OddSum=OddSum + x; } fclose(stream); printf("OddSum is: %d\n",OddSum); printf("EvenSum is: %d\n",EvenSum); getchar(); }
11) It presupposes a text file ("vocale.txt") containing a string of characters. To count the occurrence of each voice file.
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<limits.h> void main() { char c; int a=0,e=0,i=0,o=0,u=0; //a = number of appearances of the vowel "a", etc. FILE *stream; /*declare stream (stream data)*/ stream = fopen("D:\\vowels.txt ", "r"); /*opening file*/ if(stream == NULL) { printf ("Error"); getch(); exit(1); } while(!feof(stream)) //as long as it's not end of file { fscanf(stream,"%c",&c); //or c=getc(f); or c=fgetc(f); switch(c) { case 'a': a++; break; case 'e': e++; break; case 'i': i++; break; case 'o': o++; break; case 'u': u++; }; } fclose(stream); printf("vowel \"a\" dispay %d times.\n",a); printf("vowel \"e\" dispay %d times.\n",e); printf("vowel \"i\" dispay %d times.\n",i); printf("vowel \"o\" dispay %d times.\n",o); printf("vowel \"u\" dispay %d times.\n",u); getchar(); }