0

I had a task to change every third symbol of a.txt file to symbol "a", and write the result to file b.txt. When I'm running the program b.txt results with a duplicated last symbol. I use f.get(a) command so that it would count spacebars also.

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    int i = 0;
    ifstream f;
    ofstream d;
    char a;
    f.open("a.txt", ios::in);
    d.open("b.txt", ios::out);
    while (!f.eof()) {
        f.get(a);
        i++;
        d << a;
    }
    f.close();
    d.close();
}
Bulka
  • 1
  • 1
    `while (!f.eof()) {` is a bug. Related: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm May 19 '22 at 13:14

0 Answers0