Hey guys,
I am trying to import a table of data form a text file by using "ifstream" command, But I can import just a table of 14*14 form 364*54. Any suggestion is really appreciated. My code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <conio.h>
#include<fstream>
using std::stringstream;
usingnamespace std;
float total[364][54];
int main()
{
ifstream myfile1("C:\\Users\\File1.txt", ios_base::in);
for (int i=0;i<=55;i++)
{
for (int j=0;j<=363;j++)
{
myfile1 >> total[i][j];
}
}
_getch();
}
Sure, but you're going to have to change your loop limits.
for (int i=0;i<=55;i++)
This is going to cause undefined behavior. You're going to execute that loop 56 times. Your array is only declared as [54] (0-53).
for (int j=0;j<=363;j++)
This loop is correct, but I should point out the normal idiom is to use <, not <=. By using <, this is written as
for (int j=0;j<364;j++)
Which makes the number in the termination condition the same as the dimension of the array. When you use different values, there is too much chance for error (as in you outer loop).
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thanks for the suggestion. But the point is that even if there is a problem in the counter of "for" loop, the original matrix will not be filled properly (means some differences).
Honestly, It is too weird because when I run the program step by step, I am seeing that it reads the data, but I don't know why it doesn't store them in my introduced variable "total[i][j]".
I would really grateful if someone has any suggestion.
You're not storing the contents where you think you are.
Line 13: i and j are reversed. j goes from 0 to 364, but the second dimension of your array is 54.
Lines 11, 13: Your for loop limits are still wrong. You're going to get a bounds error by referencing too many elements in each direction. You've defined two constants for your array sizes. You should use those constants in the termination condition of your for loops.
1 2 3
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
...
By using the constants in your for loops with the correct < idiom, you ensure that the loops execute the correct number of times. This also has the benefit that if you want to change the dimensions of the array (say to 14x14), you need only change lines 3 and 4.
Your array has 364 rows. These rows are 0-363. Your loop at line 11 goes from 0 to 364 resulting in referencing a non-existant row (total[364][j]). Ditto for the number of columns.
BTW, I find the use of M,N,i,j confusing. IMO, referring to a 2D array as rows and columns is clearer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<fstream>
usingnamespace std;
constint ROWS=364;
constint COLS=54;
float total[ROWS][COLS];
int main()
{ ifstream myfile1("C:\\Users\\File1.txt", ios_base::in);
for (int r = 0; r < ROWS; r++)
{ for (int c = 0; c < COLS; c++)
{ myfile1 >> total[r][c];
}
}
}