Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:15:51+00:00 2026-06-02T11:15:51+00:00

I am trying to read a binary file and store it into a database,

  • 0

I am trying to read a binary file and store it into a database, however I get a segmentation fault when I try to store a string type into the database. To be exact, the error occurs inside the push function:

new_node->name = name;

I can’t seem to find a good solution on the web, and I’m aimlessly trying different things… any help would be appreciated.

// 
// loadbin.cpp
//

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

#include "studentsDB.h"

int main( int argc, char* argv[] ) {

    string name;
    string id;
    int numCourses;
    int crn;
    vector<int> crns;

    studentsDB sDB;
    studentsDB::node *students = 0;

    int in = 1;

    if( argc > 1 ) {

        ifstream infile(argv[in], ios::binary );

        while( !infile.eof() ) {
            infile.read( ( char* )(name.c_str()), sizeof( string ) );
            infile.read( ( char* )(id.c_str()), sizeof( string ) );
            infile.read( ( char* ) &numCourses, sizeof( int ) );

            do{
                crns.push_back( crn );
            }
            while( infile.read( ( char* ) &crn, sizeof( int ) ) );

            sDB.push( &students, (string)name, (string)id, numCourses, crns );
        }
        //sDB.printList( students );
    }


    else
        cout << "Not enough argument" << endl;
}

void studentsDB::push( struct node** head_ref, string name, string id,
                       int numCourses, vector<int>crns ) {

    struct node* new_node = ( struct node* ) malloc(sizeof(struct node));
    new_node->name = name;
    //new_node->id   = id;
    new_node->numCourses = numCourses;
    //new_node->crns = crns;
    new_node->next = (*head_ref);    
    (*head_ref)    = new_node;
    size++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:15:52+00:00Added an answer on June 2, 2026 at 11:15 am

    This code is bad:

            infile.read( ( char* )(name.c_str()), sizeof( string ) );
    

    You can’t write to the buffer returned by c_str(), it is not guaranteed to be long enough to hold your result. sizeof(string) has nothing to do with the size the string can hold, by the way. You will need to allocate your own char[] buffer to hold the results of infile.read, then convert to a string afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to read a binary file (for example an executable) into a string,
I'm trying to read data from a binary file and put it into a
I am trying to read a binary file (only 32 bits long) into a
I am trying to read a binary file into an array of structure struct
I'm trying to write code to read a binary file into a buffer, then
Im trying to read some data from a binary file into a buffer allocated
I am trying to read binary from database and write as a file in
I'm trying to read data in from a binary file and then store in
Iam trying to read a binary file to memory and pass the starting address
I am trying to read a binary file from a program that writes a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.