我試圖撰寫一個代碼來計算另一個行程的平均執行時間(寫入使用了多少內核以及完成它所花費的時間)。問題是它卡在 linecounter() 函式中。
這是我將要閱讀的示例 log.txt:
1 11.3484
1 10.8089
1 10.7293
這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct result{
double *runtime;
int *size;
}R1;
int linecounter(FILE *fileptr)
{
int count_lines=0;
char chr=getc(fileptr);
while (chr != EOF)
{
printf("Inside the linecounter function()\n");
//Count whenever new line is encountered
if (chr == '\n') //works because I dont have any N in my txt, take care
{
count_lines ;
}
//take next character from file.
chr = getc(fileptr);
}
return count_lines;
}
int main()
{
FILE *f;
f=fopen("log.txt","r");
if(f == NULL)
{
printf("Error! opening the file");
exit(1);
}
printf("Counting lines\n");
int n=linecounter(f);
R1.runtime=malloc(n*sizeof(double));
R1.size=malloc(n*sizeof(int));
int j=1,k=0;
printf("Reading file\n");
j=fscanf(f,"%d %6.4f",&R1.size[k],&R1.runtime[k]); //First number of cores, then runtime
k ;
while(j!=EOF)
{
j=fscanf(f,"%d %6.4f",&R1.size[k],&R1.runtime[k]); //First number of cores, then runtime
k ;
}
int AvgRuntime=0,i=0;
if(R1.size[i]==1)
{
while(R1.size[i]==1) //for 1 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/i;
printf("Avg Runtime: %6.4f",AvgRuntime);
int prev_i=i;
AvgRuntime=0;
if(R1.size[i]==2)
{
while(R1.size[i]==2) //for 2 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/(i-prev_i);
printf("Avg Runtime: %6.4f",AvgRuntime);
prev_i=i;
AvgRuntime=0;
if(R1.size[i]==4)
{
while(R1.size[i]==4) //for 4 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/(i-prev_i);
printf("Avg Runtime: %6.4f",AvgRuntime);
fclose(f);
return 0;
}
這很簡單,我有 3 個選項,使用 1 個 2 或 4 個內核,所以我計算每種情況的平均值并為下一個情況覆寫它。當試圖獲取檔案中寫入了多少行時,問題就出現了。
通過在 linecounter 函式中添加 printf(),我注意到它永遠不會到達檔案末尾。它有某種解決方案嗎?
uj5u.com熱心網友回復:
您的代碼中有多個錯誤。
getc回傳一個int,而不是一個char。如果您char的默認情況下未簽名,則它無法保持價值EOF,并且您的比較chr != EOF永遠不會成立。
數完檔案中的行數后,檔案指標位于檔案末尾。您無法從中讀取任何進一步的值。您必須在掃描內容之前將其倒回。
您的格式說明符%6.4f對 無效scanf。您不能指定精度。只有在列印值時才能這樣做。
您的格式說明符%f需要一個float*as 引數,但您提供double*. 供double使用%lf。floats您可以使用列印,%f但必須使用%lffor scanf。
您使用%6.4f但AvgRuntime型別列印計算的平均值int。這也意味著平均值的計算是使用整數除法完成的。由于結果被截斷,因此不會有任何小數。做AvgRuntime一個 `double 代替。
你的編譯器應該告訴你很多這樣的問題。如果您沒有收到警告,則應顯著提高警告級別。如果您確實收到了這些警告,是什么讓您忽略了所有這些警告?
你也有邏輯錯誤。僅當對日志檔案中的條目進行排序時,您的平均才有效。您必須先讀取核心 1 的所有行,然后讀取核心 2 的所有行,然后再讀取核心 4 的所有行。如果檔案未排序,您的計算將失敗。
此外,如果您沒有核心條目,您將除以零,因為您不檢查ivs.的值prev_i。
最后,printftostdout通常是行緩沖的。除非達到 a,否則不會列印輸出\n。列印平均值時,應\n在行尾添加 a 。printf
固定版本(除了計算平均值的問題)可能如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct result{
double *runtime;
int *size;
}R1;
int linecounter(FILE *fileptr)
{
int count_lines=0;
int chr=getc(fileptr);
while (chr != EOF)
{
// printf("Inside the linecounter function()\n");
//Count whenever new line is encountered
if (chr == '\n') //works because I dont have any N in my txt, take care
{
count_lines ;
}
//take next character from file.
chr = getc(fileptr);
}
rewind (fileptr);
return count_lines;
}
int main()
{
FILE *f;
f=fopen("log.txt","r");
if(f == NULL)
{
printf("Error! opening the file");
exit(1);
}
printf("Counting lines\n");
int n=linecounter(f);
R1.runtime=malloc(n*sizeof(double));
R1.size=malloc(n*sizeof(int));
int j=1,k=0;
printf("Reading file\n");
j=fscanf(f,"%d %lf",&R1.size[k],&R1.runtime[k]); //First number of cores, then runtime
k ;
while(j==2)
{
j=fscanf(f,"%d %lf",&R1.size[k],&R1.runtime[k]); //First number of cores, then runtime
k ;
}
int i=0;
double AvgRuntime=0.0;
if(R1.size[i]==1)
{
while(R1.size[i]==1) //for 1 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/i;
printf("Avg Runtime: %6.4f\n",AvgRuntime);
int prev_i=i;
AvgRuntime=0.0;
if(R1.size[i]==2)
{
while(R1.size[i]==2) //for 2 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/(i-prev_i);
printf("Avg Runtime: %6.4f\n",AvgRuntime);
prev_i=i;
AvgRuntime=0;
if(R1.size[i]==4)
{
while(R1.size[i]==4) //for 4 core
{
AvgRuntime =R1.runtime[i];
i ;
}
}
AvgRuntime=AvgRuntime/(i-prev_i);
printf("Avg Runtime: %6.4f\n",AvgRuntime);
fclose(f);
return 0;
}
使用您的檔案輸出:
Counting lines
Reading file
Avg Runtime: 10.9622
Avg Runtime: -nan
Avg Runtime: -nan
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485112.html
下一篇:在C中可以交替管道嗎?
