小编por*_*ork的帖子

从C++中的文件中读取字符串

我正在尝试将字符串直接存储到一个文件中以便稍后在C++中读取(基本上对于完整的范围我试图在文件中存储带有字符串变量的对象数组,并且这些字符串变量将通过类似对象读取[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;
}
Run Code Online (Sandbox Code Playgroud)

第二部分是当我尝试阅读它时,事情变得混乱.对不起,如果之前有人询问过,我已经看过类似的问题了,但大多数都和我想做的有点不同,或者我真的不明白他们想做什么(还是挺的)这是新的).

c++ string file-io fstream

2
推荐指数
1
解决办法
9389
查看次数

标签 统计

c++ ×1

file-io ×1

fstream ×1

string ×1