【发布时间】:2012-10-11 06:42:51
【问题描述】:
这个函数的任务相当简单。给定一个 char* 数组、一个指向文件的指针和一个最大字长,它会读取文件并将每个字一个接一个地复制到 char* 数组中。由于文件中每行一个单词,因此使用\n 作为单词之间的分隔符是有意义的。所以考虑到这一点,代码应该很容易解释:
void loadDictionary(char* strDictionary[], FILE* filePointer, int nMaxLetters)
{
int nNameCount= 0, nCursor = 0;
char* strCurrent;
char cCurrent;
//allocate space for a word
strCurrent = malloc(nMaxLetters * sizeof(char));
while ((cCurrent = fgetc(filePointer)) != EOF) {
if(cCurrent != '\n')
{
strCurrent[nCursor] = cCurrent;
nCursor++;
} else { //then we've reached the end of the line (word)
//add null termination to string
strCurrent[nCursor] = '\0'; //SEG FAULT
//copy string to dictionary
memcpy(strDictionary[nNameCount], strCurrent, strlen(strCurrent)+1);
//increment count
nNameCount++;
//reset the cursor
nCursor = 0;
}
}
}
此代码在我调用strCurrent[nCursor] = '\0'; 的行处生成分段错误。我不知道为什么,因为从表面上看,这个操作似乎应该与另一个块中的操作没有什么不同,我称之为strCurrent[nCursor] = cCurrent;。 strCurrent 应该分配了足够多的空间来存储所有必要的字符。所以,我有些不知所措。帮我解决这个问题,伙计们。
注意:我认为使用fgets 而不是fgetc 来完成这项任务可能会更轻松。我很可能会转向那个;但是,由于我遇到了一个我不理解的错误,所以我不想在我理解它之前将其搁置。
编辑:
有人指出memcpy操作可能出现错误,可能是strDictionary分配不当所致。这是分配strDictionary 的main 块。也许我犯了一个错误:
int main(int argc, char* argv[])
{
char** strDictionary;
FILE* filePointer;
int nResults = 0, nLines = 0, nNumLines, nMaxChars, i;
filePointer = fopen("dictionary.txt", "r");
//obtain the number of lines and the maximum word size of the dictionary
countLines(filePointer, &nNumLines, &nMaxChars);
//allocate memory for strDictionary
strDictionary = malloc(nNumLines * nMaxChars * sizeof(char));
printf("%d words in dictionary. Longest word is %d letters\n",
nNumLines, nMaxChars);
//Output here correctly prints: 1000 and 21
//reset the file pointer (not sure if this is a necessary step, but oh well)
filePointer = fopen("dictionary.txt", "r");
//load dictionary into memory
loadDictionary(strDictionary, filePointer, nMaxChars);
for (i=0; i<10; i++)
printf("%dth element of dictionary: %s\n", i, strDictionary[i]);
return 0;
}
编辑 2:
好的,我决定使用fgets() 而不是fgetc() 来大大简化我的功能。我还为strDictionary 做了我认为正确的malloc() 操作。但是,我仍然遇到段错误。这是更新的代码:
void loadDictionary(char* strDictionary[], FILE* filePointer, int nMaxLetters)
{
printf("Call to loadDictionary. nMaxLetters = %d\n", nMaxLetters);
int nWordCount= 0, nCursor = 0;
char* strCurrent;
char cCurrent;
strCurrent = malloc(nMaxLetters); //allocate space for a word
while (fgets(strCurrent, nMaxLetters, filePointer) != NULL)
{
memcpy(strDictionary[nWordCount], strCurrent, strlen(strCurrent)+1);
nWordCount++;
}
}
int main(int argc, char* argv[])
{
char** strDictionary;
FILE* filePointer;
int nResults = 0, nLines = 0, nNumLines, nMaxChars, i;
filePointer = fopen("dictionary.txt", "r");
//count the lines in the file (works fine)
countLines(filePointer, &nNumLines, &nMaxChars);
//allocate space for the dictionary
strDictionary = malloc(nNumLines * sizeof(char*));
for (i = 0; i<nLines; i++)
strDictionary[i] = malloc(nMaxChars * sizeof(char));
printf("%d words in dictionary. Longest word is %d letters\n",
nNumLines, nMaxChars);
//load dictionary into array
filePointer = fopen("dictionary.txt", "r");
loadDictionary(strDictionary, filePointer, nMaxChars);
for (i=0; i<10; i++)
printf("%dth element of dictionary: %s\n", i, strDictionary[i]);
return 0;
}
【问题讨论】:
-
您可能想在 'nCursor' 出现段错误时检查它的值。 ps 没有必要做 sizeof(char) 它是在 C 中定义的唯一大小
-
你知道它是否在它读入的第一个单词上吗?最大行有多长,nMaxLetters 有多大?
-
可能您的代码在包含 nMaxLetters 的行上出现了段错误,而您没有考虑到还包括终止空字符。
-
我怀疑这可能是它出现段错误的
memcpy(strDictionary[nNameCount], strCurrent, strlen(strCurrent)+1);行。您是否为strDictionary的所有元素分配了足够的空间? -
我怀疑
malloc(nMaxLetters * sizeof(char));您没有为空字节终止符分配 +1。
标签: c arrays segmentation-fault