Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从文本文件中读取单词并添加到字符串数组中?_C_Arrays - Fatal编程技术网

C 如何从文本文件中读取单词并添加到字符串数组中?

C 如何从文本文件中读取单词并添加到字符串数组中?,c,arrays,C,Arrays,这是我的函数,它通过以下方式调用:getWord(words) 使用指针而不是数组会更容易吗 谢谢你的帮助 使用fread()方法 好吧,你可能让事情变得比需要的更困难。这条评论并不是要挑你的毛病,但是无论你使用什么编译器/IDE,都应该会左右地抛出错误。事实上,它的汇编是令人惊讶的。评论中的建议是合理的。在您认为代码值得信任之前,请始终编译启用警告并修复所有警告。 也就是说,你有几个地方让你自己很难。您应该传递数组和文件*指针(或文件名),而不是在函数中硬编码文件名。这使得该功能的使用非常有限

这是我的函数,它通过以下方式调用:
getWord(words)

使用指针而不是数组会更容易吗

谢谢你的帮助

使用fread()方法


好吧,你可能让事情变得比需要的更困难。这条评论并不是要挑你的毛病,但是无论你使用什么编译器/IDE,都应该会左右地抛出错误。事实上,它的汇编是令人惊讶的。评论中的建议是合理的。在您认为代码值得信任之前,请始终编译启用警告并修复所有警告。

也就是说,你有几个地方让你自己很难。您应该传递数组和
文件*
指针(或文件名),而不是在函数中硬编码文件名。这使得该功能的使用非常有限。正如您现在所知,您对fgets的使用是不正确的。此外,在传递数组后,只需将每个单词(假设每行1个)读入数组,同时确保不会超过已声明的行数

下面是一个简短的示例,它将从命令行上给定的文件名(或默认情况下从
stdin
读取)中读取。它使用
三元
运算符接受文件名作为第一个参数,或将
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.
或从标准DIN中读取:

$ ./bin/fgets_array_words_fn <dat/captnjack1.txt

 words read from 'stdin':

  words[ 0] : This
  words[ 1] : is
  ...

$./bin/fgets\u数组\u单词\u fn错误使用
fgets()
<代码>fgets(新词,“%s”,文件)-->
fgets(新词、新词大小、文件)此代码忽略了多少编译器警告?始终使用
-Wall-Wextra
编译,并在考虑完成代码之前修复所有警告。@DavidC.Rankin我单击了这些编译器标志的框,它们删除了2个警告,是否应该添加更多警告,因为我的代码错误?@chux修复了我尝试打印单词[I]时出现的乱码打印问题,然后是i++,虽然它打印了我的第一个单词,后面是3个空行,然后是下一个单词,就这样。似乎让我的问题变得更糟了。这太棒了!谢谢当然,很乐意帮忙。另请注意,当将2D数组传递给函数时,您可以将其传递为,例如
myfunction(char array[][width],…)
myfunction(char(*array)[width],…)
,但无论如何,必须始终将宽度声明为函数参数的一部分。然后,在代码主体中,只需使用数组名称调用函数,例如
myfunction(array…)
。只知道原因是作为参数传递时,第一级间接寻址转换为指针(这是另一天的解释)是的,我花了一整天的时间在库中试图找出我的2d数组混乱的原因,最后在大约4个小时后才发现我需要函数参数中的宽度。
$ 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.
$ ./bin/fgets_array_words_fn <dat/captnjack1.txt

 words read from 'stdin':

  words[ 0] : This
  words[ 1] : is
  ...