2013-04-12 49 views
0

這裏是我的代碼:,適用於一個文件,而不是另一個

 char delims[4]; 
     delims[0]='\t'; 
     delims[1]=' '; 
     delims[2]=','; 
     delims[3]='\0'; 

     i = 0; 
     while (fgets(line, 10000, fp) != NULL) 
     { 

     result = strtok(line,delims); 

     while(result != NULL) { 
      (*data_array)[i++] = atof(result); 
      result = strtok(NULL, delims); 
     } 
     } 

足夠直截了當。它完全適用於以下文件:

 3.600000 79.000000 
     1.800000 54.000000 
     3.333000 74.000000 
     2.283000 62.000000 
     4.533000 85.000000 
     2.883000 55.000000 
     4.700000 88.000000 

但它並不適用於這個文件的工作:

 3.6 79 3 
     1.8 54 3 
     3.333 74 3 
     2.283 62 1 
     4.533 85 1 
     2.883 55 1 
     4.7 88 2 
     3.6 85 1 
     1.95 51 1 
     4.35 85 3 

我也得到一個「中止(核心轉儲)」的錯誤。我究竟做錯了什麼?

編輯:這是整個功能。 * data_array和* data_labels只是在main()中聲明的。我註釋掉一些data_labels部分只是讓我能得到的第一部分工作:

int getdata(double* *data_array, int* *data_labels, int argc, char *argv[], int *items, int *attr) 
    { 
     // filename variables 
     char *filename;   // pointer to a string that will contain the name of the training data file. 
     char *result = NULL; // used with strtok() to extract each feature value given a line of delimited features. 
     FILE *fp;    // pointer to FILE, we can use this with fgets to access each line 
     char line[10000];  // array of 1000 chars for storing the raw data for one observation 
     char delims[4];   // an array of common delimiters for data files 
     delims[0]='\t'; 
     delims[1]=' '; 
     delims[2]=','; 
     delims[3]='\0'; 

     int i, j; 

     // check that we have the correct number of command line arguments 
     if (argc < 2) 
     { 
     printf("2usage: progname filename\n"); 
     return -1; 
     } 

     if (argc < 4) 
     { 
     printf("3usage: progname filename num_labels k(nn)\n"); 
     return -1; 
     } 

     if (atoi(argv[2]) < 1) 
     { 
     printf("num_labels must be a positive integer.\n"); 
     return -1; 
     } 

     if (atof(argv[2]) - atoi(argv[2]) > 0) 
     { 
     printf("num_labels must be an integer.\n"); 
     return -1; 
     } 

     if (atoi(argv[3]) < 1) 
     { 
     printf("k must be a positive integer.\n"); 
     return -1; 
     } 

     if (atof(argv[3]) - atoi(argv[3]) > 0) 
     { 
     printf("k must be an integer.\n"); 
     return -1; 
     } 

     // try to open the file 
     filename = argv[1]; 
     fp = fopen(filename, "r"); 
     if (fp == NULL) 
     { 
     printf("could not open file: %s\n", filename); 
     printf("note: the filename should be the second command line argument, after the .exe file"); 
     return -1; 
     } 

     printf("reading file: %s\n", filename); 

     // get first line of the file to get num_items and num_attrs. 
     fgets(line, 1000, fp); 
     sscanf(line, "%d \t %d", items, attr); 
     printf("num items: %d\n", *items); 
     printf("num attributes: %d\n", *attr); 

     if (atoi(argv[3]) > *items) 
     { 
     printf("k should be smaller than the number of items in the input file.\n"); 
     return -1; 
     } 

     // create an array of the data 
     *data_array = malloc(*items* *attr*sizeof(double)); 
     *data_labels = malloc(*items*sizeof(int)); 
     printf("data array size = %d\n\n",*items* *attr); 

     i=0; 
     j=0; 

     while (fgets(line, 10000, fp) != NULL) 
     { 

     // we break line into tokens using our delimeters list declared at the beginning of the function 
     result = strtok(line,delims); 
     //printf("%d\n",i); 

     while(result != NULL) { 
      (*data_array)[i++] = atof(result); 
      //printf("%f\n",(*data_array)[i-1]); 
      result = strtok(NULL, delims); 
     } 
     //(*data_labels)[j++] = (int)((*data_array)[--i]); 
     } 
     /* 
     printf("j=%d,items=%d\n",j,*items); 
     for (i=0;i<*items;i++) 
     { 
     printf("i=%d,items=%d,",i,*items); 
     printf("label=%d\n",(*data_labels)[i]); 
     } 
     // close the file 
     fclose(fp); 

     return 0; 
    } 
+0

'* data_array)'的大小和類型是什麼? –

+1

發佈一個小的,完整的,可編譯的程序來演示問題。一種可能性是你的'data_array'的大小不正確,但是不能從你發佈的內容中真正知道。 –

+0

我刪除了我的答案,因爲我猜的錯誤是你的表達式'(* data_array)[i ++] = atof(result);'是正確的,你可能有長度問題。那就是緩衝區溢出以避免在賦值之前添加一個檢查'i <(* items * * attr * sizeof(double)')。 –

回答

1

(*data_array)可以容納的元素數量少於您要在循環中填充的令牌數量。

+0

我討厭這是一個簡單的錯誤。謝謝。 – user1956609

1

你的strtok通話還好吧 - 我會用等價

char *delims = "\t ,"; 

取代delims初始化和切換重新進入strtok_r,但這不是導致崩潰的原因(除非您處於併發環境中,在這種情況下,崩潰應該是隨機的,並且大部分與正在解析的文件無關)。

這條線是高度可疑:

(*data_array)[i++] = atof(result); 

你要麼沒有分配給陣列足夠的空間由data_array指針指向,或者data_array本身是無效的。

相關問題