Reading file into an array

Hi,
I am using Visual Basic 2013. When I try and compile this program, the output is -858993460 , repeating 10 times. I am baffled.
I tried to change the encoding on my txt file, but that didn't help.
Does anyone have any ideas as to why this is happening?
Thank you,
Alexxis


#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
const int ARRAY_SIZE = 10;
int numbers [ARRAY_SIZE];
int count = 0;
ifstream inputFile;
inputFile.open ("TenNumbers.txt);
while (count < ARRAY_SIZE && inputFile >> numbers[count])
count ++;
inputFile.close ();
cout <<"The numbers are: ";
for (count =0; count < ARRAY_SIZE; count ++)
cout <<numbers[count] << " ";
cout << endl;
return 0;
}


Are you sure the file is opening?
http://www.cplusplus.com/reference/fstream/fstream/is_open/

I'm not sure if it's just a typo copying into the forum, but you're missing the closing " after the file name.
Last edited on
I tried that and I got this msg:

Error 1 error C2660: 'std::basic_ifstream<char,std::char_traits<char>>::close' : function does not take 1 arguments c:\users\teresa\documents\visual studio 2013\projects\test\test\source.cpp 14 1 test
I'm not sure what you tried - did you put something into the close function?

you're missing the closing " after the file name here
inputFile.open ("TenNumbers.txt);<--
ah, I misread what you wrote the first time. That is a typo from the forums, I have it in my original program. The error I posted was from me trying to put the txt file into inputFile.close- anyway, this is what I get when I run it with the proper "" in the open file:

The numbers are: -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460

so that is why I am asking if maybe it's related to the txt file?
Perhaps check to make sure the file opened properly?

Try using if (inputFile.fail()) in your code, and have something in there that tells you if it failed to open.
Is the file in the same directory as the program? Could be the program can't find the file.

you can use the is_open function (I linked the reference above) to see if the file actually opened.
I use Visual Studio Professional 2013, and if Visual Basic is like mine, then the way you have to organize it is weird.
For me, the input files have to be in the project directory (C:\Users\<User>\Documents\Visual Studio 2013\Projects\<name of project>) and in the same folder as the vcxproj file.
This is NOT the same place as the executable, which quite frankly makes no sense.
Topic archived. No new replies allowed.