c程序中文件读取的尴尬行为(SIGSEGV信号)

时间:2014-12-26 08:13:16

标签: c sigsegv minimum-spanning-tree file-read

我实现了一个应该使用Prim找到最小生成树的程序。图表的边缘在文件中描述。

首先,程序读取此文件并在数据结构中存储边缘。之后,它运行算法。这是完整的代码:

      #include <stdio.h>


    #include <stdlib.h>

      #define MAXVERTICES 1000
      #define INFINITY 99999
      #define TAILLEMAX 100

      struct vertex {
            int visited, predecessor;
            int dist;   // distance qui sépare ce noeud de la distance minimale d'un autre noeud
      };

      int treeEdge[MAXVERTICES][2], mstCost; // tableau des 'aretes' qui constituent l'arbre couvrant
      int graphEdge[MAXVERTICES][MAXVERTICES], nodeCount; // matrice d'adjacences (couts séparant chaque sommet des autres sommets)
      struct vertex node[MAXVERTICES];  // tableau de sommets numérotés de 1 à n

      /* construct the input graph */

      void buildGraph() {
            int source, dest, weight, test;
            char temp[TAILLEMAX];

        FILE* fichier = NULL;
        fichier = fopen("inst_v100.txt", "r");
        if (fichier != NULL)
        {
            fgets(temp, TAILLEMAX, fichier); // sauter ligne
            fscanf(fichier,"%s%d",temp,&nodeCount); // nbr de sommets
            fgets(temp, TAILLEMAX, fichier);
            fgets(temp, TAILLEMAX, fichier);

            while (1)
            {
                    test = fscanf(fichier,"%d%d%d",&source,&dest,&weight);
                    if(test==0)
                        break;
                    /* update weight of the edge */
                    graphEdge[source][dest] = weight;
                    graphEdge[dest][source] = weight;
                    fgets(temp, TAILLEMAX, fichier); // sauter ligne
            }
        }
        fclose(fichier);
        return;
  }



    /* all vertices are visited or not */
      int allVisited() {
            int i;
            for (i = 0; i < nodeCount; i++)
                    if (node[i].visited == 0)
                            return 0;
            return 1;
      }

      /* construct minimum spanning tree */
      int buildTree() {
            int i = 0, count = 0, currentNode, mindist;
            while (i < nodeCount) {
                    node[i].visited = 0;
                    node[i].predecessor = 0;
                    node[i].dist = INFINITY;
                    i++;
    }

        node[0].visited = 1;
        node[0].dist = 0;

        currentNode = 0;
        while (allVisited() == 0) {
                for (i = 0; i < nodeCount; i++) {  // pour le sommet courant, chercher la distance min qui le sépare de chaque sommet

                        /* Find the adjacent vertices and update the edge lenght */
                        if(graphEdge[currentNode][i] > 0 && node[i].visited == 0) {
                                if (graphEdge[currentNode][i] < node[i].dist) {
                                        node[i].predecessor = currentNode;
                                        node[i].dist = graphEdge[currentNode][i];
                                }
                        }
                }

                mindist = INFINITY;
                /* trouver l'arete avec le poids minimum */
                for (i = 0; i < nodeCount; i++) {
                        if (node[i].dist < mindist && node[i].visited == 0) {
                                mindist = node[i].dist;
                                currentNode = i;
                        }
                }
                /* Mark the vertex as visited - edge with min cost */
                node[currentNode].visited = 1;
                treeEdge[count][0] = node[currentNode].predecessor;
                treeEdge[count][1] = currentNode;

                /* calculate cost of MST */
                mstCost = mstCost + graphEdge[treeEdge[count][0]][treeEdge[count][1]];
                count++;
        }
        return count;
  }

  int main() {
        int i, count1;

        /* construct graph */
        buildGraph();
        count1 = buildTree();
        printf("MST is composed of below edges:\n");
        for (i = 0; i < count1; i++) {
                printf("%d<->%d\n", treeEdge[i][0], treeEdge[i][1]);
        }
        printf("\n");
        printf("Cost of Minimum Spanning Tree: %d\n", mstCost);
        return 0;
  }

笨拙地,当执行开始时,程序发送SIGSEV信号。

你可以帮我弄清楚我在做什么吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

解决了问题。

事实上,如果仔细观察,我会使用fgets和fscanf,这会引起歧义。 解决方案是使用fgets(以字符串形式存储读取的行),然后使用sscanf从该字符串中获取所需的信息。

它提供了这样的东西:

    fgets(the_string, MAXLENGTH, file);
    sscanf(the_string,"%d",&number_wanted);

非常感谢你,我希望我的回答对其他有相同问题的人有所帮助。