5

I'm writing some simple code that's supposed to read every other character, as well as overwriting their adjacent characters with '?'s in a random text file. eg. test.txt contains "Hello World"; after running the program, it'd be "H?l?o?W?r?d"

My code below allows me to read every other character from the text file in the console window, but after the program ends and when I open up test.txt, nothing has been changed. Need help to figure out why...

#include<iostream>
#include<fstream>
using namespace std;

int main()
{

    fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
    while (!data.eof())
    {
        if (!data.eof())
        {
            char ch;
            data.get(ch);
            cout << "ch is now " << ch << endl;

        }


        if (!data.eof())
            data.put('?');

    }
    data.close();
    return 0;
}
Yibo Yang
  • 2,353
  • 4
  • 27
  • 40

2 Answers2

4

You forgot to consider that you have 2 streams, istream and ostream.

You need to synchronize the location of these 2 streams to achieve what you want. I modified your code a bit to show what I mean.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    char ch;
    fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
    while (data.get(ch))
    {                
      cout << "ch is now " << ch << endl;
      data.seekg(data.tellp());   //set ostream to point to the new location that istream set
      data.put('?');
      data.seekp(data.tellg());   //set istream to point to the new location that ostream set
    }
    data.close();  // not required, as it's part of `fstream::~fstream()`
    return 0;  // not required, as 0 is returned by default
}
iammilind
  • 68,093
  • 33
  • 169
  • 336
alvits
  • 6,550
  • 1
  • 28
  • 28
  • 1
    @alvtis: Thank you for your suggestion--indeed I forgot about the two pointers. I ran your code and it does rewrite "Hello World" as "H?l?o?W?r?d". However, I encounter an infinite loop where the istream pointer gets stuck at the character 'd'... I think it has to do with the ending null-terminator – Yibo Yang Oct 07 '14 at 03:27
  • @YiboYang, I have fixed that issue. by replacing `!data.eof()` to `data.get(ch)`. But a small problem still remains, due to which "Hello World" is replaced with "H?l?o?W?r?d?". Look at the last "?" which replaces the NUL character. – iammilind Sep 02 '16 at 06:15
2

You are misusing eof(). Do it like this instead:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
    char ch;

    while (data.get(ch))
    {
        cout << "ch is now " << ch << endl;
        data.put('?');
    }

    data.close();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    That's true, but not the problem he's having. – Ben Voigt Oct 07 '14 at 02:29
  • It's undefined behaviour to read and then write the file like this without seeking, [see here](http://stackoverflow.com/a/15673510/1505939) – M.M Sep 02 '16 at 06:29