Thread: Reading a text file

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by andrew89 View Post
    I'm confused... why make a link to open the same thread? Same URL and everything. I'm very confused.
    Just demonstrating recursion

    The other thread is here.

  2. #32
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Alright, I'm giving this a check, I'll post any updates soon.
    Thanks Jim.

    Dean.

  3. #33
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by jimblumberg View Post
    You need to check your compiler settings and insure that warnings are being emitted, and that they are set to as high as possible. Here are the warnings I got when I compiled your code:


    Also please stop trying to put so much on one line, it makes your code harder to read, troubleshoot, and repair.

    Instead of:
    Code:
    while((fgets(buffer,1024, fp) != NULL)) {sscanf(buffer,"%s %d %s",Tipo,&vertice,&condicao); vertice==vertice1;}
    In my opinion the following is much easier to read:
    Code:
    while((fgets(buffer,1024, fp) != NULL)) 
    {
       sscanf(buffer,"%s %d %s",Tipo,&vertice,&condicao); 
       vertice==vertice1;
    }
    By seperating the code onto several lines then your error/warning messages would be easier to figure out.

    Code:
     
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    void ver()
    {
       char buffer[1024];
       FILE *fp;
       fp = fopen("lol.txt","r");
       char Tipo[10];
       char condicao[20];
       int total_vertice;
       int vertice;
       int vertice1;
       int vertice2;
       int v_origem;
       int v_destino;
       fscanf(fp,"%s %d",Tipo,&total_vertice);
       printf("Neste grafo há um total de %d vertices.\n",total_vertice);
       while((fgets(buffer,1024, fp) != NULL))
       {
          sscanf(buffer,"%s %d %s",Tipo,&vertice,&condicao);
          vertice==vertice1;
       }
       printf("%d",vertice1);
       while((fgets(buffer,1024, fp) != NULL))
       {
          sscanf(buffer,"EDGE %d %d",Tipo,&v_origem,&v_destino);
          v_origem==vertice2;
       }
       if(vertice1==vertice2) {
          printf("O vértice %d tem a condicao '%s' e o seu sucessor é o %d",&vertice1,&condicao,&v_destino);
       }
       else printf("ola \n");
    }
    
    
    
    
    int main ()
    {
       ver();
    }
    The error messages become:


    You do not need the two while() loops, the only way you get out of the loop is if fgets() fails to read any information, which is usually at the end of file. So you should try to read all the information in your file in one loop. Read one line into your buffer, then check to see where you are in your file, does the buffer contain, the word GRAPH, NODE, or EDGE? To test which of these words is in your string, use sscanf() to retrieve one string from the beginning of your string. Then use strcmp() to test the value. Then once you figure out what type of data you are dealing with you will need to read the data into the correct variables.


    Jim
    Jim, let me try to understand what you're saying.

    Using :
    Code:
    while((fgets(buffer,1024, fp) != NULL))
    I'm getting every each line that is different from NULL. I'm guessing in each line I should be doing an sscanf to retrieve one string from the beggining.
    Thing is, doesn't sscanf have to follow a strict modul, like if I'm using :
    Code:
    sscanf(buffer,"%s %d %d",Tipo,&v_origem,&v_destino);
    It will only scan the lines that start by a %s and then gets followed by two %d's, right ?
    So, I'm guessing I should be doing this :
    Code:
    sscanf(buffer,"%s",Tipo);
    And this Tipo would retrieve me either EDGE, NODE or GRAPH, correct ?
    Thanks, Dean.

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Quote Originally Posted by DeanWinchester View Post
    Jim, let me try to understand what you're saying.
    So, I'm guessing I should be doing this :
    Code:
    sscanf(buffer,"%s",Tipo);
    And this Tipo would retrieve me either EDGE, NODE or GRAPH, correct ?
    Thanks, Dean.
    That sounds correct, You may want to try it, after the scanf print out the value.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
     
    void ver()
    {
       char buffer[1024];
       FILE *fp;
       fp = fopen("lol.txt","r");
       char Tipo[10];
       char condicao[20];
       int total_vertice;
       int vertice;
       int vertice1;
       int vertice2;
       int v_origem;
       int v_destino;
       fscanf(fp,"%s %d",Tipo,&total_vertice);
       printf("Neste grafo há um total de %d vertices.\n",total_vertice);
       while((fgets(buffer,1024, fp) != NULL))
       {
          sscanf(buffer,"%s",Tipo);
    
          printf("BUFFER: \n%s\nTipo: %s\n", buffer, Tipo);
       }
    
    .....

  5. #35
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    That worked just perfectly except for the first line, it prints it out twice

    Check it out Jim.

    Code:
    Neste grafo há um total de 18 vertices.BUFFER: 
                                                 
    nTipo: GRAPH
    BUFFER: 
    
    
    nTipo: GRAPH
    BUFFER: 
    NODE 1 if x0 < 0                                                     
    nTipo: NODE
    BUFFER: 
    NODE 2 := x1 -x0                                                     
    nTipo: NODE
    BUFFER: 
    NODE 3 := x2 x1                                                     
    nTipo: NODE
    BUFFER: 
    NODE 4 := y3 y0                                                     
    nTipo: NODE
    BUFFER: 
    NODE 5 if y0 < 0                                                                                                      
    nTipo: NODE
    BUFFER: 
      
    nTipo: NODE
    BUFFER: 
    EDGE 1 2                                                     
    nTipo: EDGE
    BUFFER: 
    EDGE 2 3                                                     
    nTipo: EDGE
    BUFFER: 
    EDGE 3 4                                                     
    nTipo: EDGE
    BUFFER: 
    EDGE 4 2                                                    
    nTipo: EDGE
    BUFFER: 
    EDGE 1 5
    nTipo: EDGE
    But as for the rest, it's looking good.
    Thanks, Dean.

  6. #36
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try getting rid of the fscanf() that is before the loop.

    Jim

  7. #37
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Code:
    BUFFER: 
    GRAPH 18                                             
    nTipo: GRAPH
    BUFFER: 
    
    
    nTipo: GRAPH
    I don't understand why the type is GRAPH when the BUFFER has nothing in it

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your current code.

    Jim

  9. #39
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Code:
    void ver()
    {
       char buffer[1024];
       FILE *fp;
       fp = fopen("lol.txt","r");
       char Tipo[10];
       char condicao[20];
       int total_vertice;
       int vertice;
       int vertice1;
       int vertice2;
       int v_origem;
       int v_destino;
       while((fgets(buffer,1024, fp) != NULL))
       {
          sscanf(buffer,"%s",Tipo);
     
          printf("BUFFER: \n%snTipo: %s\n", buffer, Tipo);
       }
    }
    Dean.

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Changing the printf() line to:
    Code:
    printf("BUFFER: %sTipo: %s\n\n", buffer, Tipo);
    I get the following output. Which seems right to me.
    BUFFER: GRAPH 18
    Tipo: GRAPH

    BUFFER: NODE 1 if x0 < 0
    Tipo: NODE

    BUFFER: NODE 2 := x1 -x0
    Tipo: NODE

    BUFFER: NODE 3 := x2 x1
    Tipo: NODE

    BUFFER: NODE 4 := y3 y0
    Tipo: NODE

    BUFFER: NODE 5 if y0 < 0
    Tipo: NODE

    BUFFER:
    Tipo: NODE

    BUFFER:
    Tipo: NODE

    BUFFER: EDGE 1 2
    Tipo: EDGE

    BUFFER: EDGE 2 3
    Tipo: EDGE

    BUFFER: EDGE 3 4
    Tipo: EDGE

    BUFFER: EDGE 4 12
    Tipo: EDGE

    BUFFER: EDGE 1 5
    Tipo: EDGE

    BUFFER: EDGE 0 0Tipo: EDGE
    Edit: The reason you get:
    BUFFER: NODE 5 if y0 < 0
    Tipo: NODE

    BUFFER:
    Tipo: NODE

    BUFFER:
    Tipo: NODE

    BUFFER: EDGE 1 2
    Tipo: EDGE
    Is because of your blank lines, you will need to check the length of the buffer to insure it is not a blank line before processing the rest of the line.


    Jim
    Last edited by jimblumberg; 12-25-2011 at 12:43 PM.

  11. #41
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Why do you get these two :
    Code:
    BUFFER: 
    Tipo: NODE
    BUFFER: 
    Tipo: NODE
    
    

    Also, how does your .txt look like inside ?

    I'm still getting those two graphs

    Thanks, Dean.

  12. #42
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My input file contains:
    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
    I edited my last post explaining the "blank" lines.

    To fix that I added this if statement:

    Code:
          if(strlen(buffer) > 3)
          {
             sscanf(buffer,"%s",Tipo);
    
             printf("BUFFER: %sTipo: %s\n\n", buffer, Tipo);
          }
    The reason I used 3 is because these "blank" lines have the following characters, 0x20, 0x20,0x0A (space, space, line feed).

    Jim

  13. #43
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Hmm, wasn't aware of this.
    Any blank line has the characters, 0x20, 0x20 , 0x0A ?
    Or only the blank likes on this file.

    I'm gonna have to explain this project to my professor so I gotta know everything that I'm coding

    I wrote it with the strlen(buffer) > 3 and I'm getting it perfect now

    Thanks, Dean.

  14. #44
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The spaces would only be in this file. The line feed is normal. Also be aware that several of your lines have non-printing characters, probably spaces, after the text that you see. I get the following line lengths:
    Buffer: GRAPH 18
    LENGTH: 9
    Buffer: NODE 1 if x0 < 0
    LENGTH: 69
    Buffer: NODE 2 := x1 -x0
    LENGTH: 69
    Buffer: NODE 3 := x2 x1
    LENGTH: 68
    Buffer: NODE 4 := y3 y0
    LENGTH: 68
    Buffer: NODE 5 if y0 < 0
    LENGTH: 118
    Buffer:
    LENGTH: 3
    Buffer:
    LENGTH: 3
    Buffer: EDGE 1 2
    LENGTH: 61
    Buffer: EDGE 2 3
    LENGTH: 61
    Buffer: EDGE 3 4
    LENGTH: 61
    Buffer: EDGE 4 12
    LENGTH: 62
    Buffer: EDGE 1 5
    LENGTH: 9
    Buffer: EDGE 0 0 LENGTH: 8
    Jim

  15. #45
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by jimblumberg View Post
    The spaces would only be in this file. The line feed is normal. Also be aware that several of your lines have non-printing characters, probably spaces, after the text that you see. I get the following line lengths:


    Jim
    Why does every of those blank lines have two spaces ?
    Why don't they have four, five ...
    And why the \n ?

    Just trying to understand why blank likes in which I didn't give any space has two spaces ...
    The \n would be from the ENTER I did on that line ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Reading from a text file
    By chevyman2002 in forum C Programming
    Replies: 10
    Last Post: 03-09-2009, 04: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, 10:38 AM
  5. Reading from a text file.
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-08-2002, 07:02 PM