我有一个函数,它接受一个文件,逐行读取它,将每一行放入 a*char[]
中,将此二维数组放入一个结构中,然后返回此结构:wordlist.h:
#ifndef H_WORDLIST
#define H_WORDLIST
typedef struct {
char **chWordsList;
int listlen;
}Wordlist;
Wordlist getWordlistFromFile(char *chFilename);
char *getRandomWord();
#endif
功能(加上标题):
#include "wordlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define WORDSIZE 100
Wordlist getWordlistFromFile(char *chFilename){
FILE *file = fopen(chFilename,"r");
if (file == NULL){
printf("Unable to open file %s. Check if the file exists and can be read by this user.\n",chFilename);
exit(1);
}
char chWord[WORDSIZE];
int intFileSize = 0;
//First: coundt the amount of lines in the file
while((fgets(chWord,WORDSIZE,file) != NULL)){
++intFileSize;
}
rewind(file);
char *chWordList[intFileSize];
for (int count = 0; (fgets(chWord,WORDSIZE,file) != NULL); ++count){
chWordList[count] = malloc( strlen(chWord +1));
strcpy(chWordList[count],chWord);
chWordList[count][strlen(chWord) -1] = 0;
}
fclose(file);
Wordlist wordlist;
wordlist.chWordsList = chWordList;
wordlist.listlen = intFileSize;
for (int i = 0; i < wordlist.listlen; ++i){
printf("%s\n", wordlist.chWordsList[i]);
}
return wordlist;
}
到目前为止,这很好用。最后一个 for 循环准确地打印给定文件的每一行,所有完全预期的行为,完美无缺。现在,我实际上想使用该功能。所以:在我的 main.c 中:
Wordlist list = getWordlistFromFile(strFilePath);
for (int i = 0; i < list.listlen; ++i){
printf("%s\n", list.chWordsList[i]);
}
这给了我最奇怪的输出:
abacus
wordlist
(null)
(null)
��Ⳏ
E����H�E
gasses
虽然输出应该是:
abacus
amused
amours
arabic
cocain
cursor
gasses
在我看来,有些指针几乎得到free
了 d 或其他东西,而另一些则保持不变。到底是怎么回事?为什么wordlist
回归前完美,后破碎?