Well as I see, I have all the data in a FILE stream and I need to extract the data from the stream line-by-line in order build my map. I am unable to see how to work with the stream directly, without storing the digits in a variable.
Well as I see, I have all the data in a FILE stream and I need to extract the data from the stream line-by-line in order build my map. I am unable to see how to work with the stream directly, without storing the digits in a variable.
Sometimes when you have a lot of data, you need to:
1) get some data
2) work with that data
Then loop back to get some more data.
That works really well on projects where you can't possibly hold all the data, at once. I'm not sure your project is that demanding, but it's an option to keep in mind - just in case.
I have this now (k is initialized to 0):
printf() inside the loop prints everything correctly. However, outside of the loop, when I run printf() I get: 2 2 4 2 2 -1 instead of 2 2 4 2 2 1. This is a little weird since printf() seems okay within the loop.Code:for(;;) { fgets(buff, sizeof (buff), netlist); sscanf(buff, "%d %d %d %d %d %d", &numbers[k][0],&numbers[k][1], &numbers[k][2], &numbers[k][3], &numbers[k][4], &numbers[k][5] ); printf("%d %d %d %d %d %d\n", numbers[k][0],numbers[k][1], numbers[k][2], numbers[k][3], numbers[k][4], numbers[k][5]); k++; if(numbers[k][0] == -1) break; }
This may help clean up your input:
The advantage is that no erroneous data is ever put in with the good data, and you won't be trying to read past the end of the file.Code:if((fgets(buff, sizeof(buff), netlist)) == NULL) break;
That will stop your printf() problems, immediately.
I note that in post #13 I gave a more comprehensive example of what Adak demonstrated in post #19. Frankly, it does not make sense to break from within an otherwise infinite loop when you can use the loop condition with improved readability.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay, so I am trying to use the loop condition to exit the while() loop. I use an external for() to check the values of my numbers[ ] [ ] array:
This is the output:Code:while (fgets(buff, sizeof(buff), file_stream) && sscanf(buff, "%d %d %d %d %d %d", &numbers[k][0],&numbers[k][1], &numbers[k][2], &numbers[k][3], &numbers[k][4], &numbers[k][5]) == 6 && numbers[k][0] != -1) { printf("Inside while loop: %d %d %d %d %d %d\n", numbers[k][0],numbers[k][1], numbers[k][2], numbers[k][3], numbers[k][4], numbers[k][5]); k++; } test = k; for(k=0; k<test; k++) { printf("Outside while loop: %d %d %d %d %d %d\n", numbers[k][0],numbers[k][1], numbers[k][2], numbers[k][3], numbers[k][4], numbers[k][5]); }
The values for the while() loop are correct, but when I check them outside, they are incorrect.Code:nside while loop: 1 2 4 2 2 2 Inside while loop: 2 1 4 2 2 3 Inside while loop: 2 2 4 2 2 1 Outside while loop: 1 2 4 2 2 2 Outside while loop: 2 1 4 2 2 2 Outside while loop: 2 2 4 2 2 -1
Post the smallest and simplest compilable program that demonstrates the problem. Post also the contents of the input file used to test.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The contents of the input file is in post #1.
C pastebin - collaborative debugging toolCode:2 4 1 2 4 2 2 2 2 1 4 2 2 3 2 2 4 2 2 1 -1 -1 -1 -1 -1 -1
I noticed this:
It looks like you want:Code:int numbers [4000][5];
Code:int numbers[4000][6];
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That did the trick! Thank you. I should have known that an array should be of size N+1 for N elements.
Eh, no, your arrays had 5 elements when you wanted 6 each. You only need an additional element if you are using a special value as a terminator, e.g., a null character for strings.Originally Posted by user_name_void
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
but the array was from 0...5 which was six elements, and now its 0...6 which is 7 elements.
This declares an array of 2 elements:Originally Posted by user_name_void
Code:int x[2];
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So have all my elements stored in an integer array. I can start populating my gird map. I have the size of the grip map already (1st line of the input txt file). So I am doing something like this to populate it ina for() loop:
Where k has the value from the while() loop in post 21.Code:x1 = numbers[k][0]; y1 = numbers[k][1]; x2 = numbers[k][3]; y2 = numbers[k][4]; map[x1][y1] = 1; map[x2][y2] = 1;
So what I am trying to do is mark all (x,y) co-ordinates in the array map[ ] [ ], so that each occupied pair of (x,y) has a value of 1. The first and the second number, and the fourth and fifth number of every line describes the position of two elements on the grid map. The remaining numbers per line says how they are connected, that is, element has multiple inlets/outlets (max of 4 inlet/outlet).
Ex:
1 2 4 2 2 2
says:
outlet 4 of element (1,2) is connected to inlet 2 of element (2,2)
The second line of the input txt file indicates the number of vertical and horizontal paths surrounding each element. In this way, element (1,2) can connect to element (2,2) and not be restricted to use one single path (think of a highway with multiple lanes).
The point of doing all this is that I have to find (shortest) path from one element to the other. Sort of like an A* algorithm. Is this method of populating/organizing the grid map sufficient to apply the A* algorithm, or is there a more efficient method?