Troubleshooting C++ Code for Data Processing in Several Files

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C++ code snippet intended for processing multiple input files. Participants explore issues related to array allocation, indexing, and alternatives for managing file streams.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes their attempt to create an array of structs to manage multiple input files but encounters issues with dynamic array sizing.
  • Another participant points out that in standard C++, array sizes must be constant at compile time, leading to potential segmentation faults when using variable sizes.
  • Several participants note the importance of using 0-based indexing in C++ arrays, highlighting that 1-based indexing may lead to errors.
  • There is a suggestion to use a linked list as an alternative to manage file streams, as vectors are deemed unsuitable in this context.
  • A participant shares a working example using a fixed-size array for output files, demonstrating a different approach to file handling.
  • Another participant suggests dynamic allocation using "new" to avoid fixed-size limitations, allowing for more flexible array management.
  • Discussion includes the challenges of using vectors with file streams due to the lack of assignability and copy constructors for stream types.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to array sizing and indexing but propose different solutions and alternatives, indicating that no single approach has been universally accepted.

Contextual Notes

Limitations include the unresolved nature of the original code's functionality and the specific conditions under which participants suggest alternative data structures.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
6
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K