Like Tree1Likes
  • 1 Post By AndiPersti

Problem with my function that reads from a text file

This is a discussion on Problem with my function that reads from a text file within the C Programming forums, part of the General Programming Boards category; I have a function that should read the a text file I have and store the information in a structure. ...

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

    Problem with my function that reads from a text file

    I have a function that should read the a text file I have and store the information in a structure.
    The text files are structured in this kind of format
    number of views| bunch of words title | username
    number of views| bunch of words title | username
    number of views| bunch of words title | username
    Where each section is delimited by a '|'.
    Here is the structure.
    Code:
    struct table {
      char *name[MAX];
      char *title[MAX];
      int views[MAX];
      struct table *prev,*next;
    };
    and here is the function
    Code:
     void info(FILE *file, struct table* new){
        int c,flag = 0,value,index=0,j;
        char title[150], user[100];
        while(c!=EOF){
          c = 0;j=0;
          while(c=getc(file)&&c!=EOF && c!='|'){
          value = 10* value + c;}
        new->views[index]=value;
        value =0;
        
        new->title[index]=title;
        while(c=getc(file)&&c!=EOF && c!='|'){
          title[j]=(char)c;j++;
        }
        j=0;
        new->name[index]=user;
        while(c=getc(file)&&c!=EOF && c!='|'&& c!='\n'){
          user[j]=(char)c;j++;
        }
      }
    }
    The problem is that for some reason this goes into an infinite loop and I cannot even fill 1 structure. I am not even sure if the function works correctly.

    Comments, suggestions, and feedback would help immensely. Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would use fscanf(fp, "%[^|] %[^|] etc. ,and take the data right into your struct members,

    This is an example of using sscanf(), where the record members are separated by commas and one space:
    Code:
     sscanf(buff, "%[^,], %[^,], %[^,], %d ", videos[i].title,videos[i].actor,videos[i].genra,&videos[i].rating);
          //printf(" title: %s\n  actors: %s\n   genra: %s\n  rating: %d\n\n",
            // videos[i].title,videos[i].actor,videos[i].genra,videos[i].rating); getchar();
    The print out line is just to confirm that you got what you thought you got, during development. fscanf() has the same data format specifiers, and return type.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I am trying to train myself to use lower level functions such as getc so that I have more control over what's happening.

    The idea is that after having a grasp at lower level functions, I can use the higher level ones such as those you mentioned.

    For some reason when even when
    Code:
    c = '|'
    the first while loop does not terminate. I tried changing the condition to c!=124 which is the int value that represents | but the loop still does not terminate and only terminates when c==EOF I'm guessing.

    Any idea what could be wrong?
    Last edited by workisnotfun; 11-18-2012 at 05:51 PM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by workisnotfun View Post
    The problem is that for some reason this goes into an infinite loop and I cannot even fill 1 structure.
    The assignment operator (=) has lower precedence than the logical and operator (&&). Thus

    Code:
    while(c=getc(file)&&c!=EOF && c!='|')
    is seen by the compiler as (parentheses in red added for clarity)
    Code:
    while ( c = (getc(file) && c != EOF && c != '|'))
    i.e. you assign the result of the whole expression to "c".

    You have to put parentheses around c = getc(file).

    I recommend using more spaces in your code and putting only one statement on each line. This would make your code easier to read.

    Bye, Andreas
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program that reads text from a file and encodes the file by.....
    By kendrakay2000 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2010, 05:06 PM
  2. Two dimensional array that reads file input
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2009, 09:44 PM
  3. Random Reads to file...
    By looza in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 10:51 AM
  4. Replies: 2
    Last Post: 05-07-2006, 10:52 PM
  5. A program which reads the first characters in a file
    By Zewu in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 02:24 PM

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