-
working with a .dat file
Hey,
I have a .dat file that i have to read from, and i need to be able to add a whole column in the .dat file together and then divide it by 20 and and print the answer.
does anyone know how I would go about doing that? I'm literally going mad cause I cannot find a way to do it, as i have never worked with .dat files,
i'm not asking for code just an idea of how I have to do it cause it's for my coursework,
thanks xXx
-
Treat the .dat file as a normal (text?) file with whatever is the specified format.
-
sorry i'm still confused on what you mean?
-
Suppose you have a .txt file that you have to read from, and which you need to be able to add a whole column in the .txt file together and then divide it by 20 and and print the answer.
How would you do it?
-
Read up about fstream library. It works with file IO
-
well so far I have done this, it can read the file
Code:
#include <iostream.h>
#include <fstream.h>
#include <assert.h>
void main( int argc, char* argv[] )
{
assert( argc >= 2 ) ;
ifstream accfile( argv[1] ) ;
if ( !accfile )
{
cerr << "File " << argv[1] << " not found\n" ;
exit(1) ;
}
}
Contestants c ;
accfile >> c.name >> c.club >> c.age >> c.500 >> c.1000 >> c.1500 >> c.2000 ;
const int ACC_MAX = 20;
Contestants accs [ACC_MAX];
int filesRead = 0;
do
{
accfile >> accs[filesRead].name
>> accs[filesRead].club
>> accs[filesRead].age
>> accs[filesRead].500
>> accs[filesRead].1000
>> accs[filesRead].1500
>> accs[filesRead].2000 ;
if (accfile)
{
filesRead++ ;
}
} while (accfile) ;
cout << filesRead << "Accounts Read\n" ;
}
and i've have been working on a struct, it isn't complete yet but this is what i have so far
Code:
struct Contestants
{
char name;
char club;
int age;
double 500;
double 1000;
double 1500;
double 2000;
};
void raceWinner ( Contestants c )
{
for ( c.2000 < ACC_MAX;
}
i could be very well barking up the wrong tree, but i am trying everything at the moment.
-
Code:
struct Contestants
{
char name;
char club;
int age;
double 500;
double 1000;
double 1500;
double 2000;
};
That doesn't compile, does it?
-
I havent tried yet, I'm just playing around with what I might need to do to try and get the program to read all the numbers in 500 add them all together and divide it by 20 and print the answer
-
I've been trying to google my problem for like 3 days now, and this forum was my last resort cause I wanted to try and figure it out, but im at the end of my teather at the moment
-
Variable names cannot start with numbers.
-
ok, thanks, ive changed them now
-
So, does your file have multiple columns and you only need one. If so, you don't need the struct at all. Just read the other columns and discard the values, because you don't need them. This task is so basic, that one can hardly imagine you might be required to use a struct.
By the way, code goes into a function.
It's really something as simple as
Code:
while (fileIn >> temp >> rubbish >> important >> trash >> throwaway) {
//add important to an accumulator variable
//increment line counter if you need to know it
}
//divide accumulator value by 20 (or by how many lines there was)
-
ok thanks for the reply,
I knew it would probably be a simple answer, but I''m very new to c++ and I'm not very good at it at the moment,
Thanks again Karla x
-
void main() is evil. Check Salem's avatar.