微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++ 我试图流式传输文件,并替换流式传输的每一行的第一个字母它似乎没有按预期工作

如何解决C++ 我试图流式传输文件,并替换流式传输的每一行的第一个字母它似乎没有按预期工作

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>

void add1(std::fstream& files)
{

    char c;
    int i=0;
    int j=0;
    int k=0;
    int con=0;
    string word;

    while(files.get(c)&&!files.eof())
    {
        i++;
        j++;
        if(c=='\n'||(con>=1&&isspace(c)))
        {
            con++;
            if(con>=2)
            {
                break;
            }
            else
            {
                cout<<j<<"\/"<<i<<endl;
                files.seekp(i-j,files.beg);
                files.write("h",1);
                files.seekg(i);

*seekg 结束了我尝试 fstream::clear 的循环。我认为如果 seekg 有效,它会很完美。

+ 没有 seekg 它可以工作,但仅适用于 3 行然后关闭

                j=0;
                word="";
            }

        }
        else
        {
            con=0;
            word=word+c;

        }
    }
}

*目标是能够流式传输文件,并在流式传输时替换文件中每一行的第一个字母。*


解决方法

你似乎有一个逻辑错误,使思考过于复杂。

我不知道你想用你的变量“word”做什么。它无处被消耗。所以,我会忽略它。

然后你正在玩读写指针。那是没有必要的。你只需要操作写指针。

然后,您想“流式传输”某些内容。这我不完全明白。也许这意味着,即使您不替换任何内容,您也希望始终向流中写入一些内容。如果您有 2 个流,这在我的理解中才有意义。但在那种情况下,它会非常简单,不需要进一步思考。

如果我们使用相同的流并且不想替换一个字符,那么它已经存在,存在,并且可能不会再次被相同的字符覆盖。

所以,如果没有什么可以替换的,那么我们就什么都不写。 . .

另外,这很重要,我们不做替换操作,如果我们有一个空行,因为没有什么可以替换的。现在有一个空行中的第一个字符。

而且,最重要的是,我们不能在同一个 fstream 中添加字符。在这种情况下,我们必须将文件的其余部分向右移动一个。所以。 2个流总是更好。那么,这个问题就不会发生了。

那么,逻辑是什么。


算法:

我们总是看以前读过的字符。如果那是一个 '\n' 而当前字符不是,那么我们现在在一个新行中并且可以替换第一个字符。

仅此而已。


它也会考虑到,如果 '\n' 是用 2 个字符编码的(例如 \r\n)。它永远有效。

而且,它很容易实现。 10 行代码。

请看:

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

constexpr char ReplacementCharacter{ 'h' };

void replaceFirstCharacterOfLine(std::fstream& fileStream) {

    // Here we stor the previously read character. In the beginning,a file always starts
    // with a newline. Therefore we pretend that the last read character is a newline
    char previouslyReadCharacter{'\n'};

    // Here we store the current read character
    char currentCharacter{};

    // Get characters from file as lon as there are characters,so,until eof
    while (fileStream.get(currentCharacter)) {

        // No check,if a new line has started. We ignore empty lines!
        if ((previouslyReadCharacter == '\n') && (currentCharacter != '\n')) {

            // So last charcter was a newline and this is different. So,we are in a new,none empty line
            // Set replacement character
            currentCharacter = ReplacementCharacter;

            // Go one back with the write pointer
            fileStream.seekp(-1,std::ios_base::cur);
            // Write (an with taht increment file pointer again)
            fileStream.put(currentCharacter);
            // Write to file
            fileStream.flush();
        }
        else {
            // Do not replace the first charcater. So nothing to be done here
        }
        // Now,set the previouslyReadCharacter to the just read currentCharacter
        previouslyReadCharacter = currentCharacter;
    }
}

int main() {
    const std::string filename{"r:\\replace.txt"};

    // Open file
    std::fstream fileStream{ filename };

    // Check,if file could be opened
    if (fileStream)
        replaceFirstCharacterOfLine(fileStream);
    else
        std::cerr << "\n\n*** Error: Could not open file '" << filename << "'\n\n";
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。