2016-09-16 80 views
1

獲得一個無限循環,但是當我嘗試「//」了兩個圈,我開始越來越FSEEK()賽格錯誤...文件的無限循環/分割錯誤

目的...:要找到共同的文件之間的短語(詞組分組)。

#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define CONTENTS 20000 
#define MAXFILES 30 
#include "hash.h" 


void printOutput(int numFiles, int output[30][30]) 
{ 
    int i, j; 
//print output matrix 
for (i = 0; i < numFiles; i++){ 
    printf("f%d ", i + 1); // print upper row of file names, use 4 spaces 
} 

for (i = 0; i < numFiles; i++){ 
    if (i > 8) // if file marker is 2 digits, use 2 spaces 
    { 
     printf("f%d ", i + 1); // print upper row of file names 
    } 
    else printf("f%d ", i + 1); // else file marker is 1 digit, use 3 spaces 

    printf("\n"); 
    for (j = 0; j < numFiles; j++){ 
     // the following if else statements manage the spacing for various numbers, soley for aesthetic purposes 
     if (output[i][j] == 45){ // print the dash character 
      printf("%c  ", output[i][j]); // use 5 spaces 
      continue; 
     } 
     else if (output[i][j] > 9 && output[i][j] < 100){ // if 2 digit number, 4 spaces 
      printf("%d ", output[i][j]); 
      continue; 
     } 
     else if (output[i][j] > 99 && output[i][j] < 1000){ // if 3 digit number, 3 spaces 
      printf("%d ", output[i][j]); 
      continue; 
     } 

     else if (output[i][j] > 999 && output[i][j] < 10000){ // if 4 digit number, 2 spaces 
      printf("%d ", output[i][j]); 
      continue; 
     } 
     else if (output[i][j] > 9999){ // if 5 digit number, 1 space 
      printf("%d ", output[i][j]); 
      continue; 
     } 
     else printf("%d  ", output[i][j]); // 1 digit number, 5 spaces 
    } 
    printf("\n"); 
} 
} 


int main(int argc, char *argv[]) 
{ 
    int group_length, valid; 
    int list[30]; 
    //Range checking 
    while(valid == 0) 
{ 
    printf("What is the number of words to analyze (2 - 10)?\n"); 
    scanf("%d",&group_length); 

    if((group_length < 2) || (group_length > 10)) 
     printf("\n"); 
    else 
     valid = 1; 
    } 

    struct node * Map[30]; 
    int f; 
    for(f = 0; f < 30; f++) 
    { 
    Map[f] = createHash(2000); 
    } 

    FILE *fp; 
    int i, numberF = 0; 
    int output[30][30]; // upper triangular output matrix 
    char name[CONTENTS]; 
    char fnames[CONTENTS]; 
    fp = fopen("inputfile.txt", "r"); 
    char *names = malloc(sizeof *names); 
    int *SequenceList = malloc(sizeof *SequenceList); 
    //int *SequenceList malloc(MAXFILES * sizeof(int *)); 
    int seq; //Sequence 
char entireFile[CONTENTS]; 
char *wordArray[CONTENTS]; 
char *token2, *search = " \r\n\t"; 

for (i = 0; fgets(name, 100, fp) != NULL && i < MAXFILES; i++) 
{ 
    ++numberF; 
    char *token = NULL; //setting to nukl before using it to strtok 
    token = strtok(name, ":"); 
    strtok(token, "\n");//Getting rid of that dirty \n that I hate 
    strcat(&fnames[i], token); 
    //Part 2: 
    FILE *fpp; 
    fpp = fopen(fnames,"r"); 
    fseek(fpp, 0, SEEK_END); 
    int inputLength = ftell(fpp); 
    rewind(fpp); 
    //Reads data from a given stream into an array pointed to 
    fread(entireFile, inputLength, 1, fpp); 
    fclose(fpp); 
    //Closing file 
    token2 = strtok(entireFile, search); 
    seq = 0; 
    //wordArray[seq] = token2; 
    char temp[CONTENTS]; 

    while (token2 != NULL) 
    { 
     while(seq < group_length) 
    { 
     token2 = strtok(NULL, search); 
     wordArray[seq] = token2; 
     strcpy(temp, wordArray[seq]); 
     insertHash(Map[i], temp, SequenceList[i]); 
     seq++; 
     //output[i] = Map[i]->counts[i]; 
    } 
    }//End of token2 NULL loop 

    } 

    //closing inputfile.txt... Presuming now is best time 
    fclose(fp); 

    //Printing and calling printOutput function 
    int k, l; 
    for (k = 0; k < 30; k++) 
     for (l = 0; l < 30; l++) 
     output[k][l] = 45;  
    printOutput(numberF, output); 

return 0; 

}

的正是我談論...摘錄:

我hash.c的
FILE *fpp; 
    fpp = fopen(fnames,"r"); 
    fseek(fpp, 0, SEEK_END); 
    int inputLength = ftell(fpp); 
    rewind(fpp); 
    //Reads data from a given stream into an array pointed to 
    fread(entireFile, inputLength, 1, fpp); 
    fclose(fpp); 
    //Closing file 
    token2 = strtok(entireFile, search); 
    seq = 0; 
    //wordArray[seq] = token2; 
    char temp[CONTENTS]; 

    while (token2 != NULL) 
    { 
    while(seq < group_length) 
    { 
     token2 = strtok(NULL, search); 
      wordArray[seq] = token2; 
     strcpy(temp, wordArray[seq]); 
     insertHash(Map[i], temp, SequenceList[i]); 
     seq++; 
     //output[i] = Map[i]->counts[i]; 
    } 
    }//End of token2 NULL lo 

代碼(請確保它永遠不會過期,是公開的):http://pastebin.com/Cz4R7WwK //更新 其頭:http://pastebin.com/ex2zARGt

編輯: 獲得一個新賽格錯誤最新增加的後.. 我在hash.c 這裏insertHash的CMP部分入門賽格錯誤是在警告insertHash

scratch_Hash.c: In function ‘insertHash’: 
scratch_Hash.c:47:4: warning: passing argument 1 of ‘hash’ discards ‘const’ qual ifier from pointer target type [enabled by default] 
ha = hash(info)%200; 
^ 
scratch_Hash.c:31:15: note: expected ‘unsigned char *’ but argument is of type  const char *’ 
unsigned long hash(unsigned char *str) 
        ^
scratch_Hash.c:57:12: warning: assignment from incompatible pointer type [enable d by default] 
    node = node->next; 
     ^
scratch_Hash.c:64:18: warning: assignment from incompatible pointer type [enable d by default] 
newNode->next = table[ha]; 

前兩位以後我可以做,但擔心incompatile指針類型。在這種情況下我如何解決這個問題?

現貨直銷,其中賽格錯誤是...:

while(node != NULL) 
    { 
    if (strcmp(node->data,info) == 0) 
    {  
     node->counts[file]++; 
     return node; 
    } 
    node = node->next; 
} 
+0

添加警告編譯時會幫你 –

+0

一旦SEQ == group_length外,同時循環運行下去。爲什麼在那裏? – stark

回答

0

有一些問題與分配。在你struct node定義你有

int counts[30]; 

但是當你createHash(2000)您正在訪問多達數[1999]。你可能打算讓計數指針像

int *counts; 

和createHash

newTable->counts = malloc(size * sizeof(int)); 

而且分配它,你在堆上分配只分配用於指針足夠的內存,而不是結構本身。例如,在hash.c

struct node * newTable = malloc(sizeof *newTable); 

將分配足夠的存儲器用於一個指針,而不是一個struct node。你可能想改用

struct node * newTable = malloc(sizeof(struct node)); 

對於無限循環的問題,insertHash的第一個參數是struct node**型的,但你傳遞一個struct node*。你想使用

insertHash(Map, ... 

編譯器警告應該指出這些問題。最後,在你的node定義,定義

struct Node *next; 

但你要

struct node *next; 
+0

是的,我可能應該只是把它做成30號......因爲我真的只需要30.我意外地把多少個字符數而不是最大文件數創建爲用於計數的createHash。所以我會留在int計數[30]。謝謝。對不起,像下面的struct node *這樣愚蠢的錯誤。沒有抓住它。並且insertHash(map ...)不是insertHash(map [i],...)似乎擺脫了那個警告,但是如果我寫了它,仍然可以通過這種方式獲得我想要的東西嗎?當我在createHash中創建struct node * newTable = maloc(sizeof(node));我得到一個錯誤,說節點未聲明 – anchorman

+0

修正:struct node * newTable = malloc(sizeof(struct node)); – anchorman

+0

更新了缺少結構的答案。你需要做其他的改變來使insertHash(Map,...工作,就像傳遞i一樣。你也可以把函數聲明改成insertHash(struct node * node,...),它將處理調用insertHash(Map [i ],...) –