This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Previous revision | ||
cs:c_language:file_reading_1 [2019/02/26 14:35] |
cs:c_language:file_reading_1 [2021/04/08 11:09] (current) zioskenz |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== File reading (Example 1) ====== | ||
+ | **Concepts: | ||
+ | Function for open, close and read a file. Example on how to read a file row-by-row, word-by-word, | ||
+ | **Text:**\\ | ||
+ | Implements a C program that: | ||
+ | * Reads the following file: | ||
+ | <file txt file.txt> | ||
+ | First row | ||
+ | Second row | ||
+ | </ | ||
+ | * a row at a time (row-by-row) | ||
+ | * a word at a time (word-by-word) | ||
+ | * a character at a time (character-by-character) | ||
+ | * The program must also reads the following file, where each row is structured and composed of a string (a name), an integer number and a real number: | ||
+ | <file txt file2.txt> | ||
+ | Jules 122333 23.5 | ||
+ | Laia 122334 29.5 | ||
+ | </ | ||
+ | * The content read from the two files must be printed on the screen (in the case the file is read character-by-character, | ||
+ | |||
+ | **Video solution: | ||
+ | Video with the explanation of the solution: | ||
+ | |||
+ | < | ||
+ | <iframe width=" | ||
+ | </ | ||
+ | |||
+ | **Soluzion: | ||
+ | <file C read_file_1.c> | ||
+ | /* Example on how to read a file row-by-row, word-by-word, | ||
+ | a structured file (where field of different type are save in each row of the file). */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LEN 50 | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | FILE *fp; | ||
+ | |||
+ | char row[LEN+1]; /* To read strings of maximum length of 50 characters, | ||
+ | an array of characters of dimension 51 must be declared, | ||
+ | because after the string the termination character ' | ||
+ | char ch; | ||
+ | int intValue; | ||
+ | float floatValue; | ||
+ | |||
+ | /* Reading the content of the file file.txt */ | ||
+ | /* Opening the file */ | ||
+ | printf(" | ||
+ | fp = fopen(" | ||
+ | |||
+ | /* Control if the file has been opened without errors */ | ||
+ | if (fp == NULL){ | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* Reading the file row-by-row */ | ||
+ | printf(" | ||
+ | while ( fgets(row, LEN, fp) != NULL ) { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | /* Not recommended */ | ||
+ | /* | ||
+ | rewind(fp); | ||
+ | |||
+ | printf(" | ||
+ | fgets(row, LEN, fp); | ||
+ | while ( !feof(fp) ) { | ||
+ | printf(" | ||
+ | fgets(row, LEN, fp); | ||
+ | } | ||
+ | */ | ||
+ | |||
+ | rewind(fp); | ||
+ | |||
+ | /* Reading the file word-by-word */ | ||
+ | printf(" | ||
+ | while ( fscanf(fp, " | ||
+ | | ||
+ | } | ||
+ | |||
+ | rewind(fp); | ||
+ | |||
+ | /* Reading the file character-by-character */ | ||
+ | printf(" | ||
+ | while ( (ch=getc(fp)) != EOF ) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | fclose(fp); /* Closing the file */ | ||
+ | |||
+ | /* Reading the content of the file file2.txt */ | ||
+ | /* Note: the file contains in each row a string, an integer number and a real number */ | ||
+ | |||
+ | /* Opening the file */ | ||
+ | printf(" | ||
+ | if ( (fp = fopen(" | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* First reading method */ | ||
+ | /* Recommended becase it is more compact */ | ||
+ | printf(" | ||
+ | while ( fscanf(fp, "%s %d %f", row, & | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | rewind(fp); | ||
+ | |||
+ | /* Second reading method */ | ||
+ | printf(" | ||
+ | while ( fgets(row, LEN, fp) != NULL ) { /* E' consigliabile il metodo precedente */ | ||
+ | sscanf(row, "%s %d %f", row, & | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | fclose(fp); /* Closing the file */ | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |