[英]fprintf is not printing the data into the file
我已經用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;
}
這段代碼很奇怪,它的某些部分不應該由c編譯器編譯。
例如:
int NV = 500 #No. of nodes
int m = 10000 #No. of edges
int nblinks = 0; # edge counter:
其中#No. of nodes
#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");
。
然后,如果您在屏幕上看到數據,請查看文件出了什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.