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.

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 ) {}

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?

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;
   }

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.