C programming - Help with opening text file and reading from it

This is a discussion on C programming - Help with opening text file and reading from it within the C Programming forums, part of the General Programming Boards category; Hello! in my code i want to open a text file and then read/print out the content off the file. ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    C programming - Help with opening text file and reading from it

    Hello! in my code i want to open a text file and then read/print out the content off the file.

    this is my code:
    Code:
    int main()
    {
        char s[20];
        FILE * fp;
    
    
        if((fp=fopen("bilar.txt", "r"))==NULL){
        printf("Error! opening file");
           exit(1);
        }
        fscanf(fp,"%s",s);
        printf("%s",s);
    
    
        fclose(fp);
        return 0;
    }


    it prints out 1

    and here is the text file (bilar.txt)that i want the code to open and print out:

    volvo 2003 lastbil
    volgswagen 2009 personbil
    peugot 1969 personbil
    scania 2001 lastbil
    chevrolet 1971 personbil
    ferrari 1959 personbil

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Well you need some kind of loop, such as

    Code:
    int ch;
    while ( (ch=fgetc(fp)) != EOF ) {
    }
    
    while ( fscanf(fp,"%19s",s) == 1 ) {
    }
    
    while ( fgets( s, sizeof(s), fp ) != NULL ) {
    }
    It's really a question of whether you prefer to read the file
    - character at a time
    - word at a time
    - line at a time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    You need to create three variables to read the contents of the file. 2 character arrays and one integer variable, say char model[20], int year, char last[20]
    You will use a loop to read through the file
    Something like this will work
    Code:
      fscanf(fp,"%s%d%s",model,&year,last);
      printf("%s %d %s",model, year, last);
    while ( !feof(fp) ) {
      fscanf(fp,"%s%d%s",model,&year,last);
      printf("%s %d %s",model, year, last);
    }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by bluechip View Post
    You need to create three variables to read the contents of the file. 2 character arrays and one integer variable, say char model[20], int year, char last[20]
    You will use a loop to read through the file
    Something like this will work
    Code:
      fscanf(fp,"%s%d%s",model,&year,last);
      printf("%s %d %s",model, year, last);
    while ( !feof(fp) ) {
      fscanf(fp,"%s%d%s",model,&year,last);
      printf("%s %d %s",model, year, last);
    }
    That's wrong, bluetooth. Note that when the eof is hit you'll have an extra printf call. You presumably meant this:
    Code:
    while (fscanf(fp, "%19s %d %19s", model, &year, last) == 3) {
      printf("%s %d %s", model, year, last);
    }
    Last edited by oogabooga; 10-13-2012 at 01:00 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    Code:
    fscanf(fp,"%s%d%s",model,&year,last);
    
    while ( !feof(fp) ) {
    
      printf("%s %d %s",model, year, last);
      fscanf(fp,"%s%d%s",model,&year,last);
    
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    @AndiPersti: Thanks for that! I learnt that way of controlling loops from the book 'C: How To Program Deitel'. I didn't know it was wrong!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening, Reading and Updating a text file.
    By bonett09 in forum C Programming
    Replies: 8
    Last Post: 12-25-2011, 05:03 AM
  2. Opening Text File
    By panda3 in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2011, 10:43 AM
  3. Opening and reading text files
    By pete212 in forum C Programming
    Replies: 3
    Last Post: 04-22-2008, 10:16 AM
  4. Opening text file
    By zdude in forum C Programming
    Replies: 3
    Last Post: 10-31-2002, 07:23 AM
  5. Opening a text file in C
    By lotus in forum C Programming
    Replies: 17
    Last Post: 05-27-2002, 12:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21