c++ - Can printing cout << '\n' garble text output? -


i have simple program test function i'm writing detects if text file maze valid. allowed characters '0', '1', ' ' (space) , '\n' (newline). however, when execute code sample text file, strange results. third line of following image should read "found illegal character [char] @ [location] in maze [number]" before printing imported maze shown, , matches file.

enter image description here

main.cpp:

#include <iostream> #include <fstream> #include <sstream>  using namespace std;  int main() {     string filename = "mazes/invalid4.txt";     string tempmaze;     int createdmazes = 0;     cout << "\nattempting open " << filename;     fstream thing;     thing.open(filename.c_str());     if(thing.is_open())     {         cout << "\nhurray!";         stringstream mazefromfile;         mazefromfile << thing.rdbuf();         tempmaze = mazefromfile.str();         for(int = 0; < tempmaze.size(); i++)         {             // test make sure characters allowed             if(tempmaze[i] != '1' && tempmaze[i] != '0' && tempmaze[i] != ' ' && tempmaze[i] != '\n')              {                 cout << "\nfound illegal character \"" << tempmaze[i] << "\" @ " << << " in maze " << ++createdmazes << ": \n" << tempmaze;                 return 1;             }         }         cout << " , no illegal characters!\n" << tempmaze << "\nfinished printing maze\n";         return 0;     }      else cout << "\naw...\n";     return 0; } 

is possible text files aren't breaking lines '\n'? what's going on here?

your text file has line endings crlf (\r\n). when output cr, move cursor beginning of line. in essence, you're first writing 'found illegal character \"', moving cursor beginning of line , writing rest on top of it. need handle line breaks differently fix this.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -