Opening Multiple File

I am not sure if you can use for loop like this to pen multiple file array files. Can you please look and tell me is I am doing this right.

1
2
3
4
5
6
7
8
9
10
11
12
//Opens the file, where filenames is an array of files, count is the number of files in the array.
ifstream *OpenFiles(char * const fileNames[], size_t count)
{ 
    int fileCntr;
    char *filePntr[] = {"filePntr0", "filePntr1" , "filePntr2", "filePntr3", "filePntr4"};
    for (fileCntr = 0; fileCntr < count; fileCntr++)
    {
        ifstream filePntr[fileCntr](fileNames[fileCntr], ios::in);
            if (filePntr.is_open()) ErrorAndExit(fileNames[0]); 
    }
    return (&filePntr);
}
This shouldn't compile. filePntr is of type char**. &filePntr is of type char***. Neither of those make sense if you're returning a pointer to ifstream.

The for loop opens and closes each file.

Not really sure what you're trying to accomplish.
1
2
3
4
5
6
7
8
ifstream *OpenFiles(char * const fileNames[], size_t count){ 
    std::ifstream *ret=new std::ifstream[count];
    while (count){
        count--;
        ret[count].open(fileNames[count]);
    }
    return ret;
}
I did helios suggestion and have this program but I am getting a syntax error on the main line 15.
and cannot figure out why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

char *fileNames[] = {"mFile1.txt", "mFile2.txt", "mFile3.txt", "mFile4.txt", "mFile5.txt"};

ifstream *OpenFiles(char *fileNames[], size_t count);

int main()
{
    size_t count = 4;
    ifstream *OpenFile;
    OpenFile = OpenFiles(fileNames[],  count);
    return EXIT_SUCCESS;
}

/* Opens an array of files and returns a pointer to the first 
 * element (the first file).
 */
ifstream *OpenFiles(char *fileNames[], size_t count)
{ 
    ifstream *ret = new ifstream[count];
    while (count < 0)
    {
        count--;
        ret[count].open(fileNames[count]);
    }
    return ret;
}
OpenFile = OpenFiles(fileNames, count);
The homework specification states that I should dynamicall allocate memory when opening the file and free it when done with the file.
is new automatically allocate memory and delete[] free it?
I could find any document in www.cplusplus.com about allocating mem and free it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Opens an array of files and returns a pointer to the first 
 * element (the first file).
 */
 
ifstream *OpenFiles(char * const fileNames[], size_t count)
{ 
    //Variable declaration and initialization
    ifstream *ret = new ifstream[count];

    // If no command line arguments, error and exit  
    if (count <= 1) 
    {
        cerr << "There is no arguments.\n";
        //display message and wait for response before exitin
        cin.get();
        exit(EXIT_FAILURE);
    }

    //opens the files in the main program arguments 
    int loopCount;
    while (count >= 1)
    { 
        //open the file in a read optional second argument
        ret[count].open(fileNames[count]);
        //if any of the files fails to open close all the files and exit
        if (!ret[count].is_open()) 
        { 
            cerr << "Failed to open " << fileNames[count] << "\n";
            for (loopCount = count; loopCount >= 0; loopCount--) 
            {
                ret[loopCount].close();
                delete[] ret[loopCount];
                cout << "Closed " << fileNames[loopCount] << "\n";
                count = 0;
            }
        } 
        count--;
    }
    return ret;
}
new and new[] allocate memory and construct objects.
delete and delete[] destroy objects and deallocate memory.

delete must be paired with new, and delete[] must be paired with new[].

http://www.cplusplus.com/doc/tutorial/dynamic/

If you went C++ whole-hog, instead of piece-meal C++ written as if it were C, what you have so far might look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::vector<std::ifstream> MyOpenFiles( const std::string fileNames[], std::size_t count )
{
    std::vector<std::ifstream> files ;
    for ( unsigned i=0;  i<count;  ++i )
        files.emplace_back(fileNames[i]) ;

    bool all_open = true ;
    for ( unsigned i=0;  i<count;  ++i )
    {
        if ( !files[i].is_open() )
        {
            std::cerr << "Failed to open " << filenames[i] << '\n' ;
            all_open = false ;
        }
    }

    if ( !all_open )
        files.clear() ;

    return files ;
}
Last edited on
Topic archived. No new replies allowed.