Oct 13, 2016 at 4:16am UTC
Lets say I want to take every character in a file and make it lower case. I could do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string path = "file.txt" ;
ifstream in{path};
string result;
while (in) {
char ch;
in.get(ch);
ch = tolower(ch);
result += ch;
}
in.close();
ofstream out{path};
if (out){
out << result;
}
out.close();
This works, but I feel like there is an even better solution out there. Can anyone give me some insight?
Last edited on Oct 13, 2016 at 4:19am UTC
Oct 13, 2016 at 5:21am UTC
I was thinking of perhaps a way to immediately change the character after the read without dealing with seekp() or anything like that. That may not even be possible though.
Oct 13, 2016 at 5:35am UTC
Something more like
1 2 3 4 5 6
//...
while (in) {
file.get(ch);
file.somehow_convert_this_char_to_lower(ch);
}
/...
I mean that the conversion takes place as we are reading in the characters.
Last edited on Oct 13, 2016 at 5:36am UTC