How can I use fstream .get to get more then one char?
How can I use fstream .get to get more then one char?
Loop it.
Or you could use the getline function.
http://www.cplusplus.com/ref/iostrea...m/getline.html
Or if you are using strings
http://www.cppreference.com/cppstring/getline.html
Take a look at get()
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am using get but it is only leting me get one char at a time
It only lets me compare one char. what I would like to do is compare a string but not sure how to do that.Code:while (fin.get(ch)) { //Image file name if (ch == 'F' ) { fin >> imageName; }
Use getline like Enahs said.
You can also use fin >> like you did with imageName, just read into a string variable. It will read up until the next whitespace, whereas getline will read until the next newline.
I am using
It wont get any data from the text file. Any one know why not?Code:string s; int flt; while (getline(fin , s)) { //Image file name if (s == "F" ) { fin >> imageName; } if (s == "I") { fin >> flt; ImRight = flt; }
What does the file data look like (especially the first few lines)?
ok well the file looks like this
F AnimBall.bmp
L 1
I 93
T 41
O 125
R 0
G 255
B 0
# 14
D 150
W 900
M 87
until I can get more then one char.
second if I use
I get errorCode:while (fin.getline(fin , s))
no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::basic_ifstream<char, std::char_traits<char> >&, std::string&)'
Don't use fin.getline(fin, s). That is wrong (getline is a member if you are using with C style strings, but you are using C++ strings, so you had it correct).
The problem is that getline reads in the entire line. But since F and AnimBall.bmp are on the same line, one call to getline reads them both.
Use fin >> s to read into the string instead of getline.