The results of this program giving me values that should not be the way it is, it must display the list of numbers...

#include <iostream>
#include <fstream>
 
using namespace std;
 
void ReadList(int Array[], int N)
{
    ifstream data_file;
	data_file.open("numbers.dat");
 
	N=10;
 
	for(int i=0; i<N; i++)
	{
		data_file >> Array[i];
		cout << Array[i];
	}
 
	data_file.close();
}
 
int main ()
{
	int numbers[10];
 
	ReadList(numbers, 10);
 
	system("pause");
	return 0;
}

numbers.dat: 4 -30 0 7 42 -20 18 400 -123 -6(eof)

but the result is always like this not the list should be..

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
Press any key to continue...

I don't think you can read int from the file using ifstream. The problem with your code is that they are displaying the memory location of your array. Is there any reason to use an int array? You can use the read or readline function of the ifstream to capture the values. Refer to this link http://www.cplusplus.com/reference/iostream/ifstream/ for more information about ifstream.

ahm I just fixed the problem, anyway thanks for your help man.

Glad you got the answer :D

I would suggest you use the following whenever you use a file for data. It may help with an immediate solution that would otherwise be directly hidden from you.

ifstream fin;
	fin.open ("1.txt");
	if(fin.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

You will be notified if the file is truly there, or not. Should help resolve any potential problems from reading in values to an array from a nonexistent file, or a file not found.

I don't think you can read int from the file using ifstream.

Did you try it? You certainly can.

Glad you got the answer :D

I would suggest you use the following whenever you use a file for data. It may help with an immediate solution that would otherwise be directly hidden from you.

ifstream fin;
	fin.open ("1.txt");
	if(fin.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

You will be notified if the file is truly there, or not. Should help resolve any potential problems from reading in values to an array from a nonexistent file, or a file not found.

That's not the right application of fail(). Fail() is for detecting when the stream goes bad due to a bad input. Use fin.is_open() instead.

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.