User Tools

Site Tools


cs:c_language:file_reading_1
Return to Home page

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, character-by-character and how to read a structured file (where field of different type are save in each row of the file).

Text:
Implements a C program that:

  • Reads the following file:
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:
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, a space must be printed to separate characters)

Video solution:
Video with the explanation of the solution:

Soluzion:

read_file_1.c
/* Example on how to read a file row-by-row, word-by-word, character-by-character and how to read
   a structured file (where field of different type are save in each row of the file). */
 
#include <stdio.h>
#include <stdlib.h>
 
#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 '\0' must be stored */
    char ch;
    int intValue;
    float floatValue;
 
    /* Reading the content of the file file.txt */
    /* Opening the file */
    printf("READING file.txt\n");
    fp = fopen("file.txt", "r");
 
    /* Control if the file has been opened without errors */
    if (fp == NULL){
        printf("Error: impossible to open the file file.txt\n");
        exit(1);
    }
 
    /* Reading the file row-by-row */
    printf("First reading method:\n");
    while ( fgets(row, LEN, fp) != NULL ) {
       printf("READ: %s\n", row);
    }
 
    /* Not recommended */
    /* 
    rewind(fp);
 
    printf("Other reading method:\n");
    fgets(row, LEN, fp);
    while ( !feof(fp) ) {
        printf("READ: %s\n", row);
        fgets(row, LEN, fp);
    }
    */
 
    rewind(fp);
 
    /* Reading the file word-by-word */
    printf("Second reading method:\n");
    while ( fscanf(fp, "%s", row) != EOF ) {
       printf("READ: %s\n", row);
    }
 
    rewind(fp);
 
    /* Reading the file character-by-character */
    printf("Third reading method:\n");
    while ( (ch=getc(fp)) != EOF ) {
       printf("%c ", ch);
    }
 
    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("\nREADING file2.txt\n");
    if ( (fp = fopen("file2.txt", "r")) == NULL){
        printf("Error: impossible to open the file file2.txt\n");
        exit(1);
    }
 
    /* First reading method */
    /* Recommended becase it is more compact */
    printf("First reading method:\n");
    while ( fscanf(fp, "%s %d %f", row, &intValue, &floatValue) != EOF ) {
      printf("READ: %s %d %f\n", row, intValue, floatValue);
    }
 
    rewind(fp);
 
    /* Second reading method */
    printf("Second reading method:\n"); 
    while ( fgets(row, LEN, fp) != NULL ) { /* E' consigliabile il metodo precedente */
      sscanf(row, "%s %d %f", row, &intValue, &floatValue);
      printf("READ: %s %d %f\n", row, intValue, floatValue);
    }
 
    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
/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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy