Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中邻接矩阵的文本输入_C - Fatal编程技术网

c语言中邻接矩阵的文本输入

c语言中邻接矩阵的文本输入,c,C,这是我的代码的内联输入 int graph[V][V] = {{0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, {6, 8, 0, 0, 9}, {0, 5, 7, 9, 0}, }; 我想将这个图形输入从文本文件传送到图形数组。如果文件包含一位数,那么您可以使用我曾

这是我的代码的内联输入

int graph[V][V] = {{0, 2, 0, 6, 0},
                  {2, 0, 3, 8, 5},
                  {0, 3, 0, 0, 7},
                  {6, 8, 0, 0, 9},
                  {0, 5, 7, 9, 0},
                 };

我想将这个图形输入从文本文件传送到图形数组。

如果文件包含一位数,那么您可以使用我曾经使用过的代码

#include <stdio.h>
#include <stdlib.h>

int main() {
    int v;
    printf("Please enter the value of v ");
    scanf("%d",&v);
    int** graph = malloc(sizeof(int*)*v);
    int i,j;
    for(i=0;i<v;i++) 
        graph[i] = malloc(sizeof(int)*v);
    FILE *fp;
    fp = fopen("input","r");
    char c;
    for(i=0;i<v;i++) {
          for(j = 0; j < v; j++) {
            fscanf(fp, " %c", &c);
            graph[i][j] = c-'0';
        }
    }
    for(i=0;i<v;i++) {
        for(j=0;j<v;j++) {
            printf("%d ",graph[i][j]);
        }
        printf("\n");
    }
    return 0;
}

没有必要投马洛克,谢谢。我一直认为它返回一个空指针。是的!这就是为什么您不需要强制转换mallock的值的原因也许您忘记发布您迄今为止尝试过的代码了?