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
0, not1, so it should befor (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.std::getline. After that, use something similar toif (! file >> A[i][j]) { std::cerr << "cannot parse input\n"; return -1; }to read the file into the array.vectorofvectors is slow due to poor spatial locality. Avectorofstd::arraywould be better assuming (and it's a fairly safe assumption) thatstd::arraydoesn't do anything silly to ruin data contiguity.