Just demonstrating recursion
The other thread is here.
Just demonstrating recursion
The other thread is here.
Alright, I'm giving this a check, I'll post any updates soon.
Thanks Jim.
Dean.
Jim, let me try to understand what you're saying.
Using :
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.Code:while((fgets(buffer,1024, fp) != NULL))
Thing is, doesn't sscanf have to follow a strict modul, like if I'm using :
It will only scan the lines that start by a %s and then gets followed by two %d's, right ?Code:sscanf(buffer,"%s %d %d",Tipo,&v_origem,&v_destino);
So, I'm guessing I should be doing this :
And this Tipo would retrieve me either EDGE, NODE or GRAPH, correct ?Code:sscanf(buffer,"%s",Tipo);
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); } .....
That worked just perfectly except for the first line, it prints it out twice
Check it out Jim.
But as for the rest, it's looking good.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
Thanks, Dean.
Try getting rid of the fscanf() that is before the loop.
Jim
I don't understand why the type is GRAPH when the BUFFER has nothing in itCode:BUFFER: GRAPH 18 nTipo: GRAPH BUFFER: nTipo: GRAPH
Post your current code.
Jim
Dean.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); } }
Changing the printf() line to:I get the following output. Which seems right to me.Code:printf("BUFFER: %sTipo: %s\n\n", buffer, Tipo);
Edit: The reason you get: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
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.BUFFER: NODE 5 if y0 < 0
Tipo: NODE
BUFFER:
Tipo: NODE
BUFFER:
Tipo: NODE
BUFFER: EDGE 1 2
Tipo: EDGE
Jim
Last edited by jimblumberg; 12-25-2011 at 12:43 PM.
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.
My input file contains:
I edited my last post explaining the "blank" lines.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
To fix that I added this if statement:
The reason I used 3 is because these "blank" lines have the following characters, 0x20, 0x20,0x0A (space, space, line feed).Code:if(strlen(buffer) > 3) { sscanf(buffer,"%s",Tipo); printf("BUFFER: %sTipo: %s\n\n", buffer, Tipo); }
Jim
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.
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:
JimBuffer: 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