<thread split from here>

Could i have an elaboration on this explanation please?

My poblem being that i have wrote 2 objects to a binary file called "data.dat"

No problem there.

But then i have a function that needs to find the number of objects in the file.I can do this no problem in my main,however i cant seem to get it working with my class function.

My problem is how can my function in the class implementation file have access to the file data.dat that was created in the main.I have tried it the following way but have it does not give the correct answer.

void class::getNumberOfoBJECTS(HOW DO I PASS FILENAME HERE?) {

    // This variable will store the return of tellg(),or size  of the file in bytes.
    int sizeOfFile = 0;
    int sizeOfObject = 0;
    int numOfObjects = 0;

    fstream* file = new fstream("data.dat", fstream::in | fstream::binary);

    // Now i am going to open file for reading and find the
    // size of my file by using seekg for the file pointer
    // and tellg to return the size in bytes.

    // move file pointer to the eof.

    //use tellg() to get the size of the file.
    sizeOfFile = file->tellg();

    cout << "Size of file is " << sizeOfFile << endl;

    sizeOfObject = sizeof(OBJECT);
    numOfObjects = sizeOfFile / sizeOfObject;

    cout << "Number of objects in file is:" << numOfObjects << endl;


Edited by Nick Evan: Added remark

7 Years
Discussion Span
Last Post by Ancient Dragon

>>void class::getNumberOfoBJECTS(HOW DO I PASS FILENAME HERE?)
I guess you were not listening.

void class::getNumberOfoBJECTS(string fileName) {
    fstream* file = new fstream(fileName.c_str(), fstream::in | fstream::binary);

I was listening i guess i should have mentioned i would like to do this my a characeter pointer.

So in my method is would have

void functionName(char* filename);

and in my main


But when i implement my function in my class implementation file and try this

class::functionName("file.dat") {

and try to manipulate the file in here


it does not work!


>>it does not work!
What does that mean??? Are you getting compile errors? Yes, then post the errors and the code that caused the errors.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.