Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Ifstream issues

  1. Feb 17, 2008 #1
    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 (Text):

    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);
    }
    }
     
  2. jcsd
  3. Feb 17, 2008 #2

    jtbell

    User Avatar

    Staff: Mentor

    How do you know there's something wrong? That is, what are the symptoms?

    Oh-oh, now I see something...

    Code (Text):

    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: Feb 17, 2008
  4. Feb 17, 2008 #3
    I was getting a segmentation fault. It compiled fine but when I try to run it it complains. Sorry, I should have mentioned that.

    Thanks. Do you know of any other way to do what I am trying to do?
     
  5. Feb 17, 2008 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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)



    Surely you've learned other ways of allocating an array of information?
     
  6. Feb 17, 2008 #5
    Sorry, SIZE was declared but I hadn't included it in my original post.

    Turns out this is exactly what the problem was.

    Vectors don't work in this context so I'll try a Linked List.
     
  7. Feb 18, 2008 #6

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    #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: Feb 18, 2008
  8. Feb 18, 2008 #7

    jtbell

    User Avatar

    Staff: Mentor

    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: Feb 18, 2008
  9. Feb 18, 2008 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  10. Feb 18, 2008 #9

    jtbell

    User Avatar

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Ifstream issues
  1. Strmm Issue (Replies: 1)

  2. Issues with C++ errors? (Replies: 13)

Loading...