Thread: Segmentation Fault when printf string from a textfile

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    Question Segmentation Fault when printf string from a textfile

    Hi! I have a struct

    Code:
    struct Human {
       char *name;
       int age;
    };
    I am trying to save and then read from textfile. I am doing this:

    Code:
    struct Human human= {"Carl",30};
    printf("%s", human.name);
    
    
    ... Then I am saving it using
    Code:
    fwrite(&human, sizeof(Human), 1, outfile);
    I am getting a resut as I am expecting and the printf works fine.
    However, If I load from file using similar fread(). Then do the same printf, then I get the segmetation fault! Also This happens surely because of the string, not the integer, integers still gest outputted correctly if I get rid of string output.
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    The problem is that fwrite writes the struct object member-wise, so if it has a pointer member, it writes the value of the pointer, i.e., an address, not what the pointer points to. You could change the pointer to char to be an array of char, but then you would be limited by the array size. If you want to keep it as a pointer, then you would be better off writing to file in a different way, e.g., a text format (or later I would recommend SQLite).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Change your structure to
    Code:
    struct Human {
       char name[30];
       int age;
    };
    Pointers have to be dealt with separately when it comes to reading and writing to files.
    Code:
    struct Human {
       char *name;
       int age;
    };
    
    struct Human human= {"Carl",30};
    
    // save
    size_t len = strlen(human.name);
    fwrite(&len, sizeof(len), 1, outfile);  // the length of the name
    fwrite(human.name, len, 1, outfile); // the name, excluding the \0
    fwrite(&human.age, sizeof(human.age), 1, outfile); // the age
    
    
    // load
    size_t len;
    fread(&len, sizeof(len), 1, infile);  // the length of the name
    human.name = malloc(len+1);  // make space for the chars, and a \0
    fread(human.name, len, 1, infile); // the name, excluding the \0
    human.name[len] = '\0';
    fread(&human.age, sizeof(human.age), 1, infile); // the age
    Serialization - Wikipedia is the rabbit hole to go down if you want more.
    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. segmentation fault on the last printf
    By cooper1200 in forum C Programming
    Replies: 20
    Last Post: 05-12-2019, 05:33 AM
  2. String - Segmentation Fault
    By Raj 89 in forum C Programming
    Replies: 8
    Last Post: 11-27-2012, 04:44 AM
  3. String segmentation fault
    By dojha00 in forum C Programming
    Replies: 7
    Last Post: 08-29-2012, 03:30 AM
  4. commenting out printf causes segmentation fault
    By zahid990170 in forum C Programming
    Replies: 7
    Last Post: 10-04-2011, 08:38 AM
  5. Segmentation fault on printf() [NOOB ALERT]
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-26-2011, 06:44 PM

Tags for this Thread