将 .txt 文件的内容收集为字符串,C++

发布于 2024-09-30 13:24:40 字数 631 浏览 7 评论 0原文

我目前有一个小程序,它将 .txt 文件的内容重写为字符串。

但是我想将文件的所有内容收集为单个字符串,我该怎么做?

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


using namespace std;


int main () {
    string file_name ; 


    while (1 == 1){
        cout << "Input the directory or name of the file you would like to alter:" << endl;
        cin >>  file_name ;


        ofstream myfile ( file_name.c_str() );
        if (myfile.is_open())
        {
        myfile << "123abc";

        myfile.close();
        }
        else cout << "Unable to open file" << endl;


    }


}

I currently have a little program here that will rewrite the contents of a .txt file as a string.

However I'd like to gather all the contents of the file as a single string, how can I go about this?

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


using namespace std;


int main () {
    string file_name ; 


    while (1 == 1){
        cout << "Input the directory or name of the file you would like to alter:" << endl;
        cin >>  file_name ;


        ofstream myfile ( file_name.c_str() );
        if (myfile.is_open())
        {
        myfile << "123abc";

        myfile.close();
        }
        else cout << "Unable to open file" << endl;


    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暗恋未遂 2024-10-07 13:24:40
#include <sstream>
#include <string>

std::string read_whole_damn_thing(std::istream & is)
{
    std::ostringstream oss;
    oss << is.rdbuf();
    return oss.str();
}
#include <sstream>
#include <string>

std::string read_whole_damn_thing(std::istream & is)
{
    std::ostringstream oss;
    oss << is.rdbuf();
    return oss.str();
}
满栀 2024-10-07 13:24:40

您声明一个字符串和一个缓冲区,然后使用 while not EOF 循环读取文件并将缓冲区添加到字符串。

You declare a string and a buffer and then read the file with a while not EOF loop and add buffer to string.

千笙结 2024-10-07 13:24:40

libstdc++ 的家伙有一个 关于如何做的很好的讨论这与rdbuf

重要的部分是:

std::ifstream in("filename.txt");
std::ofstream out("filename2.txt");

out << in.rdbuf();

我知道,您询问过将内容放入 string 中。您可以通过将 out 设为 std::stringstream 来实现这一点。或者您可以使用 < 将其增量添加到 std::string 中代码>std::getline

std::string outputstring;
std::string buffer;
std::ifstream input("filename.txt");

while (std::getline(input, buffer))
    outputstring += (buffer + '\n');

The libstdc++ guys have a good discussion of how to do this with rdbuf.

The important part is:

std::ifstream in("filename.txt");
std::ofstream out("filename2.txt");

out << in.rdbuf();

I know, you asked about putting the contents into a string. You can do that by making out a std::stringstream. Or you can just add it to a std::string incrementally with std::getline:

std::string outputstring;
std::string buffer;
std::ifstream input("filename.txt");

while (std::getline(input, buffer))
    outputstring += (buffer + '\n');
我一直都在从未离去 2024-10-07 13:24:40
string stringfile, tmp;

ifstream input("sourcefile.txt");

while(!input.eof()) {
    getline(input, tmp);
    stringfile += tmp;
    stringfile += "\n";
}

如果您想逐行执行此操作,只需使用字符串向量即可。

string stringfile, tmp;

ifstream input("sourcefile.txt");

while(!input.eof()) {
    getline(input, tmp);
    stringfile += tmp;
    stringfile += "\n";
}

If you want to do it line by line, just use a vector of strings.

金兰素衣 2024-10-07 13:24:40

您还可以迭代并读取文件,同时将每个字符分配给字符串,直到到达 EOF。

这是一个示例:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char xit;
    char *charPtr = new char();
    string out = "";
    ifstream infile("README.txt");

    if (infile.is_open())
    {
        while (!infile.eof())           
        {
            infile.read(charPtr, sizeof(*charPtr));
            out += *charPtr;
        }
        cout << out.c_str() << endl;
        cin >> xit;
    }
    return 0;
}

You can also iterate and read through the file while assigning each character to a string until the EOF is reached.

Here is a sample:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char xit;
    char *charPtr = new char();
    string out = "";
    ifstream infile("README.txt");

    if (infile.is_open())
    {
        while (!infile.eof())           
        {
            infile.read(charPtr, sizeof(*charPtr));
            out += *charPtr;
        }
        cout << out.c_str() << endl;
        cin >> xit;
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文