0

I would like to read text into arrayA[][] which includes data and characters. It seems like

And I need to calculate the data then. My code cannot do that. I try to output A[][] to see if there is anything wrong and it turns out that all the elements are 0.000. Please help me to find out how to change it. Thanks!

enter code here

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
int    p = 518868;
int    q = 11;
int    s, t, i, j, k, m, d, r, u;
double A[518868][11];
int    main(void)
{

    FILE  *fid;

    using namespace std;

    ifstream file("dump600.txt");

    if (file.is_open())
      {
        for (i = 1; i <= p; i++)
          {
            for (j = 1; j <= q; j++)
              {
                file >> A[i][j];
              }
          }
      }



fid = fopen("RstA600.txt", "wt");
for (i = 1; i <= 10000; i++)
{
    for (j = 1; j <= 11; j++)
    {
        if (j == 11)
        {
           fprintf(fid, "%f\n", A[i][j]);
        }
        else
        {
            fprintf(fid, "%f\t", A[i][j]);
        }
    }
}
fclose(fid);

dump600.txt
id  type x  y   z   c_q[1]       c_q[2]     c_q[3]       c_q[4]    x    y
1   1    0  0   30  -0.0075608  -0.710037   0.703789    0.021699    0   0
3   1    10 0   30  0.0138984   -0.409617   0.0338428   -0.911523   10  0
5   1    20 0   30  -0.31169    -0.685503   0.450455    0.479609    20  0
7   1    30 0   30  -0.194787   -0.373789   0.511419    -0.74886    30  0
12
  • 2
    Arrays in C++ start with index 0, not 1, so it should be for (i = 0; i < p; i++) and so on; further, check the return value of operator >> to see if a value could have been read correctly. Your code may fail if the first line contains anything else than a number, because it will then repeatedly failing in reading this non-number as a number. Commented Aug 7, 2017 at 20:28
  • 1
    can you please format this to something readable It seems like id type x y z c_q[1] c_q[2] c_q[3] c_q[4] x y 1 1 0 0 30 -0.0075608 -0.710037 0.703789 0.021699 0 0 3 1 10 0 30 0.0138984 -0.409617 0.0338428 -0.911523 10 0 5 1 20 0 30 -0.31169 -0.685503 0.450455 0.479609 20 0 7 1 30 0 30 -0.194787 -0.373789 0.511419 -0.74886 30 0 Commented Aug 7, 2017 at 20:29
  • 4
    This is a good opportunity to learn to use a debugger and step through your code, to find out what's gone wrong. Commented Aug 7, 2017 at 20:30
  • 2
    Read and discard the first line with std::getline. After that, use something similar to if (! file >> A[i][j]) { std::cerr << "cannot parse input\n"; return -1; } to read the file into the array. Commented Aug 7, 2017 at 20:54
  • 1
    @Ron vector of vectors is slow due to poor spatial locality. A vector of std::array would be better assuming (and it's a fairly safe assumption) that std::array doesn't do anything silly to ruin data contiguity. Commented Aug 7, 2017 at 20:59

1 Answer 1

1

The problem seems to be the first line containing text, which will let any file >> A[i][j] fail. To overcome this, you could skip the first line using a std::getline; also consider to somehow react on invalid input. Further, indexes in C++ start with 0, not with 1, such that for (j = 1; j <= q; j++) and so on run out of bounds and yield undefined behaviour. See a sample of code that should work:

  if (file.is_open())
  {
    std::string line;
    if(std::getline(file, line)) {
      for (i = 0; i < p; i++)
      {
          for (j = 0; j < q; j++)
          {
             if (!file >> A[i][j]) {
                cout << "Invalid input.";
             }
          }
      }
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.