【问题标题】:Using fstream write使用 fstream 写入
【发布时间】:2013-06-18 17:20:08
【问题描述】:

我正在尝试在指定文件中查找一行并将其替换为我的行。我无法访问要运行它的机器上的库,所以我创建了一个自定义文件。问题似乎是对 fstream 对象的写入调用。我想知道你们中的任何人是否可以提供帮助。另外,我的 getline 循环在到达文件末尾之前就停止了,我不知道为什么。

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

#define TARGET2 "Hi"

using namespace std;

void changeFile(string fileName){
    fstream myStream;
    myStream.open(fileName.c_str(),fstream::in | fstream::out);     

    string temp;
    string temp2 = "I like deep dish pizza";    

    while(getline(myStream, temp)){
        if(temp == TARGET2){
            cout << "Match" << endl;
            myStream.write(temp2.c_str(), 100);
            myStream << temp2 << endl;
            cout << "No runtime error: " << temp2 << endl;                  
        }
        cout << temp << endl;
    }
    myStream.close();
}

int main (void){        
    changeFile("Hi.txt");
    return 0;
}

你好.txt

Hi
Today is June 18
I like pizza
I like pepperoni

输出是:

Match
No runtime error: I like deep dish pizza
Hi

【问题讨论】:

    标签: c++ fstream


    【解决方案1】:
    myStream.write(temp2.c_str(), 100);
    myStream << temp2 << endl;
    

    你为什么要把这个写入文件两次,你为什么告诉它“我喜欢深盘披萨”有 100 个字符长?仅使用第二行就可以满足您的需求。

    我认为循环结束的原因是您在阅读文件时正在编写文件,这导致getline 感到困惑。如果文件很小,我会将整个内容读入stringstream,替换您要替换的行,然后将整个stringstream 写入文件。就地更改文件要困难得多。

    例子:

    #include <fstream>
    #include <iostream>
    #include <sstream>
    
    int main(int argc, char** argv) {
    
        /* Accept filename, target and replacement string from arguments for a more
           useful example. */
        if (argc != 4) {
            std::cout << argv[0] << " [file] [target string] [replacement string]\n"
                << "    Replaces [target string] with [replacement string] in [file]" << std::endl;
            return 1;
        }
    
        /* Give these arguments more meaningful names. */
        const char* filename = argv[1];
        std::string target(argv[2]);
        std::string replacement(argv[3]);
    
        /* Read the whole file into a stringstream. */
        std::stringstream buffer;
        std::fstream file(filename, std::fstream::in);
        for (std::string line; getline(file, line); ) {
            /* Do the replacement while we read the file. */
            if (line == target) {
                buffer << replacement;
            } else {
                buffer << line;
            }
            buffer << std::endl;
        }
        file.close();
    
        /* Write the whole stringstream back to the file */
        file.open(filename, std::fstream::out);
        file << buffer.str();
        file.close();
    }
    

    运行方式:

    g++ example.cpp -o example
    ./example Hi.txt Hi 'I like deep dish pizza'
    

    【讨论】:

    • 我写了两次,因为第二行似乎不起作用。我将尝试使用 stringstream 方法,看看效果如何。
    • @BabbuMaan 我提供了一个示例,说明如何做到这一点,以防万一它有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多