在C语言中,怎么读入txt文件中的稀疏矩阵啊,麻烦大家看看,谢谢
H矩阵中,第一列表示行号,第二列表示该行有多少个不为0的数,后面的数字表示不为0的数的列号。
现在要用c语言读入这个数,然后对他进行操作,把它变成一个512*252的稀疏矩阵,再把这个512*252的稀疏矩阵存储在另一个文件中,
自己琢磨了很久,还是没有做出来,我的代码如下,大家能帮忙看一下吗?非常感谢
程序代码:#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAX_LENGTH 60
int main(int argc, char* argv[])
{
//StripSpace::Run();
FILE *h;
FILE *check;
char data[9][MAX_LENGTH];
char* result[252][9];
char* H[252][512];
check=fopen("data.txt","w");
if( (h=fopen("H矩阵.txt","rt"))==NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
int n = 0;
int j=0;
while ( !feof( h ))
{
fscanf( h, "%s %s %s %s %s %s %s %s %s", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5], &data[6], &data[7], &data[8] ); // C4996
for (int i = 0; i < 9; i++)
{
result[n][i] = (char*)malloc(MAX_LENGTH);
strcpy(result[n][i], data[i]);
}
for(j=0;j<512;j++)
{
if(j==*result[n][i]&&(i!=0)&&(i!=1))
*H[n][j]=1;
else
*H[n][j]=0;
}
fwrite(H, sizeof(H), 1, check);
for(j=0;j<512;j++)
printf("%s",H[n][j]);
printf("\n");
n++;
}
fclose( h );
fclose( check );
for (int i = 0; i < 252; i++)
{
for (int j = 0; j < 9; j++)
{
free(result[i][j]);
}
}
}
return 0;
}








