Skip to main content
gamedev.net
🔒 Locked

Easy string question

Started by Dookie 8 replies 1k views
Original Post
Dookie
Dookie
Hey guys, it's me again! I'm trying to get a number or string out of a string of characters that follows the colon character (:), and was wondering what's the best way to do that. Here's an example... I have a 'settings.txt' file that contains lines similar to what follows: Size of window: 2 Mouse sensitivity: 300 Client name: Harry I also have a reader that reads in each line, so I end up only having to deal with one of these lines at a time. What I haven't figured out is how to get the value following the colon and space. Would any of you kind folks show me how to do that? Thanks in advance for the help! [smile]
"The crows seemed to be calling his name, thought Caw"
SiCrane
SiCrane
What programming language are you using?
Dookie
Dookie
Oops, guess I shoulda mentioned that... [grin] Sorry!

It's C++.
"The crows seemed to be calling his name, thought Caw"
SiCrane
SiCrane
If you use an ifstream, you can use getline() to read a line up to a delimiter such as ":". After that you can just use operator>> on the stream to read in the integer.
Dookie
Dookie
Thanks for the info, SiCrane. I researched 'getline()' a little but can't really figure out how to make use of it. Here's what I'm currently doing:

ifstream inbal("settings.txt");
inbal >> myString;
// Do something with 'myString' to get the value or string following the colon

The above loads an entire line of text into the string 'myString'. I looked at 'getline()', but can't figure out how to use it to get what's between the colon and the end of myString. Would you show me how to do that?
"The crows seemed to be calling his name, thought Caw"
SiCrane
SiCrane
std::string ignore;  getline(inbal, ignore, ':');int size_of_window;inbal >> size_of_window;getline(inbal, ignore, ':');int mouse_sensitivity;inbal >> mouse_sensitivity;

Dookie
Dookie
Thanks for the example, SiCrane! But I still don't know how to implement your idea, because when I use what you suggested:
inbal.getline(inbal, ignore, ':');
I get this error when I compile my program:

error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,char)' : cannot convert parameter 1 from 'std::ifstream' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Then, if I replace the 'inbal' with 'inText' (which is a simple char array):
inbal.getline(inText, ignore, ':');
I get another error:

error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,char)' : cannot convert parameter 2 from 'std::string' to 'std::streamsize'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Then if I replace the 'ignore' with the number 128:
inbal.getline(inText, 128, ':');
the program finally compiles. But when I go to run the program, it crashes with an 'Unhandled Exception' error while referencing the 'istream' file as where the crash originated. Dang! I Googled for more info, but can't find anything besides examples on how to use 'getline()' to read input from the keyboard (no info on how to use it to read input from a filestream). Hopefully somebody can help me get this to work, because my head's getting sore from banging it against the monitor (I have a hard CRT, not a soft LCD). Ouch!
"The crows seemed to be calling his name, thought Caw"
SiCrane
SiCrane
getline(inbal, ignore, ':');

not

inbal.getline(inbal, ignore, ':');
Dookie
Dookie
OHHHH... Heh. (very small flashlight bulb illuminates over Dookie's head)

That - works - perfectly. Very awesome! Thanks a lot for the help and education, SiCrane. I really appreciate it! [smile]
"The crows seemed to be calling his name, thought Caw"
Zahlman
Zahlman
The free function std::getline reads into a std::string.

The member function getline of std::istreams reads into a char[] (rather, reads into memory starting at a provided char* address).

This is an unfortunate wart on the design of the standard library, but the easiest recommendation is to just always use the free function. (If you have a good reason to use char arrays in the first place, then it probably makes more sense to read into them with .read().)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.