I invoke my program with the name of a text file, which gets put into argv[2 & 4] (there's two files). I then need this name to get passed into a function. Would it be better to so ifstream myFile(argv[2]) then pass in an ifstream like void func(ifstream myFile, ifstream myOtherFile)
or should I pass in the argv[2 & 4] itself like void func(char* nameOne, char* nameTwo)
? If the second way, is that the correct way I would pass that in?
The files are only used within the scope of that function, so I can open and close them in the function, or open them, pass them into the function, then close them after returning from the function. I just would like to know which way would make more sense. Thanks
RAII is thematic C++ resource management - whenever possible have the }
handle clean-up. In your case, this guideline suggests that an automatic duration ifstream
, which only lives in the scope of func
, is preferable since it closes the file on destruction. Since ifstream
is move only, you could call func
as func(ifstream(argv[2]), ifstream(argv[4]))
.