Solved

Read a record from a file.

Posted on 2004-04-30
10
226 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:morees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Hoegje
ID: 10956957
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
0
 
LVL 11

Expert Comment

by:avizit
ID: 10957111

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.

0
 

Author Comment

by:morees
ID: 10957213
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.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mafalda
ID: 10959117
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
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10959147
I assumed windows where int == long (32 bits)
For other os you might consider using long
0
 

Author Comment

by:morees
ID: 10959549
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
0
 
LVL 6

Accepted Solution

by:
Mafalda earned 100 total points
ID: 10960307
I missed the count condition a little ... better do the following:

short count = 1;
int value = 0;
while (fin >> value)
{
  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;
  }

  ++count;
  if (count > 3)
  {
    count = 1;

   // do here what you want with the 3 values
  }
}
0
 
LVL 1

Expert Comment

by:Hoegje
ID: 11198900
You call it.. or just give me the points :-)
0
 

Author Comment

by:morees
ID: 11200483
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question