I'm writing a program to open up multiple files given at the command line. I first did it with array notation. That seems to work. Now I am trying to use compact pointer notation for practice and getting used to pointers, but I am not doing it right. Anyone want to tell me what I'm doing wrong? Thanks.
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
ifstream *OpenFiles(char * const fileNames[], size_t count)
{
ifstream *fileObj = new ifstream[count];
if (fileObj == NULL) {
cerr << "Failed to create space for files";
exit(EXIT_FAILURE);
}
// working loop with array notation
// for (int loopCount = 0; loopCount < (int) count; loopCount++) {
// fileObj[loopCount].open(fileNames[loopCount], ios::out);
// if (fileObj[loopCount].is_open()) {
// cout << "Opened " << fileNames[loopCount] << "\n";
// }
//
// }
// start compact pointer notation that doesn't work
ifstream *start, *end;
for (start = fileObj; start < end; start++) {
start.open(fileNames, ios::out);
if (start.is_open()) {
cout << "Opened " << start << "\n";
}
}
return fileObj;
}