简体   繁体   English

ifstream / ofstream问题与C ++?

[英]ifstream / ofstream issue with c++?

I have been having a very hard time writing to a binary file and reading back. 我一直很难写二进制文件并回读。 I am basically writing records of this format 我基本上是在写这种格式的记录

1234|ABCD|efgh|IJKL|ABC

Before writing this record, I would write the length of this entire record ( using string.size()) and then I write the record to the binary file using ofstream as follows: 在写入此记录之前,我将写入整个记录的长度( using string.size()) ,然后使用ofstream将记录写入二进制文件,如下所示:

int size; 整数大小;

ofstream studentfile;
studentfile.open( filename.c_str(),ios::out|ios::binary );
studentfile.write((char*)&size,sizeof(int));
     studentfile.write(data.c_str(),(data.size()*(sizeof(char))));
     cout << "Added " << data << " to " << filename << endl;
     studentfile.close();

And I read this data at some other place 我在其他地方读取了这些数据

ifstream ifile11;
     int x;
     std::string y;
     ifile11.open("student.db", ios::in |ios::binary);
     ifile11.read((char*)&x,sizeof(int));
     ifile11.read((char*)&y,x);
     cout << "X " << x << " Y " << y << endl;

first I read the length of the record into the variable x, and then read the record into string y. 首先,我将记录的长度读入变量x,然后将记录读入字符串y。 The problem is, the output shows x as being '0' and 'y' is empty. 问题是,输出显示x为'0'并且'y'为空。

I am not able figure this out. 我无法弄清楚。 Someone who can look into this problem and provide some insight will be thanked very much. 非常感谢能够调查此问题并提供一些见解的人。

Thank you 谢谢

You can't read a string that way, as a std::string is really only a pointer and a size member. 您不能以这种方式读取字符串,因为std::string实际上只是一个指针和一个大小成员。 (Try doing std::string s; sizeof(s) , the size will be constant no matter what you set the string to.) (尝试执行std::string s; sizeof(s) ,无论您将字符串设置为什么,大小都将保持不变。)

Instead read it into a temporary buffer, and then convert that buffer into a string: 而是将其读取到临时缓冲区中,然后将该缓冲区转换为字符串:

int length;
ifile11.read(reinterpret_cast<char*>(&length), sizeof(length));

char* temp_buffer = new char[length];
ifile11.read(temp_buffer, length);

std::string str(temp_buffer, length);
delete [] temp_buffer;

I know I am answering my own question, but I strictly feel this information is going to help everyone. 我知道我在回答自己的问题,但我严格认为此信息将为所有人提供帮助。 For most part, Joachim's answer is correct and works. 在大多数情况下,约阿希姆的答案是正确的并且可行。 However, there are two main issues behind my problem : 但是,我的问题背后有两个主要问题:

1. The Dev-C++ compiler was having a hard time reading binary files.

2. Not passing strings properly while writing to the binary file, and also reading from the file. For the reading part, Joachim's answer fixed it all.

The Dev-C++ IDE didn't help me. Dev-C ++ IDE并没有帮助我。 It wrongly read data from the binary file, and it did it without me even making use of a temp_buffer. 它错误地从二进制文件中读取数据,并且甚至在没有使用temp_buffer的情况下也做到了。 Visual C++ 2010 Express has correctly identified this error, and threw run-time exceptions and kept me from being misled. Visual C ++ 2010 Express已正确识别此错误,并抛出了运行时异常,并防止我被误导。 As soon as I took all my code into a new VC++ project, it appropriately provided me with error messages, so that I could fix it all. 一旦我将所有代码带入一个新的VC ++项目中,它就会适当地向我提供错误消息,以便我可以对其进行修复。

So, please do not use Dev-C++ unless you want to run into real troubles like thiis. 因此,请不要使用Dev-C ++,除非您想遇到诸如thiis之类的真正麻烦。 Also, when trying to read strings, Joachim's answer would be the ideal way. 另外,当尝试读取字符串时,Joachim的答案将是理想的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM