程序适用于字符串文字,但不适用于字符串数组

时间:2014-05-24 00:52:16

标签: c arrays string hashtable

我有一个哈希表ADT,它有两个函数,insert和lookup。我在插入函数中添加了一个哈希表,哈希表大小,ID#和书名,并将其插入到哈希表中。当我传递一个字符串文字,即insert(...,"Hello, world!"...);当我从文件中读取字符串,将它们存储在一个数组中,并尝试使用我的插入和查找函数时,它不起作用。 我这里有我的所有代码,但最重要的文件是main.c和hash.c. Hash.c有newHash(),hash(),insert()和lookup()函数,main.c从两个文件中读取,在本例中为test1.lib.in和test1.req.in,并且来自第一个file将从每行获取一本书的库ID和标题,然后将其放入哈希表中。从第二个文件中,它获得书名的请求,并应在其链接列表中打印ID。

有效的代码示例。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
#include "hash.h"

int main(){
    ListHndl* temp = newHash(10);
    insert(442440, "cvyaqbznxel", 10,temp);
    lookup(temp,"cvyaqbznxel", 10);
    return 0;
}

无法运作的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list.h"
#include "hash.h"


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

    if (argc != 3) {
        printf("Incorrect arguments, please specify 2 files to be read\n");
        return EXIT_FAILURE;
    }
    FILE *file = fopen( argv[1], "r");
    FILE *secondFile = fopen(argv[2], "r");
    if (file == 0 || secondFile == 0) {
        printf("Could not open a file\n");
        return EXIT_FAILURE;
    }
    int numDataLines2;
    int numDataLines;
    int hashTableSize;
    //First line of first file gives number of lines in file and 
    //size of hash table to be made
    if(fscanf(file, "%d%d", &numDataLines, &hashTableSize) < 2) {
        printf("Unable to parse first line of first file\n");
        return EXIT_FAILURE;
    } 
    ListHndl* theHash = newHash(hashTableSize);
    int libraryID;
    char *tempString = calloc(numDataLines,41*sizeof(char));
    char lineHolder[129];
    //discard the new line which always shows up
    fgets(lineHolder, 128, file);
    for(int i = 0; i < numDataLines; i++) {
        //Gets the whole line to be scanned with sscanf
        fgets(lineHolder, 128, file);

        //If the line consists of just a newline char, continue
        if(strcmp(lineHolder, "\n") == 0 ) {
            continue;
        }
        //Scans the line retrieved from fgets and placed in lineHolder
        if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID,&tempString[i]) == 0){
            printf("Unable to parse line %d of first file\n",i+2);
            return EXIT_FAILURE;
        }
        insert(libraryID, &tempString[i], hashTableSize, theHash);
    }
    char String[41];
    fgets(String, 40, secondFile);
    numDataLines2 = atoi(String);
    char *storeSecondFileStuff = calloc(numDataLines2,41*sizeof(char));
    for(int i = 0; i< numDataLines2; i++) {
        fgets(lineHolder, 128, secondFile);
        if(strcmp(lineHolder, "\n") == 0) {
            continue;
        }
        if(sscanf(lineHolder, "%40[^\n]",&storeSecondFileStuff[i]) == 0) {
            printf("Unable to parse line %d of second file\n",i+2);
            return EXIT_FAILURE;
        }
        lookup(theHash, &storeSecondFileStuff[i], hashTableSize);
    }
    printf("\n");
    fclose(file);
    fclose(secondFile);
    return 0;
}

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你有很多问题。首先,您可能无法正确扫描输入行。改变你的行

 if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID,&tempString[i]) == 0)

 if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID, tempString) < 0)

这样,您将捕获sscanf函数未成功转换两个参数的情况 - 例如,如果输入行中没有逗号。请注意sscanf会返回成功转化的次数;成功将返回2的值,因此测试<2是正确的方法。

另请注意,我已将&tempString[i]更改为tempString。前者指向tempString的某个位置 - 只分配了41个字符。然而,你总是允许最多40个字符(加上'\0'写入它 - 所以你将写入字符串的结尾。因为这只是一个临时变量,所以这样做是没有意义的。输入temp变量,然后做你需要做的任何事情。

这意味着您的insert也会从

更改
    insert(libraryID, &tempString[i], hashTableSize, theHash);

    insert(libraryID, tempString, hashTableSize, theHash);

同样,你需要在你的代码中做同样的事情。

这是尝试让代码为您工作 - 看看这是否适合现场。请注意,我真正做的就是更改tempStringstoreSecondFileStuff的类型,并相应地修改它们在各种函数调用中的使用方式。我没有尝试编译/运行,因为涉及的其他文件的复杂性 - 但这应该有所帮助:

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

    if (argc != 3) {
        printf("Incorrect arguments, please specify 2 files to be read\n");
        return EXIT_FAILURE;
    }
    FILE *file = fopen( argv[1], "r");
    FILE *secondFile = fopen(argv[2], "r");
    if (file == 0 || secondFile == 0) {
        printf("Could not open a file\n");
        return EXIT_FAILURE;
    }
    int numDataLines2;
    int numDataLines;
    int hashTableSize;
    //First line of first file gives number of lines in file and 
    //size of hash table to be made
    if(fscanf(file, "%d%d", &numDataLines, &hashTableSize) < 2) {
        printf("Unable to parse first line of first file\n");
        return EXIT_FAILURE;
    } 
    ListHndl* theHash = newHash(hashTableSize);
    int libraryID;
    char **tempString = calloc(numDataLines,sizeof(char*));  // <<< ARRAY of pointers
    char lineHolder[129];
    //discard the new line which always shows up
    fgets(lineHolder, 128, file);
    for(int i = 0; i < numDataLines; i++) {
        //Gets the whole line to be scanned with sscanf
        fgets(lineHolder, 128, file);
        tempString[i] = calloc(1, 41 * sizeof(char)); // <<< space for this string
        //If the line consists of just a newline char, continue
        if(strcmp(lineHolder, "\n") == 0 ) {
            continue;
        }
        //Scans the line retrieved from fgets and placed in lineHolder
        if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID, tempString[i]) < 0){ // <<< changed
            printf("Unable to parse line %d of first file\n",i+2);
            return EXIT_FAILURE;
        }
        insert(libraryID, tempString[i], hashTableSize, theHash); // <<< changed
    }
    char String[41];
    fgets(String, 40, secondFile);
    numDataLines2 = atoi(String);
    char **storeSecondFileStuff = calloc(numDataLines2, sizeof(char*)); // changed: again char **
    for(int i = 0; i< numDataLines2; i++) {
        fgets(lineHolder, 128, secondFile);
        storeSecondFileStuff[i] = calloc(1, 41 * sizeof(char));
        if(strcmp(lineHolder, "\n") == 0) {
            continue;
        }
        if(sscanf(lineHolder, "%40[^\n]",storeSecondFileStuff[i]) == 0) {
            printf("Unable to parse line %d of second file\n",i+2);
            return EXIT_FAILURE;
        }
        lookup(theHash, storeSecondFileStuff[i], hashTableSize); // <<<< changed
    }
    printf("\n");
    fclose(file);
    fclose(secondFile);
    return 0;
}