c++ - getline with delimiter storing an empty character -


i trying input delimited text file getline when debug it shows me there empty character @ beginning of variable.

this happening tid variable happens first on each line. when debug shows character array:

[0] = '' [1] = '2' [2] = '3' [3] = '4'

here relevant code:

ifstream infile("books.txt"); if (!infile){     cout << "file couldn't opened." << endl;     return; } while(!infile.eof()){     string tid, ttitle, tauthor, tpublisher, tyear, tischecked;     getline(infile,tid, ';');     getline(infile,ttitle, ';');     getline(infile,tauthor, ';');     getline(infile,tpublisher, ';');     getline(infile,tyear, ';');     getline(infile,tischecked, ';');     library.addbook(tid, ttitle, tauthor, tpublisher, tyear, (tischecked == "0") ? false : true); } 

here few lines of book.txt:

123;c++ primer plus; steven prata; sams; 1998;0; 234;data structures , algoriths; adam drozdek; course technlogy; 2005;0; 345;the art of public speaking; steven lucas;mcgraw-hill;2009;0; 456;the security risk assessment handbook; douglas j. landall;auerbach;2006;1; 

because ; delimiter getline, doesn't consume newline. add getline call without explicitly specified delimiter, or ignore( numeric_limits<streamsize>::max(), '\n' ) end of loop. has "bonus" of ignoring data after last semicolon.

bonus diagnostic code: http://ideone.com/u9omo


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -