Read Flat File (This is different I assure you!)

So before I decided to post this I searched the forum to see if it was addressed yet so please atleast give this issue a chance.

I am able to read in a flat file such as:

/*
John 83 52.2
swimming Jefferson
Jane 26 10.09
sprinting San Marin
*/

I have also been able to assign each string into variables flawlessly, i.e.

[c0de]

#include "stdafx.h"
#include <string>
#include <iostream>
#include <string>
#include <sstream>
#include<iomanip>
#include<fstream>


using namespace std ;


void main()
{
std::ifstream inf("My_text_file.txt");

char name[30];
while(!inf.getline(name, 30, ' ').eof())
{
cout << "NAME: " << name << endl;
//Athlete* ap;
char jersey_number[10];
char best_time[10];
char sport[40];
char high_school[40];
inf.getline(jersey_number, 10, ' '); //#read thru pipe
cout << "JERSEY: " << jersey_number << endl;
inf.getline(best_time, 10); //#read thru newline
cout << "BEST TIME: " << best_time << endl;
inf.getline(sport, 40, ' '); //#read thru pipe
cout << "SPORT: " << sport << endl;
inf.getline(high_school, 40); //#read thru newline
cout << "HIGH SCHOOL: " << high_school << endl;
//ap = new Athlete(name, strtod(number), strtof(best_time), sport, high_school);
//do something with ap

}

system("PAUSE");
}

[/c0de]


If you try running this code the output is exactly what I am looking for.
The problem arises when I try to change the spaces between 2 words in the .txt file. i.e.

/*
John 83 52.2
swimming Jefferson
Jane 26 10.09
sprinting San Marin
*/


What must be done to read this changed file and produce the same output? The idea is that the spaces between words can change randomly...

Any help will be greatly appreciated
Well, if you can use strings, it would be easier to use some of their member functions like .find() etc...otherwise you will have either loop through the char arrays and find the extra spaces, then delete them, or just something like the token function (strtok()?) to split them (although it CHANGES your string so make a copy first if you want one).
Thank you for your suggestions, I will attempt the find() and strrok functions using the string method
Topic archived. No new replies allowed.