Solved

Read a record from a file.

Posted on 2004-04-30
10
222 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

856 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