어떻게 바이너리 파일에서 새 문자 배열로 데이터를 올바르게 쓸 수 있습니까? 나는이 질문이 여러 번 여기에서 물어 봤지만 아직도 제대로 할 수 없다는 것을 알고있다.바이너리 파일에서 char 배열로 데이터를 올바르게 읽는 방법
내가 지금까지 ..
struct Computer_Details {
char computer_type[99];
int release_year;
float price;
};
Computer_Details pc_details;
cout << "Enter Computer Type: ";
cin.getline(pc_details.computer_type, 255);
cout << "Enter Computer Release Date: ";
cin >> pc_details.release_year;
cout << "Enter Computer Price: ";
cin >> pc_details.price;
cout << "\n\n";
//Create File
ofstream file;
file.open("PC_Database.data", ios::binary | ios::app);
if (!file) cout << "Couldn't open file\n";
else {
file.write((char*)&pc_details, sizeof(Computer_Details));
file.close();
}
ifstream readFile;
readFile.open("PC_Database.data", ios::binary);
if (!readFile) cout << "Couldn't Open File\n";
else {
readFile.seekg(0, ios::end);
int fileSize = readFile.tellg();
int pcCount = fileSize/sizeof(Computer_Details);
readFile.seekg(0, ios::beg);
Computer_Details *pc_details = new Computer_Details[pcCount];
readFile.read((char*)pc_details, pcCount * sizeof(Computer_Details));
char *buff = new char[299];
for (int i = 0; i < pcCount; i++)
{
//write to buff char
}
readFile.close();
}
['strcpy'] (http://en.cppreference.com/w/cpp/string/byte/strcpy)를 사용할 수 있습니다. –
[C++의 char 배열로 이진 파일 읽기] 가능한 중복 (http://stackoverflow.com/questions/33935567/reading-binary-file-into-char-array-in-c) – didiz
이 대답은 도움이되지 않았습니다. – Andrew