Thread: Strange problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Strange problem

    Hi,

    Could anyone please explain this problem to me?
    I use:
    Code:
    
    while(!feof(f))
    		  {
    			fgets (all_id, 100 , f);
    			sscanf (all_id,"%*s %s %s",a_id,d_id);
    			str[i] = a_id;
                            printf("\n%s",str[i]);
    			strtok(a_id,"G");
    			ida = strtok (NULL, "G");
    			idsa[i]=atoi(ida);
    			
    			i++;
    			
    		  }
    
    printf("\n%s",str[2]);
    Even if I use strncpy in place of strtok, the value retained by the str is the last value assigned to it using str[i]=a_id!
    Why cannot the str retain its value outside loop?
    I'll be obliged if anyone could explain this fundamental misconception of mine?

    Thanks,
    Angkar

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Precisely I see its a stale value! When I switch back to strtok again, its throwing the value of the last result that I got using strncpy!!!

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Try changing the loop to something other than feof. Why it's bad to use feof() to control a loop It can cause some wierd things about how many times the loop runs. Fix that, it might help.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    What are the alternatives?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What are the alternatives?
    Sticking to one thread and not spamming the board would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM