fprintf不会将数据打印到文件中

时间:2017-06-23 09:38:32

标签: c

我在C中编写了一个代码。在main()的for循环中,我写了以下内容。当我运行代码时,fprintf语句不会将数据写入数据文件。我正在为图表添加边。在添加时,我正在使用相邻列表检查对是否已经在列表中。如果对不在列表中,我使用函数调用addEdge():

添加边
int main()
{

        FILE *ed = NULL;
        ed = fopen("c2.txt","w");
        FILE *ls = NULL;
        ls = fopen("e2.txt", "w");
        FILE *lk = NULL;
        lk = fopen("link2.txt","w");
        if(ls == NULL){
        printf("Error in opening file ls!\n");}
        if(ed == NULL){
        printf("Error in opening file ed!\n");}
        if(lk == NULL){
        printf("Error in opening file lk!\n");}
        else 
        {
            int src,dest;
            int src1,dest1;
            int i,k,v;
            int NV  = 500   #No. of nodes
            int m  = 10000 #No. of edges
            int nblinks = 0; # edge counter:
            double x[NV],x1[NV];
            double x2[NV];
            unifRand(x,x1,x2); // function call for infection rate:Pass only name of array:
            int ss[NV];
            data1(ss);       // function call for generated data:
            double sum,sum1 = 0.0;
            double R = 0.00268; //Learning rate: // (i) 0.01 // ii. 0.001 // (iii) 0.00251 (iv)0.00268
            double L[NV][NV-1];
            double L1[NV][NV-1];
            double L_old[NV][NV-1];
            double L2[NV][NV-1];
            double t = 0, dt = 0.1;
            double C; 
            struct Graph* graph  = createGraph(NV); // Function call for graph:
            int d;
            int nblinks = 0;     // Edge counter:
            L_old[0][1] = 0.01; // choice of initial coupling/guess : 0.02


            for(i=0; i<NV-1; i++)
            {

                 x2[i] = 0.02; 

             }

            fprintf(ed,"True\t\tMeasured\t x[i]\t\t x1[i]\t\t x2[i]\t\tsum\t\t Error\t\ttime\tnode\n");
            fprintf(ed,"------------------------------------------------------------------------------------------------------------\n");
            fprintf(lk," Pair of Nodes:\n");
            fprintf(lk,"---------------------------------------------------------------------\n");




    for( i = 0, k = 1; i < NV-1; i++)
    { 

            struct AdjListNode* temp = graph->array[i].head; 
            printGraph(graph, &sum, temp);
            # if the adjacent node is not zero: 
            while(temp != NULL && temp->next != NULL)
            { 

                 fprintf(ls,"%d %d\n", i, temp->dest);
                 temp = temp->next;
                  while(nblinks < m)  
                  { 

                  #Random numbers for source and destination nodes:
                  int src1 = random()%NV;
                  int dest1 = random()%NV;

                  if(src1 < dest1)
                  {
                      src  = src1;
                      dest = dest1;
                  }
                  else
                  {
                      src = dest1;
                      dest = src1;
                   } 

                 int pairs[2] = {src, dest};
                 //If pairs are not in the list, add the edges then:  
                 if(src != dest && pairs[2] != (i, temp->dest)) 
                 {

                  printf("%d\t%d\n",src,dest);
                  fprintf(lk,"(%d,%d)\t(%d,%d)\n", pairs[0],pairs[1],i, temp->dest);
                  addEdge(graph, src, dest);
                  nblinks++; // counter:


                  }  // End if


                 }   //End while1 :


                 }   // End while 2:

            # I have written my calculation here   :
            fprintf(ed,"%0.6lf\t%0.6lf\t%0.6lf\t%0.6lf\t%0.5lf\t\t%0.6lf\t%0.6lf\t%0.1lf\t%d\t%0.6lf\n", L[i][k],L1[0][1],x[i],x1[i], 
         x2[i],sum1,C, t,ss[k],L2[i][k]);  
              }  // End for
        }        // End else

       fclose(ed); 
       fclose(ls); 
       fclose(lk);
       ed = NULL; 
       ls = NULL; 
       lk = NULL;
       return 0;
}

1 个答案:

答案 0 :(得分:0)

这段代码非常奇怪,它的某些部分不应该由c编译器编译。

E.g:

        int NV  = 500   #No. of nodes
        int m  = 10000 #No. of edges
        int nblinks = 0; # edge counter:

其中#No. of nodes就像评论一样,int NV = 500最后没有; - 所以不是完整的代码行。

风格也不完美,例如:

    if(ls == NULL){
    printf("Error in opening file ls!\n");}
    if(ed == NULL){
    printf("Error in opening file ed!\n");}
    if(lk == NULL){
    printf("Error in opening file lk!\n");}
    else 
    {

应该是

    if(ls == NULL)
    {
          printf("Error in opening file ls!\n");
          return 1;
    }
    if(ed == NULL)
    {
          printf("Error in opening file ed!\n");
          return 1;
    }
    if(lk == NULL)
    {
          printf("Error in opening file lk!\n");
          return 1;
    }
    // no else needed after 3 way to the exit (return)

或更好

    if(!lk || !ed || !ls)
    {
          printf("Error in opening on of the files\n");
          return 1;
    } 

一般情况下,如果所有文件都以所需模式打开(并且在所有写入都关闭后),fprintf不应该出现问题,除了格式字符串不正确或输出数据错误。

<强>更新

只是为了检查输出,您可以使用stdout作为fprintf的第一个参数来在屏幕(控制台)上查看结果,或者使用lk = stdout;代替lk = fopen("link2.txt", "w");

然后,如果您在屏幕上看到数据,请查看该文件的错误。