Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with fstream object while handling files in C++.

26 views
Skip to first unread message

bintom

unread,
Oct 3, 2017, 10:22:28 AM10/3/17
to
I have the following C++ program that uses an fstream object to read each character in a text file "MyStory.txt" and replace the lower-case alphabets with upper case.

It wasn't working initially, so I added a tellp() function call to tell me the position of the write pointer. And lo and behold, the program started working fine. But I have no clue at the rationale of this behaviour. Also, is there a better (more sensible) way of getting the program to do its job?


int main()
{ fstream io("MyStory.txt", ios::in | ios::out | ios::ate);
char ch;

io.seekg(0);
io.seekp(0);

while(io)
{ io.get(ch);
if(!io)
break;
if(islower(ch))
{ ch = toupper(ch);
io.seekp(-1, ios::cur);
io.put(ch);
io.tellp();
}
}

io.close();
}

Bo Persson

unread,
Oct 3, 2017, 11:25:24 AM10/3/17
to
When switching between reading and writing (in any direction) you have
to perform a seek. io.tellp() might do that for you.


Bo Persson

0 new messages