fstream and number conversions

B

Bill Wayne

Hi,

I'm trying to figure out a way to convert some Dungeons and Dragons
treasure lists into bracketed numbers. For example:

01-18 - - Protection +1
19-28 - - Feather falling

Should be converted to:

[18] Protection +1
[10] Feather Falling

I'm trying to do this by opening up a text file with an fstream
function, then move it to the first hyphen. It should read the number
after it and subtract that from the number before it, then add one. The
fstream will then put it into another text file. Note that the large
dashes after the first number range could be a second number range. All
of ranges in the second column would go in their own list, and ditto
with the third.

The problem I'm having is that I just don't have a good way to do the
proper reading. I've messed around with characters, but that didn't
work. What would be a good way to do this, and/or is there a better
way?

On a related sidenote, how do I get the computer to open the text path
a user types in? I haven't had much luck on this one, either.

Help with either question will be appreciated.

Bill
 
M

Markus Schoder

Bill said:
Hi,

I'm trying to figure out a way to convert some Dungeons and Dragons
treasure lists into bracketed numbers. For example:

01-18 - - Protection +1
19-28 - - Feather falling

Should be converted to:

[18] Protection +1
[10] Feather Falling

I'm trying to do this by opening up a text file with an fstream
function, then move it to the first hyphen. It should read the number
after it and subtract that from the number before it, then add one. The
fstream will then put it into another text file. Note that the large
dashes after the first number range could be a second number range. All
of ranges in the second column would go in their own list, and ditto
with the third.

I would read the file line by line into a string with
std::getline(std::istream &, std::string &).
You then can use the std::string functions for parsing or can use a
package like boost::regexp.
 
D

Daniel T.

"Bill Wayne said:
Hi,

I'm trying to figure out a way to convert some Dungeons and Dragons
treasure lists into bracketed numbers. For example:

01-18 - - Protection +1
19-28 - - Feather falling

Should be converted to:

[18] Protection +1
[10] Feather Falling

I'm trying to do this by opening up a text file with an fstream
function, then move it to the first hyphen. It should read the number
after it and subtract that from the number before it, then add one. The
fstream will then put it into another text file. Note that the large
dashes after the first number range could be a second number range. All
of ranges in the second column would go in their own list, and ditto
with the third.

The problem I'm having is that I just don't have a good way to do the
proper reading. I've messed around with characters, but that didn't
work. What would be a good way to do this, and/or is there a better
way?

On a related sidenote, how do I get the computer to open the text path
a user types in? I haven't had much luck on this one, either.

Help with either question will be appreciated.

Have you tried:

void foo( fstream& file ) {
int a, b;
file >> a;
file.ignore()
file >> b;
// that should give you the two numbers.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top