   C++ problem with file input
  I have a text file which looks like this:25,10No problem there, since I only have to use integers to get these values.std::ifstream f("testfile");char comma = ',';int firstvalue,secondvalue;f >> firstvalue >> comma >> secondvalue;I want to have the text file in the following manner though:"A string",5For some reason it cannot use strings, I don't know why. I created a std::string and did the same thing.    std::ifstream f("testfile");    std::string s("");    int i=0;char comma = ',';    f >> s >> comma >> i;    std::cout << s << " " << i << std::endl;The value of the 'i' variable and the string is correctly output, but 'i' is output again with its old value. Suppose the file looks like this: "Banana",7 Then the output will become: Banana 7 0I have no idea where the 0 comes from, but it's the old value of 'i'. Why doesn't my code work?   
  You have a few options of varying levels of difficulty. Unlike certain other languages that have real, non-sucky string support you need to put a lot more effort to bend c++ to your will. A common method is to read in the entire line of text and then parse that line into the components you are looking for. so:std::string line;std::getline(f, line);Then you would use the various string member functions (see here, in particular find and substr) and then convert the components into the types you actually want with stringstream (see here for an example of usage). Hope this helps.   ;Quote:Original post by nobodynewsYou have a few options of varying levels of difficulty. Unlike certain other languages that have real, non-sucky string support you need to put a lot more effort to bend c++ to your will. A common method is to read in the entire line of text and then parse that line into the components you are looking for. so:std::string line;std::getline(f, line);Then you would use the various string member functions (see here, in particular find and substr) and then convert the components into the types you actually want with stringstream (see here for an example of usage). Hope this helps.Er, wouldn't using something like Boost.Regex's regex_token_iterator (or heck, a wrapper around pcre if you're a boost hater) be easier?;Quote:Original post by passwordI have a text file which looks like this:25,10No problem there, since I only have to use integers to get these values.std::ifstream f("testfile");char comma = ',';int firstvalue,secondvalue;f >> firstvalue >> comma >> secondvalue;The input statement does not care about the value of the char variable 'comma' before input. If you initialize it to '&', or indeed leave it uninitialized, it would still pick up a comma.Input with istream::operator>> is dependent on the type of the variable you're reading into, not its previous value.Reading into a char skips leading whitespace, then reads one more character into the char.Reading into an int skips leading whitespace, then reads as many characters as are needed to make an integer.Reading into a string skips leading whitespace, then reads as many characters as are needed to make a word; i.e. up until the next whitespace.Quote:Suppose the file looks like this: "Banana",7 Then the output will become: Banana 7 0I have no idea where the 0 comes from, but it's the old value of 'i'. Why doesn't my code work?There are no spaces in the input file, so the entire file is read in as the string. Then there is nothing left to be read into the int, so it retains its old value.To read up until the next instance of a specific character, use the free function std::getline().You should probably be using std::getline() anyway, to grab lines of the file (i.e. reading up until the next instance of the default "specific character" - a newline - hence the name of the function), so that any "extra" stuff at the end of a line (after the data you wanted to read) is not picked up by the next read attempt.   ;  I see, thank you.   