gpt4 book ai didi

c - 读取 .txt 文件并将数据保存为 C 中的矩阵

转载 作者:行者123 更新时间:2023-12-01 15:15:37 24 4
gpt4 key购买 nike

我有兴趣读取 .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 -

其实它有更多的城市,但没关系。

我想阅读这份文件并记录距离。我尝试了以下代码:

#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;
}

但我没有得到我想要的...而且我不知道如何分隔名称和距离(字符和整数)。如果有人能帮助我,我将不胜感激!

最佳答案

虽然很想用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;
}

程序输出:

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 -

关于c - 读取 .txt 文件并将数据保存为 C 中的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com