Input File in C++

Started by
1 comment, last by Bregma 13 years ago
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.
l jsym l
Advertisement

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.


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 strtok
wut yewbie said, or use std::regex (or boost::regex if you do not yet live in the second decade of the 21st century).

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement