使用此 C 代碼,我想用readdir函式回傳的每個不同文本檔案中決議的單詞填充動態字符陣列。
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
char *getWord(FILE *fp){
char word[100];
int ch, i=0;
while(EOF!=(ch=fgetc(fp)) && !isalpha(ch))
;//skip
if(ch == EOF)
return NULL;
do{
word[i ] = tolower(ch);
}while(EOF!=(ch=fgetc(fp)) && isalpha(ch));
word[i]='\0';
return strdup(word);
}
int readWords(const char * fn,char ** arr){
char *word;
FILE * fp;
int size=0;
//read first time to get the number of words
fp=fopen(fn,"r");
while(word=getWord(fp)){
size ;
}
fclose(fp);
printf("size:%d\n",size);
arr = (char **) malloc (size * sizeof (char *));
//reopen to insert in dynamic array
fp=fopen(fn,"r");
for (int i = 0; i < size; i ){
arr[i] = (char *) malloc (100 * sizeof (char));
word=getWord(fp);
// printf("%s",word);
strcpy(arr[i],word);
}
fclose(fp);
return size;
}
int main(int narg, char *arg[] )
{
struct dirent *de; // Pointer for directory entry
char absdir[256];
char absfn[256];
FILE * F;
int s;
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(arg[1]);
strcpy(absdir,arg[1]);
strcat(absdir,"/");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL){
if (de->d_type!=4){ // is file?
strcpy(absfn,absdir);
strcat(absfn,de->d_name);
printf("abs_fn:%s \n",absfn);
char** arr_words;
s=readWords(absfn,&arr_words);
printf("%s number of words:%d\n", absfn,s);
for (int i=0;i<s;i )
printf("word %d:%s\n",i,arr_words[i]);
//free 2D array
for(int i=0;i<s;i )
free(arr_words[i]);
free(arr_words);
}
}
closedir(dr);
return 0;
}
這些詞被很好地決議,getWord但我找不到填充char **arr passed引數并在主函式中使用它的解決方案。我想char **arr_words 用以下形式動態分配
arr_words[0]=First word of file text
arr_words[1]=Second word
arr_words[s-1]=Last Word
該Readword函式回傳在每個檔案中讀取的字數。
謝謝
uj5u.com熱心網友回復:
您的錯誤是此處描述的更高級問題之一:動態記憶體訪問僅適用于函式內部
arr是區域變數。它是指向指標陣列中第一個指標的指標。為了將其回傳到 main(現在緊緊抓住),您的函式需要傳遞一個指向指標陣列中第一個指標的指標......
或者在普通的 C 中,可怕的三星級編程,char***. 但這實際上是對它的有效使用,關于存在的三個間接級別的唯一有效使用。
或者,您可以只使用 returnchar**并跳過這個額外的間接級別,這可能更具可讀性。
使用哪種形式有點主觀,但您需要將您的功能更改為以下之一:
int readWords(const char * fn, char*** arr)
{
...
*arr = malloc (size * sizeof (char *))
...
}
或者
char** readWords(const char * fn, int* wordsRead)
{
...
arr = malloc (size * sizeof (char *));
...
*wordsRead = size;
return arr;
}
uj5u.com熱心網友回復:
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
char *getWord(FILE *fp){
char word[100];
int ch, i=0;
while(EOF!=(ch=fgetc(fp)) && !isalpha(ch))
;//skip
if(ch == EOF)
return NULL;
do{
word[i ] = tolower(ch);
}while(EOF!=(ch=fgetc(fp)) && isalpha(ch));
word[i]='\0';
return strdup(word);
}
char** readWords(const char * fn,int *wordsRead){
char *word;
char**arr;
FILE * fp;
int size=0;
//read first time to get the number of words
fp=fopen(fn,"r");
while(word=getWord(fp)){
size ;
}
fclose(fp);
printf("size:%d\n",size);
arr = malloc (size * sizeof (char *));
//reopen to insert in dynamic array
fp=fopen(fn,"r");
for (int i = 0; i < size; i ){
arr[i] = (char *) malloc (100 * sizeof (char));
word=getWord(fp);
// printf("%s",word);
strcpy(arr[i],word);
}
fclose(fp);
*wordsRead = size;
return arr;
}
int main(int narg, char *arg[] )
{
struct dirent *de; // Pointer for directory entry
char absdir[256];
char absfn[256];
FILE * F;
int s;
int wordsRead;
char buffer[100];
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(arg[1]);
strcpy(absdir,arg[1]);
strcat(absdir,"/");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL){
if (de->d_type!=4){ // is file?
strcpy(absfn,absdir);
strcat(absfn,de->d_name);
printf("abs_fn:%s \n",absfn);
char **arr_words;
arr_words=readWords(absfn,&wordsRead);
printf("%s number of words:%d\n", absfn,wordsRead);
for (int i=0;i<wordsRead;i ){
printf("word %d:%s\n",i,arr_words[i]);
}
//free array of pointers
for(int i=0;i<s;i )
free(arr_words[i]);
free(arr_words);
}
}
closedir(dr);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/341253.html
上一篇:查找亂數的MAX和MIN
