This one defeats me...
Mar 28, 2018 at 5:56pm UTC
I have been trying to make a program that reads a text file in, then scrambles the text. It works but not on the last line of my text file. Can't figure out why not? Forgive all the couts, I was trying to debug this thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string text;
std::fstream file;
int posStart, posEnd;
file.open("test.txt" );
if (file.is_open()) {
std::cout << "The file is open." << std::endl;
posStart = file.tellp(); //0
int i = 0;
while (getline(file, text)) {
i++;
std::cout << text << "\n" ;
std::cout << text.size() << " size \n" ;
std::cout << i << "\n" ;
posEnd = file.tellp();
for (int temp = 0; temp < text.size(); temp++) {
text[temp] = ~text[temp];
}
file.seekp(posStart);
std::cout << "writing!!!!" << text << std::endl;
file << text;
posStart = posEnd;
}//while
}
else {
std::cout << "Failed." << std::endl;
}
file.close();
std::cin.ignore();
std::cin.get();
return 0;
}
Mar 28, 2018 at 6:21pm UTC
did you press enter after last line in file? e.g. line three is blank
1 First Line
2 Second Line
3
Last edited on Mar 28, 2018 at 6:21pm UTC
Mar 28, 2018 at 7:10pm UTC
Well, no actually.
my test file looks like this...
line one.
line 2.
line 3.
Mar 28, 2018 at 7:15pm UTC
Thanks icy1. That was the easiest solution in the world. Perhaps even a little too easy...
Topic archived. No new replies allowed.