[英]How to read words from a text file and add to an array of strings?
这是我调用的函数: getWord(words);
void getWord(char words[][MAXWORDLENGTH]){
int i;
char newWord[MAXWORDLENGTH];
FILE* file;
file = fopen("wordbank.txt","r");
if(file==NULL){
printf("Error\n");
}else{
printf("File read\n");
for(i=0;i<=MAXWORDS;i++){
fgets(newWord, "%s", file);
printf("newWord: %s", newWord);
strcpy(words[i], newWord);
}
}
fclose(file);
}
不幸的是,我的数组“单词”没有用字符串填充每个元素,它能够将文本文件中的第一个单词捕获到[0]元素中,然后其余单词变得乱七八糟。
我的文本文件如下所示:
apple
winter
summer
skunk
clouds
pencil
grape
对我来说,使用指针而不是数组会更容易吗?
谢谢您的帮助!
好吧,您可能使事情变得比他们需要的困难。 该评论并不是要接受您的意见,但是您正在使用的任何编译器/ IDE都应该左右散布错误。 它完全编译的事实令人惊讶。 评论中的建议是正确的。 始终, 始终编译启用的警告并修复所有警告,然后再认为您的代码可信赖。
话虽这么说,但您有两个方面让您感到困难。 您应该传递数组和FILE*
指针(或文件名),而不是对函数中的文件名进行硬编码。 这使得功能的使用非常有限。 您现在所知道的,使用fgets
是不正确的。 此外,在传递数组后,只需确保将每个单词(假设每行1个)读入数组,同时确保不超过声明的行数即可。
这是一个简短的示例,将从命令行上给出的文件名(或默认情况下从stdin
)读取。 它利用ternary
运算符来接受文件名作为第一个参数,或者将fp
设置为stdin
。 试试看,如果您有任何疑问,请告诉我:
#include <stdio.h>
#define MAXW 64 /* maximum number of lines to read */
#define MAXC 32 /* longest word in abridged Dict. is 28 char
"Antidisestablishmentarianism" */
size_t getwords (char (*words)[MAXC], FILE *fp);
int main (int argc, char **argv) {
char words [MAXW][MAXC] = {{0}}; /* array to hold words */
size_t nwords = 0; /* number of words read */
size_t i;
/* open argv[1] for reading (default: stdin) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
nwords = getwords (words, fp);
if (fp != stdin) fclose (fp); /* close file */
printf ("\n words read from '%s':\n\n",
argc > 1 ? argv[1] : "stdin");
for (i = 0; i < nwords; i++)
printf (" words[%2zu] : %s", i, words[i]);
return 0;
}
/* function to read words, 1 - per line, from 'fp' */
size_t getwords (char (*words)[MAXC], FILE *fp)
{
size_t idx = 0; /* index of words read */
/* read each line in file into words array
note: includes trailing newline character */
while (idx < MAXW && fgets (words[idx], MAXC, fp)) {
idx++;
/* note you should check if chars remain in line */
}
if (idx == MAXW) /* check word count against MAXW */
fprintf (stderr, "warning: MAXW words read.\n");
return idx;
}
编
gcc -Wall -Wextra -O3 -o bin/fgets_array_words_fn fgets_array_words_fn.c
输入文件
$ cat dat/captnjack1.txt
This
is
a
tale
Of
Captain
Jack
Sparrow
A
Pirate
So
Brave
On
the
Seven
Seas.
产量
$ ./bin/fgets_array_words_fn dat/captnjack1.txt
words read from 'dat/captnjack1.txt':
words[ 0] : This
words[ 1] : is
words[ 2] : a
words[ 3] : tale
words[ 4] : Of
words[ 5] : Captain
words[ 6] : Jack
words[ 7] : Sparrow
words[ 8] : A
words[ 9] : Pirate
words[10] : So
words[11] : Brave
words[12] : On
words[13] : the
words[14] : Seven
words[15] : Seas.
或从stdin
读取:
$ ./bin/fgets_array_words_fn <dat/captnjack1.txt
words read from 'stdin':
words[ 0] : This
words[ 1] : is
...
使用fread()方法
while(fread(newWord, MAXWORDLENGTH,1,file)){
printf("newWord: %s", newWord);
strcpy(words[i], newWord);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.