I'm pretty much a complete beginner to c++ and was just trying to figure fstream out mostly for fun but i can't seem to find an answer to my question somewhere else on the web.
My question is just as the title sais: is it possible to write and read in the same fstream "session"? I wanted to do a quick program reading through an .txt file and when it encountered a word with a specific first letter it would replace that with another letter. Writing this i come up with more questions but let's start with me showing my code so far:
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
char temp[200];
char test = 's';
fstream file("C:\\lol.txt");
while (!file.eof())
{
file.getline(temp,200);
if (temp[0] == 'b')
file << test;
cout << temp << "\n";
}
file.close();
cin.get();
}
Hopefully you understand what I'm trying to achieve here and yes i understand that the 's' will be placed on the line after and not at all replace the 'b'.
Another problem is that after I've run this program nothing has changed in the .txt file. that's why I'm wondering if i can't read/write in the same session.
So what I'm hoping for is some help if it's possible to make this program to read in lines from a text document, comparing them with set characters/full lenght words and replacing them with something else.
Sorry if I've repeated myself 5k times just trying to be as clear as possible
Hm, if looks like it should be legal...I would think an easier way to do this would be to simply load the text file into an std::vector <std::string>, then do your operations on the vector and then write it back to the file when you are done.