バイナリファイルを読み込んでデータベースに格納しようとしていますが、文字列型をデータベースに格納しようとするとセグメント化エラーが発生します。正確には、エラーがプッシュ機能の内部で発生します。バイナリファイルから行を読むC++
new_node->name = name;
私は、ウェブ上での良い解決策を見つけるように見えることはできません、と私はあてもなく違うことをしようとしている...任意の助けいただければ幸いです。
//
// 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++;
}
ちょうどニックピンクになりますが、バイナリファイルに_lines_はありません。 –