Solved

Read a record from a file.

Posted on 2004-04-30
10
216 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
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Hoegje
Comment Utility
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
Comment Utility

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
Comment Utility
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
 
LVL 6

Expert Comment

by:Mafalda
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

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

Author Comment

by:morees
Comment Utility
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
Comment Utility
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
Comment Utility
You call it.. or just give me the points :-)
0
 

Author Comment

by:morees
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now