我想讀取二進制文件並將其存儲到數據庫中,但是當我嘗試將字符串類型存儲到數據庫中時出現分段錯誤。確切的說,推函數內部發生錯誤:從二進制文件中讀取行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_。 – 2012-04-20 05:47:38