2010-06-28 46 views
0

我正在編寫一個程序來讀取文本文件中的信息,並且我已經正常工作。問題是我試圖添加功能來計算一些字段的平均值,並且必須將字符串轉換爲雙精度。我注意到atof會在某些情況下工作,但大部分會返回-1。然後,我意識到我沒有包含stdlib,所以我補充說,但現在我只是發生了一個分段錯誤,只有這一個變化。包括<stdlib.h>會導致分段錯誤

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

main(int argc, char *argv[]){ 

    int numParts=10; 
    long numTests=49000; 

    char filename[] = "sweep_data.txt"; 
    FILE *fp= fopen(filename,"r"); 
    FILE *out= fopen("TesterData.csv", "w+"); 
    FILE *meanf= fopen("meanData.txt", "w+"); 

    char delims[] = " <>"; 
    char *result = NULL; 
    char line [128]; 

    char *TestNum=NULL; 
    char *TestName=NULL; 
    char *TestName2=NULL; 
    char *SequencerName=NULL; 
    char *lowlim=NULL; 
    char *hilim=NULL; 
    char *value=NULL; 
    char *units=NULL; 
    char *DeviceNum=NULL; 

    char ValueArray[numTests][10]; 
    char ***ValuePtr = NULL; 
    char InfoArray[numTests][20]; 
    char ***InfoPtr = NULL; 

    double mean[numTests]; 
    long sum; 
    int intResult; 
    int count=0; 
    int DeviceCount=-1; 
    int i,j, m,n,k,a,b, len, mLen; 

    /*Allocate Memory for 2D arrays*/ 
    ValuePtr = malloc(numParts * sizeof *ValuePtr); 

    for(i=0; i<numParts;i++){ 
     ValuePtr[i]=malloc(numTests*sizeof *ValuePtr); 


     for(j=0; j<numTests; j++){ 
      ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1); 
     } 
    } 

    InfoPtr = malloc(6 * sizeof *InfoPtr); 

    if(InfoPtr != NULL){ 
     for(a=0; a<6;a++){ 
      InfoPtr[a]=malloc(numTests*sizeof *InfoPtr); 

      for(b=0; b<numTests; b++){ 
       InfoPtr[a][b]= malloc(strlen(InfoArray[b]) +1); 
      } 
     } 
    } 

    while(fgets(line, sizeof line, fp) != NULL){ 
     result = strtok(line, delims); 
     TestNum=result; 

     intResult = strtol(result, NULL, 10); 
     if(intResult ==0){ 
      if(strcmp(result, "Device:")==0){ 
       DeviceCount++; 
       count=0; 
      } 
      continue; //if doesn't start with a number go to next line 
     } 

     result = strtok(NULL, delims); 
     TestName= result; 
     result = strtok(NULL, delims); 
     TestName2= result; 
     result = strtok(NULL, delims); 
     SequencerName = result; 
     lowlim=SequencerName; 

     if(atof(SequencerName)>1 || atof(SequencerName)<-1){ 
      result= strtok(NULL, delims);  
      lowlim=result; 
      strcat(TestName, TestName2); 
     } 
     else 
      SequencerName= TestName2;  


     if(strstr(TestName, "%")!=NULL || strcmp(TestName,"PTgen")==0 || strcmp(TestName,"mode")==0){ 
      units="NA"; 
     } 

     else if(strstr(TestName, "2nd") == NULL && strstr(TestName, "3rd") == NULL){ 
      result= strtok(NULL, delims); 
      units = result; 
     } 

     result= strtok(NULL, delims); 
     value=result; 

     if(strstr(TestName, "%")==NULL && strcmp(TestName,"PTgen")!=0 && strcmp(TestName,"mode")!=0){ 
      result=strtok(NULL, delims); 
      if(strstr(TestName, "2nd") != NULL || strstr(TestName, "3rd") != NULL) 
       units = result; 
     } 

     result=strtok(NULL, delims); 
     hilim=result; 
     if(hilim[strlen(hilim)-1]=='\n'){ 
      hilim[strlen(hilim)-1]='\0'; 
      hilim[strlen(hilim)-1]='\0'; 
     } 

     if(DeviceCount==0){ 
      strcpy(InfoPtr[0][count], TestNum); 
      strcpy(InfoPtr[1][count], TestName); 
      strcpy(InfoPtr[2][count], SequencerName); 
      strcpy(InfoPtr[3][count], lowlim); 
      strcpy(InfoPtr[4][count], hilim); 
      strcpy(InfoPtr[5][count], units); 
     } 

     strcpy(ValuePtr[DeviceCount][count],value); 

     count++; 
    } 

    for(b=0;b<numTests;b++){ 
     sum=0; 
     for(a=0;a<numParts;a++){ 
      fprintf(meanf, "%s\n", ValuePtr[a][b]); 
      sum=atof(ValuePtr[a][b]); 
      fprintf(meanf, "%f\n",sum); 
     } 
     mean[b]=sum/numParts; 
     for(n=0; n<2;n++){ 
      fprintf(meanf, "%s ", InfoPtr[n][b]); 
     } 
     fprintf(meanf, "%f\n",mean[b]); 
    } 
    printf("NumTests: %i\n",count); //number of tests run 
    printf("NumParts: %i\n", DeviceCount+1);//number of parts run 
    fprintf(out,"Test#, TestName,SeqName,LowLim,UpLim,Units,"); 

    for(j=1; j<=numParts;j++){ 
     fprintf(out," Device#%i,", j); 
    } 
    fprintf(out, "\n"); 

    for (n = 0; n < numTests; n++) { 
     for(a=0;a<6;a++){ 
      fprintf(out, "%s", InfoPtr[a][n]); 

      fprintf(out, ", "); 
     } 

     for (m =0; m < numParts ; m++) { 
      fprintf(out, "%s, ", ValuePtr[m][n]); 
     } 
     fprintf(out, "\n"); 
    } 
} 

我在那裏需要轉換爲雙部分是

for(b=0;b<numTests;b++){ 
    sum=0; 
    for(a=0;a<numParts;a++){ 
     fprintf(meanf, "%s\n", ValuePtr[a][b]); 
     sum+=atof(ValuePtr[a][b]); 
     fprintf(meanf, "%f\n",sum); 
    } 
    mean[b]=sum/numTests; 
    for(n=0; n<2;n++){ 
     fprintf(meanf, "%s ", InfoPtr[n][b]); 
    } 
    fprintf(meanf, "%f\n",mean[b]); 
} 
+0

當您運行在調試程序是什麼說的? – bk1e 2010-06-29 06:12:13

+0

我得到:程序接收到的信號SIGSEGV,分段錯誤。 0x0000003658f70a30 strlen()from /lib64/tls/libc.so.6。這將表明一個問題,如下所示。但是,當stdlib沒有被包含時,爲什麼它會工作? – Chris 2010-06-29 14:10:18

回答

2

以下行似乎並不正確。它對未被初始化的東西使用strlen。因此,其結果將是不確定的:

ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1); 

使用,後來在電話中向atof()將很可能不會產生一致的結果。根據實際的分配大小,在存儲數據時可能會導致分段錯誤。

0

不是說這有什麼做的段錯誤,但:

ValuePtr = malloc(numParts * sizeof **ValuePtr); 

    ... 

InfoPtr = malloc(6 * sizeof **InfoPtr);