我想写一些结构到二进制文件,但它还没有编写。它写道,当我在十六进制查看器中查看它时,文件大小大于0 buf我没有看到结构。它也没有被读过。我知道我正确打开它并且我没有发现问题。
例:
enum CellType
{
WHITE = 0,
BLACK = 1,
EMPTY = 2,
OUT = 3
};
struct Cell {
int nRow;
int nCol;
CellType type;
};
void main() {
cout << "enter file name" << endl;
cin >> fileName;
ofstream fsBoardBinFile;
fsBoardBinFile.open(fileName, ios::binary|ios::out);
Cell s1 = { 1,1,BLACK };
fsBoardBinFile.write(reinterpret_cast<char*>(&s1), sizeof(Cell));
fsBoardBinFile.close();
}
使用int main()
作为Jesper Juhl建议并尝试使用fsBoardBinFile.write(reinterpret_cast<char*>(&s1), sizeof(s));
如果使用std :: string,也可以使用+
运算符代替strcat。由于你没有包含我删除的CellType。对于刚刚:
fsBoardBinFile.write(reinterpret_cast<char*>(&s1), sizeof(s1));
写11(asci:11)而不是数字1(ascii:49)到文件。运用
ifstream fsBoardBinFile1(fileName);
fsBoardBinFile1<<((char*)&s2, sizeof(Cell));
cout << s2.nRow << s2.nCol << endl;
打印11到屏幕所以这似乎工作正常。如果CellType
出现问题,您需要更新代码以包含它的内容。