C/C++ Troubleshooting C++ Code for Data Processing in Several Files

AI Thread Summary
The discussion centers around a C++ programming issue related to handling multiple input files for data processing. The user initially attempts to create an array of structs to manage file streams but encounters a segmentation fault due to trying to declare an array with a size determined at runtime, which is not allowed in standard C++. The problem is compounded by using 1-based indexing instead of the correct 0-based indexing for arrays. Participants suggest alternatives for managing dynamic arrays, including using dynamic memory allocation with `new` to create an array of file streams, which avoids the limitations of static arrays. The discussion also touches on the challenges of using vectors with file streams, as they are not assignable due to the lack of a copy constructor. The conversation concludes with the realization that using pointers to file streams within a vector could be a viable solution.
americanforest
Messages
220
Reaction score
0
Hi everybody. I'm relatively new to C++ and am having an issue. What I need to do is take in several files and then do some data processing in each of them separately. I've tried to do this several ways and nothing has worked so far. Below is my last attempt.
Basically, each file is characterized not only by it's name but also by some information that the user has to give me. That's why I made a struct which holds info and the ifstream for each file. Then I decided to make an array of those to hold all the files. Can someone tell what is wrong with my code and how to fix it?

Code:
using namespace std;
#include <string>
#include <iostream>

struct inputstream
{
ifstream input;
//other data
};
int main()
{
char bufferin[SIZE];
int nofiles;
cout<<"How many input files will you be using? ";
cin>>nofiles;
inputstream inFiles[nofiles];
for(int i=1;i<=nofiles;i++)
{
cout<<"Please Print File: ";
cin>>bufferin;
//ask user for more info about file and put into inFiles[i]
inFiles[i].input.open(bufferin);
}
}
 
Technology news on Phys.org
How do you know there's something wrong? That is, what are the symptoms?

Oh-oh, now I see something...

Code:
cin>>nofiles;
inputstream inFiles[nofiles];

In standard C++, you can't create a static array with a size specified by a variable. The size has to be known at compilation time, that is, it has to be a constant, either a literal constant such as 5 or 25, or a named constant defined like "const int nofiles = 5;".
 
Last edited:
jtbell said:
How do you know there's something wrong? That is, what are the symptoms?

I was getting a segmentation fault. It compiled fine but when I try to run it it complains. Sorry, I should have mentioned that.

jtbell said:
Oh-oh, now I see something...

Code:
cin>>nofiles;
inputstream inFiles[nofiles];

In standard C++, you can't create a static array with a size specified by a variable. The size has to be known at compilation time, that is, it has to be a constant, either a literal constant such as 5 or 25, or a named constant defined like "const int nofiles = 5;".

Thanks. Do you know of any other way to do what I am trying to do?
 
americanforest said:
I was getting a segmentation fault. It compiled fine but when I try to run it it complains. Sorry, I should have mentioned that.
Where does it seg fault?

Where is SIZE declared? (And what is it?)

One problem I note is that you are using 1-based indexing, which is incorrect. In an X-long array, the valid indices are 0, 1, ..., X-2, X-1.

(One common cause of segmentation faults is when you use invalid array indices)



Thanks. Do you know of any other way to do what I am trying to do?
Surely you've learned other ways of allocating an array of information?
 
Hurkyl said:
Where does it seg fault?

Where is SIZE declared? (And what is it?)

Sorry, SIZE was declared but I hadn't included it in my original post.

Hurkyl said:
One problem I note is that you are using 1-based indexing, which is incorrect. In an X-long array, the valid indices are 0, 1, ..., X-2, X-1.

Turns out this is exactly what the problem was.

Hurkyl said:
Surely you've learned other ways of allocating an array of information?

Vectors don't work in this context so I'll try a Linked List.
 
I got curious because I thought I remembered doing something like this before. I couldn't do it with a vector, but I did manage it with a fixed-size array:

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    cout << "How many output files (maximum 10)? ";
    int nofiles;
    cin >> nofiles;

    ofstream outFiles[10];

    for (int i = 0; i < nofiles; ++i)
    {
        cout << "Name of output file #" << i << ": ";
        string name;
        cin >> name;
        outFiles[i].open(name.c_str());
        outFiles[i] << "This is file number " << i << "." << endl;
        outFiles[i].close();
    }
    
    return 0;
}
 
Last edited:
You can also allocate the array dynamically using "new". In my example above, replace the array declaration with

ofstream* outFiles = new ofstream[nofiles];

With this code, the maximum of 10 doesn't apply, of course.
 
Last edited:
The reason vector doesn't (directly) work is that the elements of a container are generally required to be assignable; that is, you can write x = y, and it has a copy constructor. ifstream, by explicit decision, is not assignable.
 
Right. The lack of a copy constructor for the various stream types also forces you to pass them to functions by reference, not by value.

I thought I remembered setting up a vector of file streams once, but I must have been mistaken. Maybe it was a vector of pointers to file streams.
 
Back
Top