用C++从文件中读取字符串
Reading a string from a file in C++
我试图将字符串直接存储到一个文件中,以便稍后在C++中读取(基本上是为了整个范围,我试图在文件中存储一个带有字符串变量的对象数组,这些字符串变量将通过object[0].string之类的东西读取)。然而,每次我试图读取字符串变量时,系统都会给我一个混乱的错误。以下代码是我尝试的基本部分。
#include <iostream>
#include <fstream>
using namespace std;
/*
//this is run first to create the file and store the string
int main(){
string reed;
reed = "sees";
ofstream ofs("filrsee.txt", ios::out|ios::binary);
ofs.write(reinterpret_cast<char*>(&reed), sizeof(reed));
ofs.close();
}*/
//this is run after that to open the file and read the string
int main(){
string ghhh;
ifstream ifs("filrsee.txt", ios::in|ios::binary);
ifs.read(reinterpret_cast<char*>(&ghhh), sizeof(ghhh));
cout<<ghhh;
ifs.close();
return 0;
}
第二部分是当我试图阅读它时,事情会失控的地方。很抱歉,如果以前有人问过,我已经四处寻找类似的问题,但大多数问题都与我想做的有点不同,或者我真的不明白他们想做什么(对此仍然很陌生)。
我做错了什么
您正在读取一个文件,并试图将数据放入字符串结构本身,覆盖它,这是完全错误的。
由于可以在http://www.cplusplus.com/reference/iostream/istream/read/,您使用的类型是错误的,您知道这一点,因为您必须使用reinterpret_cast
将std::string
强制转换为char *
。
C++提示:在C++中使用reinterpret_cast
(几乎)总是你做错了什么的标志
为什么读取文件如此复杂
很久以前,读取文件很容易。在一些类似Basic的语言中,您使用了函数LOAD
,并且voilà ,你有你的档案。为什么我们现在不能做呢?
因为你不知道文件里有什么
- 它可能是一根绳子
- 它可以是一个序列化的结构数组,其中包含从内存转储的原始数据
- 它甚至可以是一个实时流,也就是说,一个连续附加的文件(日志文件、stdin等等)
- 你可以逐字逐句地阅读数据
- 。。。或者一行一行
- 或者文件太大,无法放入字符串,所以您希望分部分读取
- 等等
更通用的解决方案是使用get函数以字节为单位读取文件(因此,在C++中是一个fstream)(请参阅http://www.cplusplus.com/reference/iostream/istream/get/),并自己进行操作,将其转换为您期望的类型,然后在EOF处停止。
std::isteam
接口具有以不同方式读取文件所需的所有功能(请参阅http://www.cplusplus.com/reference/iostream/istream/),即使这样,std::string
也有一个额外的非成员函数来读取文件,直到找到分隔符(通常为"\n",但它可以是任何东西,请参阅http://www.cplusplus.com/reference/string/getline/)
但我想要std::string
的"加载"函数
好的,我明白了。
我们假设您在文件中放入的是std::string
的内容,但要使其与C样式字符串兼容,即