I want to pass an input file to a function and within the function, fill an array by reference. My data file is just 90 characters. When I try running the file the compiler opens up another file called, io_base.h. I know the array and the data file are working fine because when I simply open the file and read the info into the array it works fine, just when I try passing the fstream input file things get wiggy. I couldn't really find anything about this in my book - can someone tell me if it can be done and if so, how?
This only happens when I try and run the program, it compiles just fine. I think there's something I'm not understanding about the fstream object, and how it works as opposed to regular variables. Like I said the books I have never covered this, or at least I couldn't find any mention of it.
Thanks for the reply, and this is consistent with what I've found on the net, but for some reason it's just not getting the file and printing garbage.
This is what I just tried, and from what I can tell should work...am I missing something? Thanks again for any help, I hate when something should work but doesn't, drives me bonkers.
I'm not 100% sure of the cause of your original problem, but I think it's caused by the call taking a copy of the ifstream object with a default copy constructor and the destructor being called for that object on return, closing the file or not updating the original ifstream object.
121157.cpp:35:30: error: use of deleted function ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’
In file included from 121157.cpp:2:0:
/usr/include/c++/4.7/fstream:420:11: note: ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/fstream:420:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
You display the contents of the "weather" array before you send it to "readFile". Its displaying the contents of an uninitialized array (which is random.)
Just move : readFile(weather, inputFile);
to be before the "for" loop:
You're having problems with sending "ifstream" by-value. just change it to: void readFile(char [][30], ifstream &);
And: void readFile(char weather[][30], ifstream &inputFile)
Now when it's sent by reference, it should work fine..
AH! Thanks guys, it's working today. After some messing around, I'm pretty sure I had a wrong .dat file name somewhere. Great learning experience, thanks for the help!