Thread: i have no idea what's wrong

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    i have no idea what's wrong

    I have this section of code. Basically it uses fgets to read from a file, and at the end it does this:

    Code:
                            new->name=name;
    			a[currentarea].items=new;	
    			printf("%d:%s", currentarea, a[currentarea].items->name);
    When I do the test print seen above, everything is fine, each a[currentarea].items->name prints just fine. However, once I leave the while loop and try to print name, it gives me the same name for every a[currentarea], despite the fact that a[currentarea].items is a different pointer, which means its overwriting new->name with whatever the last fgets had. I just don't understand why or how.


    Code:
    	while (1){
    		if (fgets(input, 255, file) == NULL){return;} if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			currentarea=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			strcpy(a[currentarea].name, input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			strcpy(a[currentarea].desc, input);
    			
    
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].n=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].s=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].w=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].e=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].u=atoi(input);
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			a[currentarea].d=atoi(input);
    
    		fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			new=createitem("NULL", "NULL", currentarea);
    			name=strtok(input,",");
    			printf("%s", name);
    			desc=strtok(NULL, " ");
    			new->name=name;
    			new->desc=desc;
    			new->loc=&a[currentarea];	
    			a[currentarea].items=new;	
    			printf("%d:%s", currentarea, a[currentarea].items->name);
    		if (fgets(input, 255, file) == NULL){return;} 
    
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    despite the fact that a[currentarea].items is a different pointer
    That's just being silly. You can see from the loop that they are, in fact, all the same pointer, specifically "name". All your pointers are the same, so they point to the same place in memory, hence they cannot possibly contain different things. (EDIT: I should be more specific: all the items->name pointers are the same.)

    Each object a (whatever that is) needs to obtain its own memory for a name string (using malloc), and you need to use strcpy to move the contents of the string to the right place.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    		new=createitem("NULL", "NULL", currentarea);
    "NULL" is not the same as NULL. Do you mean for the quotes to be there?

    Beyond that, you aren't showing us the definitions of anything (your structure, etc.). Oh, and if you aren't strcpy-ing things you get back from strtok all you are doing is pointing at the static buffer strtok contains.

    Tater should be along shortly to explain returning arrays and the problem you are having in more detail.


    Quzah.
    Last edited by quzah; 07-26-2011 at 04:31 PM. Reason: curses, foiled again
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by quzah View Post
    Oh, and if you aren't strcpy-ing things you get back from strtok all you are doing is pointing at the static buffer strtok contains.
    oh... that might be it actually... thanks! ill have to try and see if i can fix it now.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adam Rinkleff View Post
    oh... that might be it actually... thanks! ill have to try and see if i can fix it now.
    While you're at it... how about you show us the format of that file you're reading and describe what the haitch eee double hockey sticks you're trying to do...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-22-2010, 03:06 AM
  2. Replies: 4
    Last Post: 10-18-2006, 08:30 AM
  3. i have no idea what wrong
    By firefly in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2005, 12:42 AM
  4. Wrong Idea
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-21-2004, 11:52 AM