c++ - How to check if a text file only contains certain characters (eg. 's','f','#','\n')(update) -
I am doing a maze program and have read a maze from a file, but I need to see this file ", "F" i ¼ ?? a ???? # a ???? or "\ n" character print error message otherwise? I tried many times, but in fact it was confused. Now I have to solve it I'm trying to use STL, but there are new problems!
List of zero recruitment list (list & lt; char & gt; And myList, const char * mazfile) {ifstream inFile; string line; inFile.open (mazeFile); while (! InFile.eof ()) {getline (infile, lines); for (int i = 0; i & lt ; Lines.length (); i ++) myList.push_back (line [i]);}} bool checkMaze (list & lt; char & gt; and myList) {list & lt; char & gt; :: iterator itr ; (IRR = myList.begin (); IR! = MyList.end (); IR ++) {if (* IR! = 'S' || * IR! = 'F' || * IR! = '#' | | * IR! = '\ N') false return;} back true;} myMaze.fillList (myList, argv [1]); legal valid = myMaze.checkMaze (myList); If (myMaze.isValid (argv [1]) and valid == true) myMaze.printMaze (); Else {cout & lt; & Lt; "Unable to load the maze" & lt; & Lt; Argv [1] & lt; & Lt; "\ N"; Return 0; } But is it still not printed? What's the problem with this?
Since you have the std :: string, you can consider different string search member functions.
Example std :: string str ("s ### X ## f"); Std :: size_t found = str.find_first_not_of ("sf # \ n"); If (found! = Std :: string :: npos) {std :: cout & lt; & Lt; "Is the first non-acceptable character" & lt; & Lt; Str [found]; Std :: cout & lt; & Lt; "At position" & lt; & Lt; & Lt; & Lt; '\ N'; }
Comments
Post a Comment