Reading in Data using fstream

I am trying to write a C++ program that takes a text based file, reads the stream and produces a bunch of classes and structs out of it that I can later use for analysis.

My current plan is to use ifstream::read to read the file into an array of characters. I'll then have a member function of my class WC (for world cup, its a bunch of soccer data) that takes an array of characters and returns a pointer to a WC. This function will order and groups the data into the necessary structures and classes (the most primitive of which is a WC. Using a WC I can access whatever data I want ).

The problem with this is that I have to "collect" the data so to speak in main(), always, because as far as I know I can't make calls to ifstream::read in a header file (or other function calls, so to speak... I can only delcare constants). I want to save main() for my analysis of the data, and instead have my data in separate header files.

Anyone have any good ways I can do this?
i havent mastered C++, but i cant tell you how to do a part of this:

1
2
3
4
5
int var;
fstream filename;
filename.open ("yourfile.txt", ios::in);
filename>> var;
filename.close();


that's all i got. What it does is open a file, get the first line of numbers as a variable. Optionally, you can change int var, to be a string (for this you would have to include it's corresponding header) and it would get the first line of text. if you want more than 1 line of text, im trying to find that out right now (because im working on a program of me own).
I can't make calls to ifstream::read in a header file (or other function calls, so to speak... I can only delcare constants).
I'm not entirely sure that you and I are using "header" with the same meaning, but there is no such restriction. Any function can open and read files. For example, your main() could look like this:
1
2
3
YourClass o("file.txt");
o.analyze();
o.save();
Then the constructor for YourClass could read the file and initialize the object based on the contents. You could put you analysis code in YourClass::analyze() and finally YourClass::save() would do the obvious.
I can't really imagine how you got the idea that you can only read files in main().

I'm not entirely sure that you and I are using "header" with the same meaning, but there is no such restriction. Any function can open and read files. For example, your main() could look like this:


I'm sorry my phrasing of the question was very poor.

I wanted to do these calls in a header file:
1
2
3
4
5

YourClass o("file.txt");


extern o;


And then these

1
2
3

 void o.analyze; 
//prints data analyssi to command prompt  


in main.

This is really stupid. I think I know why. I another thread someone told me I should store my data in separate files. I thought he meant in separate C++ files. So I asked:

First, how do I access objects I've defined in one source file in another?



STUUPPID!

I meant objects I've CONSTRUCTED, not DEFINED.

a constructor is a function. YOu can't make function calls in header files! Header files are only fore forward declarations. Instead, I should have understood that My data will always be stored in a text file, and when I construct objects using this data in main, space is allocated for it using the constructor. When I'm done analyzing it and printint the resultsi, I use the destructor, and my data stays safe in the original text file. As long as I only ever READ data using ffstream, and not ifstream (or whatever class reads and writes text files) I won't ever mess it up.

This is what they were trying ot tell me in another thread.

One more Quick question: if I have a lot of data, is it better to allocate space on the stack or the heap for this class? Each instance of this class will have text fils worth of data in, and I could have a bunch of them constructed in main for analysis.

Thanks for the help guys! I really like forums because sometimes we get these sort of stupid ideas in your heads about how things are supposed to work. Talking it out helps, especially when I get strange replies from everyone.

Never learn anything in a vacuum!


-Trent




Last edited on
Topic archived. No new replies allowed.