Click here to Skip to main content
14,767,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have, for example:
1 height<200.0
1 height< 200.0
int index;
std::string attribute;    //these variable types are set and I cannot change them
char opr;
double value;

I want attribute="height", opr='<' and value=200.0. I later load those values into an unordered_map, but that's not my concern.

My problem is, whenever the operator (in that case '<') adheres the attribute string, those values dont get loaded correctly. I want to somehow separate the attribute string and the opr char.

What I have tried:

I've tried inserting a space (" ") to the attribute string, but then, char would be equal to '2', taken from 200.0.
I've also thought about setting opr to '<', but then the value would simply be 00.0, because I replaced the 2 with the <.
Updated 4-Jan-21 10:26am
Rick York 4-Jan-21 15:12pm
It's not clear what your question really is. Are you asking about reading the file or writing the file?
lelor 4-Jan-21 15:54pm
I am trying to read the file and then load it into an unordered_map. The problem is with reading, sorry if there's a wording problem
Richard MacCutchan 4-Jan-21 15:52pm
the text height<200.0 is a single string and so will be passed into the attribute field. You need to write your own text parser.

1 solution

You probably need to read the input as a single line then parse it youself. You have:
since attribute is of type string, file>>attribute will read up to the next delimiter character, which is normally a char of type space e.g. space, tab, new-line, etc. If your input is
1 height<200.0
2 width<400.0
3 depth<150.0
then, on the first read:
index = 1
attribute = "height<200.0"
opr = '2'
value = 0.0
and the file input pointer will be at the beginning of 'width' on line 2. The next attempt to read will fail to read in anything, since 'w' is not a digit, and cannot be converted to an int.
You can use getline() to read in a single line from the file, then, depending on how complex the input strings might possibly get, break down your input string, perhaps with find_first_of() or find_first_not_of or maybe look into regex classes to help parse the string.
CPallini 5-Jan-21 2:09am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900