Thread: fgets and breaking out of while loop

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int main()
    {
    	FILE *fp = fopen("test.txt", "r");
    	char line[99];
    	char variable[99];
    	char variable1[99];
    	if (!fp)
    	{
    		fprintf(stderr, "Could not open test.txt\n");
    		return 1;
    	}
    
    	while ((fgets(line, 99, fp)) != NULL)
    	{
    		printf("Line =%s\n", line);
    		switch(line[0])
    		{
    		case 'a':
    			// Skip 'a' lines. 
    			break;
    		case 'b':
    			sscanf(line, "b %s", variable);
    			break;
    		case 'c':
    			sscanf(line, "c %s", variable1);
    			break;
    		default:
    			fprintf(stderr, "Incorrectly formed line...\n");
    			break;
    		}
    	}
    	return 0;
    }
    Does this work?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Actually, switch won't work. Here is why:
    I'm building a nested structure that works in the following way:

    when a line starts with 'a', that starts a new "object" (not in the object-oriented sense).
    then the subsequent 'b' and 'c' lines contain data pertaining to that 'a' object.
    So when we reach the next 'a', the 'b' and 'c' lines will go into this object's structure.

    a 1
    ...b 1
    ...b 2
    ...c 1
    ...c 2
    a 2
    ...b 3
    ...b 4
    ...c 3
    ...c 4

    For this reason, I need to nest. You see what I'm saying?
    Last edited by pollypocket4eva; 01-05-2009 at 06:14 PM.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I see what you are saying, but without understanding more about your actual REAL code, I can't say if there's a simple way of just adding a tiny bit of code to the 'a' case, or if we really need a more complicated method of doing the work. There are ways to use two loops, but you essentially have to still detect that you read an 'a' line in the inner loop and then reuse that for the 'a' case in the outer loop, rather than reading a new line in the outer loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Can I email it to you?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pollypocket4eva View Post
    Can I email it to you?
    No, I don't do private help in that way.

    Are you storing the "Object" in an array or something, or what happens after you've read teh 'b' and 'c' lines? If you are storing in an array, just count up the index in to the array when you get to the 'a' line [start with index = -1].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Ok, here is the real thing.
    Thanks!
    Code:
    		while ((fgets(line, 100, fileinput)) != NULL)
    		{
    			if (line[0] == 'a')
    			{	
    				int Count1a = Count2a;
    				int Count1b = Count2b;
    				
    				while ((fgets(line, 100, fileinput)) != NULL)
    				{
    					if (line[0] == 'b')
    					{
    						sscanf(line, "b %f %f %f", &v1, &v2, &v3);                  
    						Count2a++;
    					} 
    					else if (line[0] == 'c')
    					{
    						sscanf(line, "c %d %d %d", &t1, &t2, &t3);
    						Count2b++;
    					}
    					else
    					{
    						break;
    					}
    				}
    				
    				tab[MainCount].1f = Count1a;
    				tab[MainCount].1l = Count2a;
    				tab[MainCount].2f = Count1b;
    				tab[MainCount].2l = Count2b;
    				
    				MainCount++;
    			}
    		}

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't believe you.
    1f, 1l, 2f, 2l are invalid names for a variable.

    Be that as it may, perhaps this will work:

    Code:
                    int MainCount = -1, 
    		int Count1a;
    		int Count1b;
    ...
    		case 'a':
                            ++MainCount;
    			Count1a = Count2a;
    			Count1b = Count2b;
    			break;
                   case 'b': 
    			sscanf(line, "b %f %f %f", &v1, &v2, &v3);                  
    			Count2a++;
                            break;
    // I'll let you do the rest of the work...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Hey, I just changed the variable names (for reasons of their application)... the form is exactly as I have it. Seriously.

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I am grateful for your help.

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Look, my question started as a "how can you nest line reading..." issue. I didn't realize it would be so far from what I had. Sorry to take your time and also to have possibly offended you by not including my actual variable names.
    I don't think that implementation will work. But thanks

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pollypocket4eva View Post
    Look, my question started as a "how can you nest line reading..." issue. I didn't realize it would be so far from what I had. Sorry to take your time and also to have possibly offended you by not including my actual variable names.
    Right - so generally, it is a bad idea to have nested loops for reading data, for the reason you have just discovered.

    It's not that I want to know whether you call your variabels fred, FirstLine or whatever, but rather that if the code doesn't actually compile I will KNOW that is NOT the code you are running on your system - and when that is the case, it's quite possible that some of the other removals will have some effect on the overall case - like we discovered here with "objects" and the fact that you are counting lines (or something) in the counters, etc, etc.

    Since this is not the first case of "I've removed some essential parts of my code to hide what I'm doing and also therefore lost some important details of how the code should work", I am naturally cautious about believing someone's code that CAN NOT COMPILE. It is hard enough to get to working code without actually trying to figure out [guess] what lines of code are "important but not there".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking out of the loop
    By ICool in forum C Programming
    Replies: 5
    Last Post: 09-24-2007, 08:27 AM
  2. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  3. breaking out of program
    By sworc66 in forum C Programming
    Replies: 4
    Last Post: 11-02-2003, 12:38 AM