Link to home
Start Free TrialLog in
Avatar of morees
morees

asked on

Read a record from a file.

Hi there,

this is something I did a while back, and can't remember how.

basically this is what should happen.

I have 2 files, one with 3 records in them (values sapce seperated) and a second file with several records in them also space seperated.

I need to read the records from the second file, and then get the times from the first file where the distance are equal. and then check if it is a new record or not.

the first file wil look like this (distance maxtime recordtime).
10 234584 226348
21 432489 423497
42 904679 894566

the second file layout (surname starttime endtime distance)
smith 09:00:00 12:06:18 21
.
.

How can i read in the fiels values into variables, so that I can compare then and do the calculations.
I have to use ifstream.

Please some help will be much apreciated.
Avatar of Hoegje
Hoegje

something like this : (from the top of my head, not tested !!!)

//open filestream
ifstream if;
if.open("file1.txt");

// read all data into program
string currLine;
vector<string> allLines;

while( !EOF ){
  if.getline(currLine);
  allLines.push_back(currLine);
}

// Now all the lines from the first file are in allLines vector.

// eg. split each line based on spaces.
vector<int> disctances;
vector<long> maxtime;
vector<long> recordtime;


// Parse first line, TODO : do thid for all the lines in allLines
currLine = allLines[0];

// extract ditance
string::const_iterator cit = currLine.begin();
while(*cit != ' '){
 string dist;
 dist += *cit;
 cit++;
}

int distI = atoi(dist.c_str());
distances.push_back(distI);

// extract the other data on the line to, and loop until all lines parsed.


// Do all this for second file too, and check the different vectors against each other.
// keep in mind that you can't sort the vectors, because the first element of the distances vector corresponds to the first
// in the other 2 vectors, etc..

This should get you started, as i almost coded it completely

is it a job for strtok() ?


NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *s, const char *delim);

       char *strtok_r(char *s, const char *delim, char **ptrptr);

DESCRIPTION
       A  `token'  is a nonempty string of characters not occurring in the string delim, followed by \0 or by a character occur-
       ring in delim.

       The strtok() function can be used to parse the string s into tokens. The first call to strtok()  should  have  s  as  its
       first  argument.  Subsequent  calls  should  have the first argument set to NULL. Each call returns a pointer to the next
       token, or NULL when no more tokens are found.

       If a token ends with a delimiter, this delimiting character is overwritten with a \0 and a pointer to the next  character
       is saved for the next call to strtok().  The delimiter string delim may be different for each call.

       The  strtok_r()  function  is a reentrant version of the strtok() function, which instead of using its own static buffer,
       requires a pointer to a user allocated char*. This pointer, the ptrptr parameter, must be the same while parsing the same
       string.

Avatar of morees

ASKER

thanks for the replies guys.......but
this is not for me though, it is for a friend who's doing an introduction course to C++.

Nope, I don't think this is a job for strtok().

an example they had in there notes looked like this.

....
ifstream fin;
fin.open("file1.txt");

int value;

while (fin >> value)
{
    //in here they had some calculations.
}
....

Is there a way he can use this type of code to get the field values.

A while loop reading the values like this is not somethig I like but it could work


short count = 0;
int value = 0;
while (fin >> value)
{
  ++count;
  if (count > 3)
    count = 1;
  int distance = 0;
  int maxtime = 0;
  int recordtime = 0;
  switch (count)
 {
  case 1:
  // the value is distance
  distance = value;
  break;
  case 2:
  maxtime = value;
  break;
  case 3:
  recordtime = value;
  }

 // do here what you want with the 3 values
}


AND SO ON y
I assumed windows where int == long (32 bits)
For other os you might consider using long
Avatar of morees

ASKER

thanks,

i think int would be more than enough for what they want to do.
and I don't want to give him a too complex program to work with.

I'll test this over the weekend.

cheers
ASKER CERTIFIED SOLUTION
Avatar of Mafalda
Mafalda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You call it.. or just give me the points :-)
Avatar of morees

ASKER

I did it almost like Mafalda,

I just changed it a bit, something like this

while (fin >> val1 >> val2 >> val3)

so I took out the case statement.

Thanks