cs:c_language:file_reading_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/file_reading_1?do=diff
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:file_reading_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
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; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/file_reading_1?do=diff
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/file_reading_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1