我正在编写一个程序来打开命令行中给出的多个文件。我首先用数组表示法做到了。这似乎行得通。现在我正在尝试使用紧凑的指针表示法来练习并习惯指针,但我做得不对。有人想告诉我我做错了什么吗?谢谢。
#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;
}