Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. FILE * createNewFile(char fname[20]){
  6.      FILE *fpt;
  7.      fpt = fopen(fname,"w");
  8.      if(fpt==NULL){
  9.                printf("Cannot open file!\n");
  10.                exit(0);
  11.      }
  12.      return fpt;
  13. }
  14.  
  15. FILE * openToAddFile(char fname[20]){
  16.      FILE *fpt;
  17.      fpt = fopen(fname,"a");
  18.      if(fpt==NULL){
  19.                printf("Cannot open file!\n");
  20.                exit(0);
  21.      }
  22.      return fpt;
  23. }
  24.  
  25. FILE * openToReadFile(char fname[20]){
  26.      FILE *fpt;
  27.      fpt = fopen(fname,"r");
  28.      if(fpt==NULL){
  29.                printf("Cannot open file!\n");
  30.                exit(0);
  31.      }
  32.      return fpt;
  33. }
  34.  
  35. void printAll(FILE *fpt){
  36.      char ch;
  37.      ch = getc(fpt);
  38.      while(ch!=EOF){
  39.         printf("%c",ch);
  40.         ch = getc(fpt);
  41.      }
  42. }
  43.  
  44. int countChar(FILE *fpt){
  45.      char ch;
  46.      int num =0;
  47.      ch = getc(fpt);
  48.      while(ch!=EOF){
  49.         num++;
  50.         ch = getc(fpt);
  51.      }
  52.      return num;
  53. }
  54. void writeLineToFile(FILE *fpt, char str[30]){
  55.      fprintf(fpt,"%s\n",str);
  56. }
  57. void readLineFromFile(FILE *fpt){
  58.      char str[30];
  59.      fscanf(fpt,"%s",str);
  60.      if(feof(fpt)){
  61.                 printf("Empty\n");          
  62.      }else{    
  63.                 printf("%s\n",str);
  64.      }
  65. }
  66. void printWordFromFile(FILE *fpt){
  67.      char str[30];
  68.      
  69.      do{
  70.              fscanf(fpt,"%s",str);
  71.              if(!feof(fpt)) printf("%s\n",str);        
  72.      
  73.      }while(!feof(fpt));
  74. }
  75.  
  76. int main(){
  77.  FILE *fpt;
  78.  fpt = createNewFile("test.txt");
  79.  writeLineToFile(fpt,"12345  aa ccc ssss");
  80.  writeLineToFile(fpt,"987565 \t XXX ZZ\nZ");
  81.  fclose(fpt);
  82.  
  83.  fpt = openToReadFile("test.txt");
  84.  printWordFromFile(fpt);
  85.  fclose(fpt);
  86.  getch();  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement