Reputation: 1202
I am trying to read data into an array from a text file. The data consists of several lines of doubles. For example lines like this:
0.019017584 -0.030264859 0.035091022 1.007338638 3.179054965 0.020865514 0.311854030
Now I was thinking to read the two-dimensional data table into a one-dimensional array and to then access them linearly. This is what I tried, but it doesn't work. I am a beginner in C++. Could anyone please help?
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
const int N = 500*7;
int main()
{
ifstream dataFile("C:\\Users\\Me\\Desktop\\Data.txt");
short i = 0;
int * data = new int[N];
double x;
while (!dataFile.eof())
{
dataFile >> x;
data[i] = x;
i++;
}
dataFile.close(); //closing the file
cout << data;
delete[] data;
return 0;
}
Upvotes: 1
Views: 90
Reputation: 5241
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::ifstream dataFile("data.txt");
std::vector<double> vecData;
while (dataFile)
{
double dTemp;
dataFile >> dTemp;
vecData.push_back(dTemp);
}
dataFile.close();
for (unsigned i = 0; i < vecData.size(); ++i)
{
std::cout << vecData[i] << std::endl;
}
return 0;
}
A couple of points:
std::cout << data
is asking the program to print out the pointer data. In C/C++, a pointer is just a number, so asking the program to print out data will print out the address (number) in memory where your first value is stored. In order to print out the actual data values, you need to iterate over all the loaded data values using another loop.
Don't mix types: for your maximum number of items N
, you have used an int
, but for your counter variable i
you have used a short
. Use the same type for both, perhaps unsigned int
.
In your loop, don't just check for the end your your file, check to see that the file is still good and that you can still read from it. Use while (dataFile.good())
or even while (dataFile)
. You should probably add another check before using your data or something similar.
Since you are using C++, consider using std::vector
or some other nice container, it makes things easier and you don't need to worry about forgetting to free your memory.
Upvotes: 1
Reputation: 73366
Since this is C++, you should use an std::vector
for this kind of job!
This what I do (Where Matrix is actually): (this also fills a vector b)
typedef std::vector< std::vector<double> > Matrix;
void inputFile(Matrix& A, std::vector<double>& b) {
std::ifstream infile;
infile.open ( "input.txt" );
if(!infile) std::cout << "File not found!" << std::endl;
const size_t N = b.size();
for(size_t i = 0 ; i < N ; ++i)
for(size_t j = 0 ; j < N ; ++j)
infile >> A[i][j];
for(size_t i = 0 ; i < N ; ++i)
infile >> b[i];
}
Tip: If you know how many doubles you will have at every line, then you could do this more easily!
About your code:
cout << data;
data is an array, which means that will output the address of the array, not the actual contents of it! In order to print, you should use a loop!
Also, you want to read double
s, but your data array is of type int
!
Finally, using eof(), in the condition of while will probably make you read the last line twice!
Upvotes: 2