Home > Blockchain >  why f.seekp(f.tellg()) cannot run?
why f.seekp(f.tellg()) cannot run?

Time:05-10

I want to finish a program which can find a string in a file and then output a char 'T' in the end of line which the string exists . And my code like below(in this example I just find a string "33"):

    std::fstream f("D://test.txt");
    char buff[256];
    while(f.getline(buff,256)){
        if(strstr(buff,"33")!= nullptr){
            std::cout<<buff<<std::endl;
            f.unget();//point to space
            f.unget();//point to end of this line
            f.seekp(f.tellg());
            f<<'T';
            break;
        }
    }

But it run wrong,as the pic show test.txt .What cause this fault? And the origin test.txt just like:

value:32 char: 
value:33 char:!
value:34 char:"
value:35 char:#
value:36 char:$
value:37 char:%
value:38 char:&
value:39 char:'

And the effect I want to get just like:

value:32 char: 
value:33 char:T
value:34 char:"
...

CodePudding user response:

Well! thanks the comment from "Igor Tandetnik". I try mycode in vs 2022, it runs well. Actually I run my code with mingw and clion in the beginning . I think this may be a bug of mingw.

  • Related