weird truncation

This is a discussion on weird truncation within the C Programming forums, part of the General Programming Boards category; I'm working on a small application that's supposed to store names, adresses etc. So far it doesn't work vey well, ...

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    weird truncation

    I'm working on a small application that's supposed to store names, adresses etc. So far it doesn't work vey well, as there's a weird error in the code. I'm sure it's very simple, but I can't figure it out. No matter what I type, only three letters is returned. Where does the rest go?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *getFromUser(char *fromUser);
    
    int main() {
    
        struct person {
            char firstName[20];
            char lastName[30];
            char adress[30];
            char email[20];
            int phonenumber;
        };
    
        struct person newPerson;
    
        char *input = { 0 };
        char *fromUser = { 0 };
        FILE *inData;
    
        printf("Enter a first name:\t");
    
        input = getFromUser(fromUser);
    
        strcpy(newPerson.firstName, input);
        printf("First name:\t\t%s\n", newPerson.firstName);
        [...]
    
    
    char *getFromUser(char *fromUser) {
    
        char *getInput;
    
        bzero(getInput, sizeof(getInput));
            
        fgets(getInput, sizeof(getInput), stdin);
    
        //getInput[strlen(getInput) -1] = '\0';
        
        return getInput;
    }

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    same mistake which i used to do...
    I suggest you learn more about dynamic memory allocation..

  3. #3
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Originally posted by Salem
    2. the bzero() is wholy unnecessary, and is non-standard anyway (use memset() if you're going to do anything). fgets() will correctly fill the space and append a \0 for you.
    I used bzero because the string was full of the three letters and garbage. How is memset() better/different than bzero()?
    3. The sizeof() on a pointer returns the size of the pointer, not the amount of memory (non in this case) which is being pointed to.
    Yeah, pretty logical when I think about it (and I obviously need to start thinking before coding).

    I corrected my code, but it still doesn't work. Now four letters is returned instead. How come it's four - and not i.e. six?

    vasanth: Yeah, I need to read up on that. Do you know of any good sites/tutorials?

    And thanks for the help so far.

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >I used bzero because the string was full of the three letters and garbage. How is memset() better/different than bzero()?

    Like Salem Said bzero is non standard. memset is a standard function.

    >I corrected my code, but it still doesn't work. Now four letters is returned instead. How come it's four - and not i.e. six?

    You should post the corrected code so we can see what is happening now that you fixed a few things.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Ok, the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *getFromUser(char *fromUser);
    
    int main() {
    
        struct person {
            char firstName[20];
            char lastName[30];
            char adress[30];
            char email[20];
            int phonenumber;
        };
    
        struct person newPerson;
    
        char *input = { 0 };
        char *fromUser = { 0 };
        FILE *inData;
    
        printf("Enter a first name:\t");
    
        input = getFromUser(fromUser);
    
        strcpy(newPerson.firstName, input);
    
        printf("First name:\t\t%s\n", newPerson.firstName);    
    
        printf("Data written to file:\t\'file.txt\'\n");
    
        inData = fopen("file.txt", "wa");
    
        if (inData == NULL) {
            fprintf(stderr, "NULL error\n");
            exit(0);
        }
    
        fwrite(newPerson.firstName, sizeof(input), sizeof(char), inData);
        rewind(inData);
        fclose(inData);
    
        return 0;
    
    }       
    
    char *getFromUser(char *fromUser) {
    
        static char getInput[100];
    
        fgets(getInput, sizeof(getInput), stdin);
        
        return getInput;
    }
    Last edited by kristy; 08-06-2003 at 03:39 PM.

  6. #6
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    All I did was change this:
    Code:
        fwrite(newPerson.firstName, sizeof(input), sizeof(char), inData);
        rewind(inData);
        fclose(inData);
    to this:
    Code:
        fprintf(inData, "%s", newPerson.firstName);
        fclose(inData);
    and now it works for me.

    I always use fprintf to write to files. I have never used any other way.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  7. #7
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Wow, you're right! I never thought the error was there. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Pointer truncation
    By Magos in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2006, 02:26 PM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

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