-
April 20th, 2004, 08:11 AM
#1
Getline needs help
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
-
April 20th, 2004, 09:45 AM
#2
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');
}
}
-
April 20th, 2004, 10:51 AM
#3
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....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|