I have this:file
A B C D E F
1 3 5 6 7 8
2 3 5 3 4 3
1 8 8 6 3 7
5 6 9 7 5 2
0 2 0 0 4 0
tell me how to read those number into a tabel 5 rows 6 cols..plz
Thanks
Cevanne
Printable View
I have this:file
A B C D E F
1 3 5 6 7 8
2 3 5 3 4 3
1 8 8 6 3 7
5 6 9 7 5 2
0 2 0 0 4 0
tell me how to read those number into a tabel 5 rows 6 cols..plz
Thanks
Cevanne
Code:#include <cstdio>
int main()
{
char c;
FILE *file=fopen("C:\\test.txt", "r");
int matrix[5][6], i=0, j=0;
if(file)
{
while(fread(&c, 1, 1, file) && c!='\n');
while(fread(&c, 1, 1, file))
{
if(c>='0' && c<='9')
matrix[i][j++]=c-'0';
if(c=='\n')
++i, j=0;
}
fclose(file);
}
for(i=0;i<5;++i)
{
for(j=0;j<6;++j)
{
printf("%d\t", matrix[i][j]);
}
printf("%c", '\n');
}
}
another way to do this , :
#include <fstream>
//creat an object:
ifstream if;
int matrix[5][6];
if.open("test.txt");
for(int i=-0;i<5;i++)
{
for(int j=0;j<6;j++)
{
if>>matrix[i][j];
}
}
of.close();
i am not sure of this one, but i think this workes....