Sign in to follow this  
l jsym l

Input File in C++

Recommended Posts

l jsym l    103
Hey, I'm trying to read in an input file for a program that I am creating and am running into some problems. The way I have it set up right now is working but it could be troublesome in the future.
I was just wondering if there was a way to read in a input file such as:
FFFFFFFF M LLLLLLLLLL
John T Smith

What I want to do is read in these strings separately according to the column widths (i.e FFFFFFF M LLLLLLLLLL) I know I could use a substring for these, however, when I have to use them in the future
I dont want to have something like if ( a == "John ") instead I want if (a == "John") without the extra bits at the end.

I am unsure if I am explaining myself clear enough or not but I tried my best to make it understandable.

Share this post


Link to post
Share on other sites
yewbie    677
[quote name='l jsym l' timestamp='1302720247' post='4798066']
Hey, I'm trying to read in an input file for a program that I am creating and am running into some problems. The way I have it set up right now is working but it could be troublesome in the future.
I was just wondering if there was a way to read in a input file such as:
FFFFFFFF M LLLLLLLLLL
John T Smith

What I want to do is read in these strings separately according to the column widths (i.e FFFFFFF M LLLLLLLLLL) I know I could use a substring for these, however, when I have to use them in the future
I dont want to have something like if ( a == "John ") instead I want if (a == "John") without the extra bits at the end.

I am unsure if I am explaining myself clear enough or not but I tried my best to make it understandable.
[/quote]

If you input the whole line into either a character array or a string you can just loop through each character, and when you hit a space copy everything from after the last space until the space you just reached.
Or with std::string there are easier ways to find spaces and cut things out etc.

or you could use something like strtok [url="http://www.cplusplus.com/reference/clibrary/cstring/strtok/"]strtok[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this