1

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;
}
2

2 Answers 2

2

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 doubles, 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!

Sign up to request clarification or add additional context in comments.

Comments

1
#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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

4 Comments

I would also recommend to test whether i < N when you test dataFile at the top of the loop, so a large input file doesn't overflow your array. But if you use std::vector as recommended, and use push_back() to add data to it, it will grow as needed and you don't need any test like i < N.
Thanks a lot for your help and advice, really useful! Could you please explain me what vecData.push_back(dTemp); does exactly?
vecData.push_back(dTemp) requests that the value dTemp be added to the end of the container. This reference might be helpful: cplusplus.com/reference/vector/vector/push_back
push_back(x) increases the number of elements of the std::vector by one and sets the (new) last element equal to x. So if you start with vecData default initialized, as shown, the first push_back will set vecData[0], the second push_back will set vecData[1], and so forth.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.