-3

I am making a linked list called docList, to assign a unique tag and a pointer to a ifstream file but it doesn't seem to work unless the file itself is a pointer.

I am parsing a much larger file into separate files to be added into docList and the implementation might to the cause of this too.

class docList {
public:
    int docNumber;
    ofstream*file;
    docList* bottom;

    docList();
    void addDocument( ofstream);
};

There is more but I think this is all relevant code. I'd like to learn from critic as well.

int parseFile() // takes a file and splits into multiple files by paragraphs
{
bool open = true;
int fileNumber = 1;

string fileName;
string fileLine;
ifstream myFile;

cout << "Name of file: ";
cin >> fileName;

myFile.open(fileName);

if( !myFile.is_open() ) // Checks if file is found
{
    cout << "File not found" << endl;
    open = false;
}
else    // File is available 
{
    ofstream fout;

    while( !myFile.eof() )
    {
        getline( myFile, fileLine );    // Get single line from main file

        stringstream sstream;
        string fileIndex;
        string outputFiles;

        sstream <<fileNumber;   // creating index for new files
        sstream >> fileIndex;
        outputFiles = "file" + fileIndex + ".txt";  

        fout.open(outputFiles); // writing to new files

        L1:
        {
            fout << fileLine << '\n';   // Writes paragraph of main file into seperate files
            getline( myFile, fileLine );
            if( myFile.eof() ) goto L2;
        }
        if( fileLine!= "" ) goto L1;

        L2:
            fout << fileLine;

        fileNumber++;

        doc.addDocument( fout );
        fout.close();
    }
}
myFile.close();
return fileNumber;
}
DGjohn
  • 11
  • 4

1 Answers1

0
class docList {
public:
    .....
    ofstream* file;
    void addDocument(ofstream);
};

All C++ stream objects are non-copyable and the global ones like std::cout, std::cerr, etc, non-movable. Hence the usual way to pass them around is by reference &. But since references can only be bounded at point of declaration, and you are binding the stream at a later time, you can only use a pointer or a reference_wrapper


Also note that, the destructor will close the file and free the resource.. So doing something like this will crash:

class docList {
public:
    .....
    ofstream* file;
    void addDocument(ofstream& of);
};

int parseFile(docList& doc){
    ....
    ofstream file;
    ....
    doc.addDocument(file);


}  //< --`file` will be closed and doc will now hold an invalid reference!

And that smells like your code...

int parseFile()
{
    ......
    ofstream fout;

//    while( !myFile.eof() )   /// Bad idea! and also, your code can look better without using labels
    while( getline( myFile, fileLine ) )   //better
    {
        .....
        fout.open(outputFiles);
        ......
        doc.addDocument( fout );
        fout.close();
    }
....
}

Using smart pointers may seem good, but its generally not nice to hold unto a resource longer than necessary

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68