reading from a file string and integers

i need to read from a file a string and some integers. in between them are characters. eg. point, [2, 5]
i managed to read a like using getline(afile, input), with input being a string data type.
is there a way i can further breakup the string input to read the integers? or am i wrong in reading it as a string?

what if i used a character array to store the line, how can i cast it to read integers like [49, 12] since each integer is a char?

i'm really bad at reading from a file. some help would be much appreciated
Last edited on
why don't you post your intput file or a part of it.

Here is what I do to parse things like that:

I write functions to perform the following tasks:

bool is_char(char ch) //not just is char, but is it a character that can be represented as text

bool is_number(char ch)

bool is_letter(char ch)

bool is_special(char ch)

string convert_string_to_integer(string s)

int convert_integer_to_string(int x)

so, from there we just get each of the characters from the file. We use the bool statements to recognize what the characters are, and for other conditional stuff. I will not tell you the logic behind it, i want you to figure that out on your own (after all, if I learned this without any guidance whatsoever, so can you. it wasnt really that hard to figure out), so that you understand how it works. I will however, tell you that you should start by listing that characteristics of the definition of a (whatever it is... coordinates?) and then make your conditional statements like so.
lastly, since we are using strings as the parsing mechanism, we need a way to extract the numbers, which is where conv_str and conv_int come in.

for conv_str and conv_int, look up stringstream.

Explanation: Files can be messed with, thats just the way it is. Even if it's encrypted a character or line or the entire thing can be deleted. So, is we use general bools, we do not assume it is always going to be "[number, number]", because there is a chance some idiot will mess it up, and your program will crash trying to read it.
Topic archived. No new replies allowed.