Advertisement
dmilicev

input_from_txt_file_v1.c

May 22nd, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. /*
  2.  
  3.     input_from_txt_file_v1.c
  4.  
  5.     Task:
  6.     https://web.facebook.com/photo.php?fbid=242589967061798&set=gm.1585074201651424&type=3&eid=ARCIFM0K7WTNghYPEzw9KVJugwAD1egoeIVYeTEbtjmJsRTI4-XDjLkrYOkf7UJOzVb0MnsxGIVSbIWD&ifg=1
  7.  
  8.     Make txt file with 3 lines each consist of string, long integer and ineger.
  9.     String is terminated by '_' character.
  10.     Read data from such txt file.
  11.  
  12.     Version with fgets() loads one line at a time and extracts data from such a string.
  13.  
  14.  
  15.     You can find all my C programs at Dragan Milicev's pastebin:
  16.  
  17.     https://pastebin.com/u/dmilicev
  18.  
  19. */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>          // for isdigit()
  25. #include<stdlib.h>          // for exit()
  26.  
  27. #define LEN 100             // length for strings
  28.  
  29.  
  30. // Create the appropriate txt file for input
  31. void make_txt_file_for_input( char FileName[] )
  32. {
  33.     FILE *p_file;           // output txt file
  34.  
  35.     p_file = fopen(FileName, "w");
  36.  
  37.     if( p_file == NULL ) {
  38.         printf("\n Error opening output text file ! \n");
  39.         exit(1);
  40.     }
  41.  
  42.     fprintf( p_file, "Journey To The Centre Of The Earth_   16585426    3 \n" );
  43.     fprintf( p_file, "The C Programming Language_       45213542    4 \n" );
  44.     fprintf( p_file, "Deitel C How To Program 8th Edition_  12685855    5 \n" );
  45.  
  46.     fclose(p_file);
  47. } // make_txt_file_for_input()
  48.  
  49.  
  50. // Read and print data from appropriate txt file
  51. void read_from_text_file( char FileName[] )
  52. {
  53.     FILE *p_file;           // input txt file
  54.     char str_line[LEN];     // string for whole one line of txt file
  55.     char str[LEN];          // for string str
  56.     char str_num1[LEN];     // for string num1
  57.     char str_num2[LEN];     // for string num2
  58.     unsigned num1;          // unsigned number
  59.     int num2;               // integer number
  60.     int i, j;               // loop itterators, indexes for strings
  61.  
  62.     p_file = fopen(FileName, "r");
  63.  
  64.     if( p_file == NULL ) {
  65.         printf("\n Error opening input text file ! \n");
  66.         exit(1);
  67.     }
  68.  
  69.     // fgets() loads one row at a time from the input file and processes it
  70.     while( fgets(str_line, LEN, p_file) != NULL )
  71.     {
  72.         printf("%s\n", str_line);   // loaded the whole line in string str_line
  73.  
  74.         // read str until character '_' , character '\n' or end of file EOF
  75.         i = 0;                      // reset index for str_line
  76.         while( str_line[i] != '_' && str_line[i] != '\n' && str_line[i] != EOF )
  77.         {
  78.             str[i] = str_line[i];
  79.             i++;
  80.         }
  81.         str[i] = '\0';              // to finish the string str, cutting '_'
  82.         printf("%s\n", str);
  83.  
  84.         // read num1
  85.         // skip until str_line[i] is not a number, character '\n' or end of file EOF
  86.         while( !isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
  87.         {
  88.             i++;
  89.         }
  90.  
  91.         // read digits of str_num1 while str_line[i] is digit, until character '\n' or end of file EOF
  92.         j = 0;                      // reset index for str_num1
  93.         while( isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
  94.         {
  95.             str_num1[j++] = str_line[i++];
  96.         }
  97.         str_num1[j] = '\0';         // to finish the string str_num1
  98.         num1 = atol(str_num1);      // convert string to long
  99.         printf("%u\n", num1);
  100.  
  101.         // read num2
  102.         // skip until str_line[i] is not a number, character '\n' or end of file EOF
  103.         while( !isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
  104.         {
  105.             i++;
  106.         }
  107.  
  108.         // read digits of str_num2 while str_line[i] is digit, until character '\n' or end of file EOF
  109.         j = 0;                      // reset index for str_num2
  110.         while( isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
  111.         {
  112.             str_num2[j++] = str_line[i++];
  113.         }
  114.         str_num2[j] = '\0';         // to finish the string str_num2
  115.         num2 = atoi(str_num2);      // convert string to long
  116.         printf("%d\n\n", num2);
  117.     }
  118.  
  119.     fclose(p_file);
  120. } // read_from_text_file()
  121.  
  122.  
  123. int main(void)
  124. {
  125.     // make txt file with data for latter input from
  126.     make_txt_file_for_input( "txt_file_for_input.txt" );
  127.  
  128.     // read and print data from text file
  129.     read_from_text_file( "txt_file_for_input.txt" );
  130.  
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement