string - Parse a file in c++ and ignore some characters -
i'm trying parse file has following information
test_wall ; comments!! je,5 forward goto,1 test_random; je,9
i'm supposed ignore comments after ";" , move on next line. when there comma i'm trying ignore comma , store second value.
string c; int a; c=getchar(); ifstream myfile (filename); if (myfile.is_open()) { while ( c != ';' && c != eof) { c = getchar(); if( c == ',') { a= getchar(); } } } myfile.close(); }
here's code. i'm not entirely sure i've understood problem correctly, if not set on right direction.
ifstream myfile (filename); if (myfile.is_open()) { // read 1 line @ time until eof string line; while (getline(myfile, line)) { // line have semi-colon? size_t pos = line.find(';'); if (pos != string::npos) { // remove semi-colon , afterwards line = line.substr(0, pos); } // line have comma? pos = line.find(','); if (pos != string::npos) { // after comma line = line.substr(pos + 1); // store string ... } } }
i've left section commented 'store string' blank because i'm not want here. possibly asking convert string integer before storing it. if add code, or ask if don't know how that. don't ask, search on stack overflow, because question has been asked hundreds of times.
Comments
Post a Comment