小编S. *_*roa的帖子

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

我有兴趣阅读.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( …
Run Code Online (Sandbox Code Playgroud)

c matrix

2
推荐指数
1
解决办法
254
查看次数

标签 统计

c ×1

matrix ×1