Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- input_from_txt_file_v1.c
- Task:
- https://web.facebook.com/photo.php?fbid=242589967061798&set=gm.1585074201651424&type=3&eid=ARCIFM0K7WTNghYPEzw9KVJugwAD1egoeIVYeTEbtjmJsRTI4-XDjLkrYOkf7UJOzVb0MnsxGIVSbIWD&ifg=1
- Make txt file with 3 lines each consist of string, long integer and ineger.
- String is terminated by '_' character.
- Read data from such txt file.
- Version with fgets() loads one line at a time and extracts data from such a string.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h> // for isdigit()
- #include<stdlib.h> // for exit()
- #define LEN 100 // length for strings
- // Create the appropriate txt file for input
- void make_txt_file_for_input( char FileName[] )
- {
- FILE *p_file; // output txt file
- p_file = fopen(FileName, "w");
- if( p_file == NULL ) {
- printf("\n Error opening output text file ! \n");
- exit(1);
- }
- fprintf( p_file, "Journey To The Centre Of The Earth_ 16585426 3 \n" );
- fprintf( p_file, "The C Programming Language_ 45213542 4 \n" );
- fprintf( p_file, "Deitel C How To Program 8th Edition_ 12685855 5 \n" );
- fclose(p_file);
- } // make_txt_file_for_input()
- // Read and print data from appropriate txt file
- void read_from_text_file( char FileName[] )
- {
- FILE *p_file; // input txt file
- char str_line[LEN]; // string for whole one line of txt file
- char str[LEN]; // for string str
- char str_num1[LEN]; // for string num1
- char str_num2[LEN]; // for string num2
- unsigned num1; // unsigned number
- int num2; // integer number
- int i, j; // loop itterators, indexes for strings
- p_file = fopen(FileName, "r");
- if( p_file == NULL ) {
- printf("\n Error opening input text file ! \n");
- exit(1);
- }
- // fgets() loads one row at a time from the input file and processes it
- while( fgets(str_line, LEN, p_file) != NULL )
- {
- printf("%s\n", str_line); // loaded the whole line in string str_line
- // read str until character '_' , character '\n' or end of file EOF
- i = 0; // reset index for str_line
- while( str_line[i] != '_' && str_line[i] != '\n' && str_line[i] != EOF )
- {
- str[i] = str_line[i];
- i++;
- }
- str[i] = '\0'; // to finish the string str, cutting '_'
- printf("%s\n", str);
- // read num1
- // skip until str_line[i] is not a number, character '\n' or end of file EOF
- while( !isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
- {
- i++;
- }
- // read digits of str_num1 while str_line[i] is digit, until character '\n' or end of file EOF
- j = 0; // reset index for str_num1
- while( isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
- {
- str_num1[j++] = str_line[i++];
- }
- str_num1[j] = '\0'; // to finish the string str_num1
- num1 = atol(str_num1); // convert string to long
- printf("%u\n", num1);
- // read num2
- // skip until str_line[i] is not a number, character '\n' or end of file EOF
- while( !isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
- {
- i++;
- }
- // read digits of str_num2 while str_line[i] is digit, until character '\n' or end of file EOF
- j = 0; // reset index for str_num2
- while( isdigit( (int)str_line[i] ) && str_line[i] != '\n' && str_line[i] != EOF )
- {
- str_num2[j++] = str_line[i++];
- }
- str_num2[j] = '\0'; // to finish the string str_num2
- num2 = atoi(str_num2); // convert string to long
- printf("%d\n\n", num2);
- }
- fclose(p_file);
- } // read_from_text_file()
- int main(void)
- {
- // make txt file with data for latter input from
- make_txt_file_for_input( "txt_file_for_input.txt" );
- // read and print data from text file
- read_from_text_file( "txt_file_for_input.txt" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement