Like Tree7Likes

Reading a text file

This is a discussion on Reading a text file within the C Programming forums, part of the General Programming Boards category; Hey everyone, I'm currently working on a job for university and I'm having a few problems reading a text file. ...

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Reading a text file

    Hey everyone, I'm currently working on a job for university and I'm having a few problems reading a text file.

    What I wanna do is read the whole file and retain a few elements out of it.
    Code:
    void watch()
    {
        FILE *fp;
        fp = fopen("lol.txt","r");
        char Type[10];
        char condition[20];
        int total_vertex;
        int vertex;
        int v_origin;
        int v_destiny;
        fscanf(fp,"%s %d",&Type,&total_vertex);
        printf("In this graph there is a total of %d vertex.\n",total_vertex);
        while(!feof(fp)){fscanf(fp,"%s %d %s",&Type,&vertex,&condition);} 
        while(!feof(fp)){fscanf(fp,"%s %d %d",&Type,&v_origem,&v_destino);}
        if(v_origin==vertex) {printf("Vertex %d has the condition '%s' and his heir is %d",vertex,condicao,v_destiny);}
    }
    lol.txt has in it this :

    Code:
    GRAPH 18                                             
    NODE 1 if x0 < 0                                                     
    NODE 2 := x1 -x0                                                     
    NODE 3 := x2 x1                                                     
    NODE 4 := y3 y0                                                     
    NODE 5 if y0 < 0                                                                                                      
    
    
    EDGE 1 2                                                     
    EDGE 2 3                                                     
    EDGE 3 4                                                     
    EDGE 4 12                                                     
    EDGE 1 5
    It has a little more edges and nodes but it's just so that you have an idea.

    Thing is, first printf works but the rest doesn't.
    Any idea why ?
    Thanks !

  2. #2
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I don't know how that worked the first time, I had to fix a few spelling mistakes in your variables before I got to where you are. The corrected code is:
    Code:
    #include<stdio.h>
    int main()
    {
    
    FILE *fp;
    fp = fopen("lol.txt","r");
    char Type;
    char condition;
    int total_vertex;
    int vertex;
    int v_origin;
    int v_destiny;
    
    fscanf(fp,"%s %d",&Type,&total_vertex);
    printf("In this graph there is a total of %d vertex.\n",total_vertex);
    
    while(!feof(fp)){fscanf(fp,"%s %d %s",&Type,&vertex,&condition);} 
    while(!feof(fp)){fscanf(fp,"%s %d %d",&Type,&v_origin,&v_destiny);}
    if(v_origin==vertex) {
            printf("Vertex %d has the condition %d and his heir is %d",vertex,condition,v_destiny);
            }
    
    return 0;
    }
    Note: I renamed the function and added a return 0 so it would compile, doesn't seem to affect anything.

    Edit: I'll keep working on this, it is not working like OP would like, but it's at the point where it should've been.
    Last edited by andrew89; 12-23-2011 at 06:47 AM. Reason: Additional information

  3. #3
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Ah. I see the problem now. lol.txt didn't have anything that matched. Try using this input and it works.
    Code:
    GRAPH 18
    NODE 1 if x0 < 0                                                     
    NODE 2 := x1 -x0                                                     
    NODE 3 := x2 x1                                                     
    NODE 4 := y3 y0                                                     
    NODE 5 if y0 < 0                                                                                                      
     
     
    EDGE 1 2                                                     
    EDGE 2 3                                                     
    EDGE 3 4                                                     
    EDGE 4 12                                                     
    EDGE 1 5
    EDGE 0 0
    Edit:
    Otherwise, the source and this infile works without a problem.
    Last edited by andrew89; 12-23-2011 at 06:54 AM. Reason: I totally forgot to change lol.txt

  4. #4
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    The input has to be the one I got on the second place.
    I got the main on another place, that is just a void function I would call on main.
    Yeah, I'm Portuguese and I wrote it in English so you could understand, thanks.
    But the text file has to have the text as I wrote it ..

  5. #5
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    As you've sent me, the first printf works but it still doesn't do the rest of the printf's.
    I got it working for the first printf aswell

  6. #6
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by DeanWinchester View Post
    The input has to be the one I got on the second place.
    I got the main on another place, that is just a void function I would call on main.
    Yeah, I'm Portuguese and I wrote it in English so you could understand, thanks.
    But the text file has to have the text as I wrote it ..
    Then it works perfectly and it shouldn't say anything.
    Code:
    fscanf(fp,"%s %d %d",&Type,&v_origin,&v_destiny)
    Code:
    GRAPH 18NODE 1 if x0 < 0                                                     
    NODE 2 := x1 -x0                                                     
    NODE 3 := x2 x1                                                     
    NODE 4 := y3 y0                                                     
    NODE 5 if y0 < 0                                                                                                      
      
      
    EDGE 1 2                                                     
    EDGE 2 3                                                     
    EDGE 3 4                                                     
    EDGE 4 12                                                     
    EDGE 1 5
    Code:
    if(v_origin==vertex) {        printf("Vertex %d has the condition %d and his heir is %d",vertex,condition,v_destiny);
            }

    You're telling it to compare the 2nd and 3rd values in the second paragraph (the EDGE list). Since none of the values match, it wouldn't print any of them. The source works, the input doesn't have any EDGE values that match so it doesn't print anything with that if statement.

  7. #7
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Wow... nevermind, I got confused. I'll take another peek lol.

  8. #8
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I'm not.

    I have this :
    Code:
    while(!feof(fp)){fscanf(fp,"%s %d %s",&Type,&vertex,&condition);} 
    while(!feof(fp)){fscanf(fp,"%s %d %d",&Type,&v_origin,&v_destiny);}
    He's scanning the ones with the type NODE "number" "condition" and he's scanning the ones with the type EDGE "number" "number".
    Then he's comparing the vertex (the number in NODE) with the number in EDGE and if they are the same, the Vertex number has the condition %s and his heir is %d.

    Makes sense, I don't think you're understanding what the program is doing ;o

    Thanks.

  9. #9
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by DeanWinchester View Post
    I'm not.

    I have this :
    Code:
    while(!feof(fp)){fscanf(fp,"%s %d %s",&Type,&vertex,&condition);} 
    while(!feof(fp)){fscanf(fp,"%s %d %d",&Type,&v_origin,&v_destiny);}
    He's scanning the ones with the type NODE "number" "condition" and he's scanning the ones with the type EDGE "number" "number".
    Then he's comparing the vertex (the number in NODE) with the number in EDGE and if they are the same, the Vertex number has the condition %s and his heir is %d.

    Makes sense, I don't think you're understanding what the program is doing ;o

    Thanks.
    Yeah, I noticed where I got confused too lol.

    The only problem that I keep running into is:
    Code:
    warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    Which I am thinking means the first fscanf is being overwritten by the second.

    There is no way you can combine all of this on one line?


    Code:
    Graph 18
    NODE 2 :=x1-x0 EDGE 1 2
    Edit: Then you could read it like:
    Code:
    %s %d %s %s %d %d
    Last edited by andrew89; 12-23-2011 at 07:20 AM. Reason: Formatting issue.

  10. #10
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    No, I can't
    I wish I could but it has to be on the same exact format I sent you ..

  11. #11
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    This is the closest I could get.
    Code:
    Vertex 5 has the condition '69' and his heir is 0
    or
    Code:
    Vertex 1 has the condition '5' and his heir is 0

    Edit:

    That's after changing the %s in your if statement to %d. Otherwise I get a Segmentation fault.
    The only explanation I can find is C doesn't parse spaces very well. I'm rather new to the C programming game myself. The only thing I can suggest is looking into strok() and strok_r() and see if that would help.
    Last edited by andrew89; 12-23-2011 at 08:21 AM. Reason: Additional information

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    In the following snippet:
    Code:
       fscanf(fp,"%s %d",&Type,&total_vertex);
       printf("In this graph there is a total of %d vertex.\n",total_vertex);
       while(!feof(fp)) {
          fscanf(fp,"%s %d %s",&Type,&vertex,&condition);
       }
       while(!feof(fp)) {
          fscanf(fp,"%s %d %d",&Type,&v_origem,&v_destino);
       }
    First you should not be using feof() to control an input loop, this will usually get one extra input. Use your input function to control the loop.

    Second since your file has at least 3 different types of data, GRAPH, NODE, EDGE, you should read one complete line, determine what kind of data this line contains. I would read the entire line with fgets(), then determine what kind of line this is by looking for your GRAPH, NODE, EDGE tags. The following will get one complete line to begin your processing,
    Code:
       char buffer[1024]; // Use a large input buffer to insure you can get the entire line.
       while((fgets( buffer, 1024, inputFile) != NULL)
       {
          // process the buffer.
    GRAPH 18
    NODE 1 if x0 < 0
    NODE 2 := x1 -x0
    NODE 3 := x2 x1
    NODE 4 := y3 y0
    NODE 5 if y0 < 0

    EDGE 1 2
    EDGE 2 3
    EDGE 3 4
    EDGE 4 12
    EDGE 1 5
    Once you have determined what type of data the line contains you can then process each section differently. Your NODE type also seems to have at least two different types of data, data with := and data with "if" statements, that will need to be processed differently.

    Jim

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652

  14. #14
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by rags_to_riches View Post
    I'm confused... why make a link to open the same thread? Same URL and everything. I'm very confused.

  15. #15
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by jimblumberg View Post
    In the following snippet:
    Code:
       fscanf(fp,"%s %d",&Type,&total_vertex);
       printf("In this graph there is a total of %d vertex.\n",total_vertex);
       while(!feof(fp)) {
          fscanf(fp,"%s %d %s",&Type,&vertex,&condition);
       }
       while(!feof(fp)) {
          fscanf(fp,"%s %d %d",&Type,&v_origem,&v_destino);
       }
    First you should not be using feof() to control an input loop, this will usually get one extra input. Use your input function to control the loop.

    Second since your file has at least 3 different types of data, GRAPH, NODE, EDGE, you should read one complete line, determine what kind of data this line contains. I would read the entire line with fgets(), then determine what kind of line this is by looking for your GRAPH, NODE, EDGE tags. The following will get one complete line to begin your processing,
    Code:
       char buffer[1024]; // Use a large input buffer to insure you can get the entire line.
       while((fgets( buffer, 1024, inputFile) != NULL)
       {
          // process the buffer.


    Once you have determined what type of data the line contains you can then process each section differently. Your NODE type also seems to have at least two different types of data, data with := and data with "if" statements, that will need to be processed differently.

    Jim
    Why would ".=" and "if" need to be processed differently since they are both strings ?

    What extra input does feof() get ?

    Thanks Jim,
    Dean.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 03:43 PM
  2. Reading from a text file
    By chevyman2002 in forum C Programming
    Replies: 10
    Last Post: 03-09-2009, 05:40 PM
  3. Reading in a text file.
    By killpoppop in forum C Programming
    Replies: 6
    Last Post: 01-11-2009, 04:38 PM
  4. Reading a text file
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 11:38 AM
  5. Reading from a text file.
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-08-2002, 08:02 PM

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