Why my code doesn't read the rest of the file?

Hi,
I am new to C++ and this forum too. I wrote a program that reads a file into an array. The problem is it only reads the first number in the file and displays the message: "Segmentation fault (core dumped). What does that mean? I am assuming it's the way the file is accessed. What's wrong with this snippet if any?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      
       for(int i=0; i<1000; i++)
       {
	 
       myfile>>file[i];
       cout<<file[i]<<endl;
		}
     myfile.close( );
      
       
     
       return 0;
       }
How does file variable declared?
file is declared as int file[]={}
I tried specifying the size to a big number(like int file[1000]) but that also generates a number of random integer numbers.
What you have done is declared file to be an array of integers of size 0, which (as you can expect) isn't all that helpful. Perhaps what you want is this:
int file[1000] = {0};
This creates an array of integers of size 1000, but with all the values initialized to zero.
it is better to create something like this:
1
2
3
4
while (!fin.eof())
{
//some code here
}
Topic archived. No new replies allowed.