0

Hello,

I've figured out how to transfere a full contents of file into a buffer. Nut now say if i wanted to do:

Surname[20];
Firstname[20];
Age

and its stored in:

user.txt like:

Chilver Jackson 25

How would I get the input stream to stop filling the surname up when it gets to the space and start filling the firstname?

Thanks.

2
Contributors
5
Replies
6
Views
11 Years
Discussion Span
Last Post by Dave Sinkula
0

I believe operator>> is whitespace delimited, so I'd say something like this.

if ( file >> Surname >> Firstname >> Age ) {}

[edit]Or maybe with getline :

if ( file.getline(Surname,   sizeof Surname,   ' ') &&
        file.getline(Firstname, sizeof Firstname, ' ') &&
        file >> Age ) {}
0

I believe operator>> is whitespace delimited, so I'd say something like this.

if ( file >> Surname >> Firstname >> Age ) {}

[edit]Or maybe with getline :

if ( file.getline(Surname,   sizeof Surname,   ' ') &&
        file.getline(Firstname, sizeof Firstname, ' ') &&
        file >> Age ) {}
else if (infile.getline(position, 20,  ' ')  && (infile >> id) && (infile.getline(fname, 20, ' ')))
 {
	cout << position;
	cout << id;
	cout << fname;
 }

doesnt work if i have char then int then char... it prints John 45 wood.

is this possible to do?

0

Yes, if your format differs, you would need to modify the expected input format. Since a space is not a valid part of a number, the input is stopped after the digits of the number (at the space); this immediately satisfies the next getline. One way might be something like this.

if ( file.getline(position, sizeof position, ' ') &&
        file >> id && file.get() &&
        file.getline(fname, sizeof fname, '\n') )
   {
      cout << position;
      cout << id;
      cout << fname;
   }
0

but then if i had a load of numbers from the file like this:


Chilver 25 Jackson 14 25 14 24 (14 25 ... etc should be stored into an array would I just doa for loop so when a white space is hit you increment i? so the next array element is used?

0

I think you need to mention the way that data is formatted in the file. Cherry-picking partial solutions will partially get you there, but it'll take longer.

Yes, leading whitespace is ignored for a sequence of numbers. And you will find issues when you switch between string input and numeric input.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.