Thread: Saving each line of a file

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    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.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #18
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    I have this now (k is initialized to 0):

    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;
    		}
    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.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This may help clean up your input:

    Code:
    if((fgets(buff, sizeof(buff), netlist)) == NULL)
      break;
    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.

    That will stop your printf() problems, immediately.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    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:

    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]);
    	}
    This is the output:
    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
    The values for the while() loop are correct, but when I check them outside, they are incorrect.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem. Post also the contents of the input file used to test.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    The contents of the input file is in post #1.
    Code:
    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
    C pastebin - collaborative debugging tool

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I noticed this:
    Code:
    int numbers [4000][5];
    It looks like you want:
    Code:
    int numbers[4000][6];
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    That did the trick! Thank you. I should have known that an array should be of size N+1 for N elements.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by user_name_void
    I should have known that an array should be of 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    but the array was from 0...5 which was six elements, and now its 0...6 which is 7 elements.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by user_name_void
    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:
    Code:
    int x[2];
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    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:

    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;
    Where k has the value from the while() loop in post 21.

    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM