我有兴趣阅读.txt文件,并将数据保存在C中的矩阵中.
dist.txt is the following:
Distance Amsterdam Antwerp Athens Barcelona Berlin
Amsterdam - 160 3082 1639 649
Antwerp 160 - 2766 1465 723
Athens 3082 2766 - 3312 2552
Barcelona 1639 1465 3312 - 1899
Berlin 649 723 2552 1899 -
Run Code Online (Sandbox Code Playgroud)
事实上它有更多的城市,但没关系.
我想阅读这份文件并记录距离.我试过以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define rows 6
#define cols 6
int main()
{
FILE *nansa;
char *buffer;
int ret,row=0,i,j;
char delims[]=" \t";
char *result=NULL;
double **mat=malloc( rows*sizeof(double*) );
for(i=0; i<rows; i++)
{
mat[i]=malloc( cols*sizeof(double) );
}
if ((nansa=fopen("dist.txt","r"))==NULL)
{
fprintf(stdout, "Error\n");
return -1;
}
while(!feof(nansa))
{
buffer=malloc( sizeof(char)*4096 );
memset(buffer,0,4096);
ret=fscanf(nansa, "%4095[^\n]\n", buffer);
if(ret != EOF)
{
int field=0;
result=strtok(buffer,delims);
while(result != NULL)
{
if(field>4) break;
mat[row][field]=atof(result);
result=strtok(NULL,delims);
field++;
}
++row;
}
free(buffer);
}
fclose(nansa);
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%g%s", mat[i][j], j<cols-1 ? "\t" : "\n");
free(mat[i]);
}
}
free(mat);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我没有得到我想要的东西......而且我不知道如何分隔名称和距离(字符和整数).如果有人能帮助我,我将非常感激!
虽然用它fgets
来读取每一行很有诱惑力(这feof
是错误的),但问题只是少数城市的例子:也许有10000个.所以我假设任何城市的名称都小于64(仅供输入).保留的内存对于名称的实际长度是正确的.
行和列将是相同的,因此没有不同的定义:实际上我只定义了城市的数量.我为城市名称(相同的向下)和距离使用单独的数组.
为了简单起见,我已经完成了错误检查,但在没有消息的情况下中止了.但是需要修改的地方是城市是一个多字的名称,如洛杉矶(%s
停在任何空白处).您需要一个不同的方法,或者使用下划线来破坏city_name.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define cities 5
int main(void){
FILE *nansa;
char buffer[64];
char distname[64]; // just to save work
char *city[cities]; // city names
int *dist[cities]; // distance array
int i, j, len, wid = 0;
if((nansa = fopen("dist.txt","r")) == NULL)
exit(1); // file open fault
// read the headings
if(fscanf(nansa, "%63s", buffer) != 1) // read the word for "distance"
exit(1); // fscanf fault
strcpy(distname, buffer);
for(i=0; i<cities; i++) { // read the city names
if(fscanf(nansa, "%63s", buffer) != 1)
exit(1); // fscanf fault
len = strlen(buffer) + 1;
if (wid < len)
wid = len; // column width
if((city[i] = malloc(len)) == NULL) // memory for city name
exit(1); // malloc fault
strcpy(city[i], buffer);
}
// read the data
for(j=0; j<cities; j++) { // read each table line
if((dist[j] = malloc(cities * sizeof(int))) == NULL) // memory for distance chart
exit(1); // malloc fault
if(fscanf(nansa, "%s", buffer) != 1) // skip the city name
exit(1); // fscanf fault
for(i=0; i<cities; i++) { // read each table line
if(fscanf(nansa, "%63s", buffer) != 1) // read the distance
exit(1); // fscanf fault
dist[j][i] = atoi(buffer);
}
}
fclose(nansa);
// display the table headings
printf("%-*s", wid, distname); // use the terminology in the file
for(i=0; i<cities; i++) // each city name
printf("%-*s", wid, city[i]);
printf("\n");
// display each line
for(j=0; j<cities; j++) {
printf("%-*s", wid, city[j]); // start with city name
for(i=0; i<cities; i++) { // each table data
if(dist[j][i])
printf("%-*d", wid, dist[j][i]);
else
printf("%-*c", wid, '-');
}
printf("\n");
}
// free the memory
for(i=0; i<cities; i++) {
free (city[i]);
free (dist[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
节目输出:
Distance Amsterdam Antwerp Athens Barcelona Berlin
Amsterdam - 160 3082 1639 649
Antwerp 160 - 2766 1465 723
Athens 3082 2766 - 3312 2552
Barcelona 1639 1465 3312 - 1899
Berlin 649 723 2552 1899 -
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
254 次 |
最近记录: |