C++ 如何将 std::string 写入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15388041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:20:28  来源:igfitidea点击:

How to write std::string to file?

c++

提问by Slashr

I want to write a std::stringvariable I am accepting from the user to a file. I tried using the write()method and it writes to the file. But when I open the file I see boxes instead of the string.

我想将std::string我从用户接受的变量写入文件。我尝试使用该write()方法并将其写入文件。但是当我打开文件时,我看到的是框而不是字符串。

The string is only a variable length single word. Is std::stringsuitable for this or should I use a character array or something.

该字符串只是一个可变长度的单字。是否std::string适合这个还是应该使用字符数组或东西。

ofstream write;
std::string studentName, roll, studentPassword, filename;


public:

void studentRegister()
{
    cout<<"Enter roll number"<<endl;
    cin>>roll;
    cout<<"Enter your name"<<endl;
    cin>>studentName;
    cout<<"Enter password"<<endl;
    cin>>studentPassword;


    filename = roll + ".txt";
    write.open(filename.c_str(), ios::out | ios::binary);

    write.put(ch);
    write.seekp(3, ios::beg);

    write.write((char *)&studentPassword, sizeof(std::string));
    write.close();`
}

回答by JSQuareD

You're currently writing the binary data in the string-object to your file. This binary data will probably only consist of a pointer to the actual data, and an integer representing the length of the string.

您当前正在将string-object 中的二进制数据写入文件。这个二进制数据可能只包含一个指向实际数据的指针和一个表示字符串长度的整数。

If you want to write to a text file, the best way to do this would probably be with an ofstream, an "out-file-stream". It behaves exactly like std::cout, but the output is written to a file.

如果要写入文本文件,最好的方法可能是使用ofstream“输出文件流”。它的行为与 完全一样std::cout,但输出被写入文件。

The following example reads one string from stdin, and then writes this string to the file output.txt.

下面的示例从 stdin 读取一个字符串,然后将此字符串写入文件output.txt.

#include <fstream>
#include <string>
#include <iostream>

int main()
{
    std::string input;
    std::cin >> input;
    std::ofstream out("output.txt");
    out << input;
    out.close();
    return 0;
}

Note that out.close()isn't strictly neccessary here: the deconstructor of ofstreamcan handle this for us as soon as outgoes out of scope.

请注意,这out.close()在这里并不是绝对必要的:ofstream一旦out超出范围,的解构器就可以为我们处理这个问题。

For more information, see the C++-reference: http://cplusplus.com/reference/fstream/ofstream/ofstream/

有关更多信息,请参阅 C++ 参考:http: //cplusplus.com/reference/fstream/ofstream/ofstream/

Now if you need to write to a file in binary form, you should do this using the actual data in the string. The easiest way to acquire this data would be using string::c_str(). So you could use:

现在,如果您需要以二进制形式写入文件,则应使用字符串中的实际数据执行此操作。获取此数据的最简单方法是使用string::c_str(). 所以你可以使用:

write.write( studentPassword.c_str(), sizeof(char)*studentPassword.size() );

回答by stefan

Assuming you're using a std::ofstreamto write to file, the following snippet will write a std::stringto file in human readable form:

假设您使用 astd::ofstream写入文件,以下代码段将以std::string人类可读的形式写入文件:

std::ofstream file("filename");
std::string my_string = "Hello text in file\n";
file << my_string;

回答by Tolini

remove the ios::binaryfrom your modes in your ofstream and use studentPassword.c_str()instead of (char *)&studentPasswordin your write.write()

ios::binary从您的 ofstream 中的模式中删除并使用studentPassword.c_str()而不是(char *)&studentPassword在您的write.write()