Thread: While not working :(

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    While not working :(

    I've been stuck just trying to figure out what's wrong with my while loop and I can't get it...

    I'm opening up a .dat file and I want the while loop to keep looping until it reads a string that says, "End of file" Here's what I have.

    Code:
    fgets (item, 25, inputfile);
    printf ("\n%s", item); 
    
    while (item != "End of data") 
    {
           printf ("\n%s", item); 
          /***** blah, blah, blah....                  ******
           ***** update to read the next item ******/
           fgets (item, 25, inputfile);
    }
    Anyone know where I've gone wrong???

    ~fatty acid

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    while (item != "End of data")
    If you want to compare strings, use the function strcmp.

    ~/

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    i was told to try that too, but i'm not sure what i'm comparing and i get errors when i just type:

    Code:
    while (strcmp(item)!="End of data")
    I also tried:

    Code:
    while (fgets (item, 25, inputfile)!="End of data")
    but that doesn't work as well. I'm just basically trying to express the algorithm "While the item (string) being read does not read 'End of data' keep doing so and so." Would placing each item read into an array of strings help?

    ~fatty acid

    ps - i strictly only know C basics. I'm not too advanced.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    would this work?

    Code:
    while (!strcmp(fgets (item, 25, inputfile)), "End of file")
    {
           printf ("\n%s", item); 
    }


    Quote Originally Posted by fatty acid
    i was told to try that too, but i'm not sure what i'm comparing and i get errors when i just type:

    Code:
    while (strcmp(item)!="End of data")
    Your strcmp isn't correct, it has to be strcmp(string1, string2). In your case it would be strcmp(item, "End of data")
    Last edited by mapunk; 12-03-2005 at 06:17 PM.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by mapunk
    Your strcmp isn't correct, it has to be strcmp(string1, string2). In your case it would be strcmp(item, "End of data")

    would this work?
    Code:
    while (!strcmp(fgets (item, 25, inputfile)), "End of file")
    {
           printf ("\n%s", item); 
    }
    You've got a misplaced parenthesis...
    Code:
    while(!strcmp(fgets(item, 25, inputfile), "End of file"))
    {
           printf ("\n%s", item); 
    }
    The only problem with this is using the return value of fgets() that way. fgets() will return NULL on error or EOF without any read, which would then get passed to strcmp() - and bad things happen from there.

    You can also read up on what strcmp does for return codes, etc.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    then could you just do this:

    Code:
    while(fgets(item, 25, inputfile) != EOF )
    {
           if (!strcmp(item, "End of file"))
           {
                  printf ("\n%s", item); 
           }
    }

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    this could be one of the solution for the problem
    Code:
    #include<stdio.h>
    
    int main()
    {
     	char *p;
     	char temp[80];
     	FILE *fp;
     	
     	if((fp=fopen("test.txt","r"))==NULL)
     	{
    	   printf("Error: File cannot be opened");
    	   getchar();
    	   exit(1);
        }
        else
        {
    	 while(fgets(temp,80,fp)!=NULL)
    	 {
    	      if((p=strstr(temp,"End of File"))!=NULL)
    	      {
    	                   printf("You have reached \"%s\"",p);
    		     getchar();
       		      break;
    	     }
    	}
         }
    } 	
    
    /* test file
    hello sir 
    hoe are you? End of File 
    
    my output
    You have reached "End of File "
    */
    ssharish2005

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Quote Originally Posted by mapunk
    then could you just do this:

    Code:
    while(fgets(item, 25, inputfile) != EOF )
    {
           if (!strcmp(item, "End of file"))
           {
                  printf ("\n%s", item); 
           }
    }
    with a few minor adjustments, that worked! thank you SOOOOO much.

    ~fatty acid

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM