0

I am currently writing a program to read data from a big .csv file and wanted to know if there's any difference between using:

ifstream  handle("filename");

and

ifstream handle;
archivo.open("filename", ios::in);

when opening the file. I have tried both so far and the two have worked in reading the data to then store it in an STL container. I wanted to know if there's any concrete difference in use, efficiency and/or memory use. Thanks in advance!

  • You open a big .csv once. Why do you care of time of opening this file instead of effective multiple reading? – 273K Jun 09 '21 at 05:34
  • Practically (assuming you meant `handle.open(....)` in the second, not `archivo.open(...)`) there is no difference. If either snippet succeeds, the stream is open and may be read in subsequent code, and there is unlikely to be any difference in terms of "efficiency" (however you measure that) or memory usage. The only difference is that, in the second case, it is possible to take additional actions between creating and opening the stream - which doesn't achieve much, since not much can be done that way that can't be achieved by using an appropriate constructor. – Peter Jun 09 '21 at 07:16

1 Answers1

0

There is no differences between them.

ifstream handle("filename"); - you make ifstream type variable and then you open a file in one part of code;

ifstream handle; - you make ifstream type variable;

handle.open("filename", ios::in); - you open file in another part of code.

Second version can be useful when you need to have an ifsteam like a member of a class.

For example:

    class myClass
    {
    public:
        myClass(std::string str)
        {
            file.open(str);
        }
    private:
        ifstream file;
        ...
    };
TARRAKAN
  • 13
  • 1