使用fstream写 [英] Using fstream write

查看:154
本文介绍了使用fstream写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在指定的文件中查找一行,并将其替换为我的行。我没有访问在我将运行这个机器上的库,所以我创建了一个自定义文件。麻烦似乎是对fstream对象的写调用。我想知道你们中间有没有什么可以帮助。此外,我的getline循环停止到达文件的结尾,我不知道为什么。

I am trying to look for a line in a specified file and replace it with my line. I don’t have access to the library on the machines I’ll be running this on, so I created a custom file. The trouble seems to be the write call to the fstream object. I was wondering if any of you can help. Also, my getline loop stops before reaching the end of the file, and I am not sure why.

#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;
}

Hi.txt

Hi
Today is June 18
I like pizza
I like pepperoni

输出为:

Match
No runtime error: I like deep dish pizza
Hi


推荐答案

myStream.write(temp2.c_str(), 100);
myStream << temp2 << endl;

为什么要把这两个文件写入文件,为什么你说我喜欢深盘披萨是100个字符长?只要单独使用第二行就可以做你想要的。

Why are you writing this to the file twice, and why are you telling it that "I like deep dish pizza" is 100 characters long? Just using the second line alone should do what you want.

我认为循环结束的原因是你在读取文件时写入文件,导致 getline 要混淆。如果文件很小,我只是把整个事情读入一个 stringstream ,替换你想要替换的行,然后写整个 stringstream 导出到文件。

I think the reason the loop is ending is that you're writing the file as you read it, which causes getline to be confused. If the file is small, I would just read the whole thing into a stringstream, replacing the line you want to replace, then write the whole stringstream out to a file. Changing the file in-place is much harder.

示例:

#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'

这篇关于使用fstream写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆