?
Solved

Read a record from a file.

Posted on 2004-04-30
10
Medium Priority
?
229 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 300 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 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