我对编码非常陌生,并且在打开文件时遇到了一些困难。我做了一些基本的文件,其中的数字,所有诠释用c读取文本文件
下面是该文件的例子:
20
110
1 0 1 5
5 6 7 8
这里是我写的读它的代码:
int* init_dados(char *name, int *n, int *iter){
FILE *f;
int *p, *q;
int i, j;
f=fopen(name, "r");
if(!f)
{
printf("Error on the access of the file\n");
exit(1);
}
// number of iteractions
fscanf(f, " %d", iter);
// number of vertices
fscanf(f, " %d", n);
p = malloc(sizeof(int)*(*n)*(*n));
if(!p)
{
printf("Error on the allocation of the memory\n");
exit(1);
}
q=p;
for(i=0; i<*n; i++)
for(j=0; j<*n; j++)
fscanf(f, " %d", q++);
fclose(f);
return p;
}
现在我有一个新的文件,它有像这样int和漂浮:
1 2 7.83
1 3 -5.45
1 4 8.90
我想读取文本文件d还将其打印在屏幕上。我想也许我可以做最后一个节目,但我也有花车。我必须将它们保存在一个新的矢量中吗?你会怎么做?你可以帮我吗?
“110”顶点信息在哪里?只有8个数字。然而,你的“新文件”可能需要读入一个'struct',比如'struct indata {int a; int b; float f; };' –
我投票结束这个问题作为题外话,因为这是一个“写我的代码”的问题,一个不好的例子。 –
请更新问题。很难说出你真正想要的。 – RoadRunner