need help with fgets() and strings

This is a discussion on need help with fgets() and strings within the C Programming forums, part of the General Programming Boards category; im trying to grab the remaining lines "you are in room #0" which i've already set a pointer to with ...

  1. #1
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73

    need help with fgets() and strings

    im trying to grab the remaining lines "you are in room #0" which i've already set a pointer to with char *rest, but it doesn't connect to room[t].description = rest; the pointer to rest seem to disapear after my while loop, can someone help and make it permanent?

    Code:
    5
    
    
    0 you are in room #0 <-- this is where buf is at, the second call of fgets
    1 you are in room #1
    2 you are in room #2
    3 you are in room #3
    4 you are in room #4
    
    can-go east 0 1
    can-go north 1 2
    can-go up 2 3
    cant-go up 3 4
    Code:
        int t = 0;
        while(fgets(buf, 256, levelfile), buf[0] != '\n') {
            char *numptr = NULL;
            char *rest = NULL;
            numptr = strtok(buf, whitespace);
            rooms[t].room_id = atoi(&buf[0]);
    
            if (numptr != NULL) {
                rest = &buf[strlen(numptr)+1];
            }
            rooms[t].description = rest;  //This is where i'm connecting the rooms descr to rest
            printf("%s\n", rooms[t].description);
            t++;
        }
    
                skip_characters(levelfile, whitespace);
                printf("printing room description\n"); //It doesn't print here i only get a \n
                printf("%s\n", rooms[0].description);
                printf("printing room id\n");
                printf("%d\n", rooms[0].room_id);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by NoobieGecko View Post
    im trying to grab the remaining lines "you are in room #0" which i've already set a pointer to with char *rest, but it doesn't connect to room[t].description = rest; the pointer to rest seem to disapear after my while loop, can someone help and make it permanent?
    Code:
        int t = 0;
        while(fgets(buf, 256, levelfile), buf[0] != '\n') {
            char *numptr = NULL;
            char *rest = NULL;
            numptr = strtok(buf, whitespace);
            rooms[t].room_id = atoi(&buf[0]);
    
            if (numptr != NULL) {
                rest = &buf[strlen(numptr)+1];
            }
            rooms[t].description = rest;  //This is where i'm connecting the rooms descr to rest
            printf("%s\n", rooms[t].description);
            t++;
        }
    
                skip_characters(levelfile, whitespace);
                printf("printing room description\n"); //It doesn't print here i only get a \n
                printf("%s\n", rooms[0].description);
                printf("printing room id\n");
                printf("%d\n", rooms[0].room_id);
    Give it wider scope.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Oh i think i see what you mean... it resets it back to null and the pointer dies...,
    edit: nope nevermind, still lost
    Last edited by NoobieGecko; 02-16-2008 at 05:34 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't even see how that can even compile. As Dave points out, once you reach that close-curly-brace that he's highlighted in magenta, you don't even have a variable named rest anymore. Unless you've got an outer variable, also called rest in your code somewhere, that's getting hidden. In that case, you can just take out the inner declaration of rest and then voila -- all the code inside the while loop will refer to the outside rest.

  5. #5
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    thats what i actually did, it turns out that was a problem, which led me to another problem, why can't i store rest in room[t].description is it because rest = &buf[strlen(numptr)+1];, which is essentially a buffer and not a string? and also, fgets return a terminating \n?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is no such thing as a "string" in C, speaking technically. rest is a pointer to a place in memory, which (at least at the moment) contains some characters that you want. I have no idea what room[t].description is defined as. If it's not a char *, then it is probably a char array, which means that there is already a memory spot set aside for those characters. So instead of changing where the array points to, you have to move the characters you have into the memory set aside for them. That's why we have strcpy.

    And if it is a char *, well, then your buffer gets overwritten but your pointers don't move, so everything appears to be set to the most recent street read in.

    And yes, fgets will return a terminating newline character. You can remove by checking to see if your "string" ends in a \n, and if so deleting it (by setting it to \0 instead).

  7. #7
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    oh, its all clear now, okay well room[t].description as it turns out is a char*, so what do i have to do to have my char*'s stay permanent? malloc the strings and point afterwards?

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by NoobieGecko View Post
    thats what i actually did, it turns out that was a problem, which led me to another problem, why can't i store rest in room[t].description is it because rest = &buf[strlen(numptr)+1];, which is essentially a buffer and not a string? and also, fgets return a terminating \n?
    Assuming that room[t].description has memory allocated.
    Code:
        /* If room[t].description is a char[] maybe make it a char * and use this approach*/
        room[t].description = malloc(strlen(rest)+1));
        strcpy(room[t].description, rest);
    For the terminating '\n' a quick dirty approach. Of curse do it before copying.
    Code:
        if(rest[strlen(rest)-1] == '\n') 
            rest[strlen(rest)-1] = 0;
    You see every time you read, the buffer is refreshed.
    Imagine the fact that you store an address IN the buffer in your structure member.
    The string pointed by your pointer, is NOT stored. We have to make a copy, and it seems
    if your pointer is a the correct position that it is very simple to do so.
    I hope this solves your problem.
    Last edited by xuftugulus; 02-16-2008 at 06:13 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should malloc space for room[t].description, and then strcpy from rest to there.

  10. #10
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    okay, sounds like a good idea, i was about to ask u about that, thanks tabs~ i have to shower... brb...(lol)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  2. fgets erroneoulsy returns null string
    By leonv in forum C Programming
    Replies: 3
    Last Post: 12-04-2006, 05:20 AM
  3. fscanf's auto-allocation for strings
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 07-03-2006, 06:47 AM
  4. fgets in while loop
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 05-02-2005, 04:42 AM
  5. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21