Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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_File_Io - Fatal编程技术网

C 如何读入文本文件并将其复制到字符串数组?

C 如何读入文本文件并将其复制到字符串数组?,c,file,io,C,File,Io,我试图打开一个名为dictorial.txt的文件,并将其内容复制到字符串数组中。但我忘了怎么做 以下是我目前掌握的情况: int main(int argc, char** argv) { char dict[999][15]; FILE *fp; fp = fopen("dictionary.txt", "r"); while(feof (fp)) { } } 有人能帮我吗

我试图打开一个名为
dictorial.txt
的文件,并将其内容复制到字符串数组中。但我忘了怎么做

以下是我目前掌握的情况:

int main(int argc, char** argv) 
{
    char dict[999][15];
    FILE *fp;
    fp = fopen("dictionary.txt", "r");
    while(feof (fp))
    {
            
    }
}
有人能帮我吗


编辑:文件中有999个单词,所有单词的长度都不超过15个字符。

如注释
中所述,(!feof(fp))
将无法正常工作,您也缺少否定符,但这与问题无关,一种简单的方法是使用
fgets
并利用其返回值检测文件的结尾,因为当没有更多行可读取时,它返回
NULL

#include <stdio.h>
#include <string.h>

int main()
{
    char dict[999][15];
    size_t ind; // store the index of last read line

    FILE *fp = fopen("dictionary.txt", "r");

    if (fp != NULL) // check if the file was succefully opened
    {
        // read each line until he end, or the array bound is reached
        for (ind = 0; fgets(dict[ind], sizeof dict[0], fp) && ind < sizeof dict / sizeof dict[0]; ind++)
        {
            dict[ind][strcspn(dict[ind], "\n")] = '\0'; // remove newline from the buffer, optional
        }

        for (size_t i = 0; i < ind; i++) // test print
        {
            puts(dict[i]);
        }
    }
    else
    {
        perror("Error opening file");
    }
}
#包括
#包括
int main()
{
字汇[999][15];
size\u t ind;//存储上次读取行的索引
文件*fp=fopen(“dictionary.txt”、“r”);
if(fp!=NULL)//检查文件是否已成功打开
{
//读每一行,直到他结束,或者到达数组边界
对于(ind=0;fgets(dict[ind],dict[0],fp)和&ind
请注意,这也将读取空行,,即仅包含
\n
的行或其他空白字符


如果成功扫描单词,则返回1。当它到达文件的末尾时,它不会。所以你也可以这样做。

while(feof(file))
没有意义,它将取决于输入文件的结构,但会很有用。@pmg这里实际上是
while(feof(fp))
。没有
而且情况更糟。是否希望最多999个单词的长度小于15个字符。。。或者最多15个单词,每个单词短于999个字符(正如您的代码所示)?@Kazumi411它应该可以完美地工作,我添加了一些代码来帮助您找出问题,我还添加了一个实时示例,检查它也要小心:
fgets()
会将读取的换行符放入缓冲区,因此,您可能希望在进一步处理之前删除它。@MikeCAT添加了一个选项来删除它,并注意到空行也将被解析。我刚刚发布了一个函数,如果您希望避免对字符串进行两次解析,则可以使用不带新行的fgets:欢迎您的见解@MikeCAT和Anastaciu注意问题中的编辑,更正数组维度顺序。@Clifford,谢谢。
#include <stdio.h>

int main()
{
    char dict[999][15];
    FILE * fp;
    fp = fopen("dictionary.txt", "r");

    int i = 0;
    while (fscanf(fp, "%s", dict[i]) != EOF)
            i++;

    // i is your length of the list now so you can print all words with
    int j;
    for (j = 0; j < i; j++)
            printf("%s ", dict[j]);

    fclose(fp);
    return 0;
}
fscanf(fp, "%s", dict[i]);