I have a program that takes multiple files as input. What I’m trying to do is use the same filestream? I keep getting an error when trying to open the stream with the second file. Why is not code not valid and creating an error at compile time? argv[2] is a const char*.
error: no match for call to ‘(std::ifstream) (char*&)’
ifstream fin(argv[1]);
//work with filestream
fin.close();
fin(argv[2]);
//work with filestream
fin.close();
The first line
ifstream fin(argv[1]);is evokingifstream‘s constructor, and the constructor can only be called once per object. Your code is trying to call it a second time. Try using open() instead:As an aside, you may also want to call clear() before you reopen your
ifstream. The reason for this is that if the first open() (or even close()) fails, error bits on theifstreamwill be set, and won’t be cleared by close().