Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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_Text_Multiline - Fatal编程技术网

C、 读取多行文本文件

C、 读取多行文本文件,c,file,text,multiline,C,File,Text,Multiline,我知道这是一个愚蠢的问题,但如何从多行文本文件加载数据 while (!feof(in)) { fscanf(in,"%s %s %s \n",string1,string2,string3); } ^^这就是我从一行加载数据的方式,它工作正常。我只是不知道如何从第二行和第三行加载相同的数据 我再次意识到这可能是一个愚蠢的问题 编辑:问题没有解决。我不知道如何从不在第一行的文件中读取文本。我该怎么做?抱歉这个愚蠢的问题。将\n放在scanf格式的字符串中与空格没有什么不同的效果。应该使

我知道这是一个愚蠢的问题,但如何从多行文本文件加载数据

while (!feof(in)) {
    fscanf(in,"%s %s %s \n",string1,string2,string3);
}
^^这就是我从一行加载数据的方式,它工作正常。我只是不知道如何从第二行和第三行加载相同的数据

我再次意识到这可能是一个愚蠢的问题


编辑:问题没有解决。我不知道如何从不在第一行的文件中读取文本。我该怎么做?抱歉这个愚蠢的问题。

\n
放在scanf格式的字符串中与空格没有什么不同的效果。应该使用FGET获取线,然后在字符串本身上使用sscanf

这也允许更容易的错误恢复。如果只是匹配换行符的问题,您可以在字符串末尾使用
“%*[\t]%*1[\n]”而不是
“\n”
。在这种情况下,您可能应该使用
%*[\t]
代替所有空格,并检查fscanf的返回值。直接在输入上使用fscanf是非常困难的(如果一行有四个单词会发生什么?如果只有两个单词会发生什么?),我建议使用fgets/sscanf解决方案

此外,正如德兰·阿扎巴尼所提到的。。。从这个片段中不清楚您是否已经这样做了,但是您必须定义空间[例如,在大型数组中或使用malloc的某个动态结构中]来存储整个数据集,或者在循环中执行所有处理


您还应该在格式说明符中指定每个字符串的可用空间<代码>%s本身在scanf中始终是一个bug,可能是一个安全漏洞。

每次调用
fscanf
,它都会读取更多的值。现在的问题是,你把每一行都读入相同的变量,所以最后,这三个变量都有最后一行的值。尝试创建一个数组或其他结构,可以保存所需的所有值。

尝试以下操作:

/edited/

或者甚至

char line[512];
in = fopen ("multilinefile.txt", "rt");  /* open the file for reading */
fgets(line, 512, in); // throw out line one

fgets(line, 512, in); // on line 2
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 2 is loaded into 'line'
// do stuff with line 2

fgets(line, 512, in); // on line 3
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 3 is loaded into 'line'
// do stuff with line 3

fclose(in); // close file

首先,你不用像那样使用feof()
,它显示了一个可能的帕斯卡背景,无论是在你的过去还是在你老师的过去

对于阅读行,最好使用POSIX 2008(Linux)
getline()
或标准C
fgets()
。无论哪种方式,您都可以尝试使用函数读取该行,并在它指示EOF时停止:

while (fgets(buffer, sizeof(buffer), fp) != 0)
{
     ...use the line of data in buffer...
}

char *bufptr = 0;
size_t buflen = 0;
while (getline(&bufptr, &buflen, fp) != -1)
{
    ...use the line of data in bufptr...
}
free(bufptr);

要阅读多行,您需要决定是否需要以前的行。如果不是,则使用单个字符串(字符数组)即可。如果您需要前面几行,那么您需要读入一个数组,可能是一个动态分配指针的数组。

我有一个更简单的解决方案,没有令人困惑的方法片段(不违反上述规定),它是:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
    string line;//read the line
    ifstream myfile ("MainMenu.txt"); // make sure to put this inside the project folder with all your .h and .cpp files

    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
           }
    else cout << "Unable to open file";
   return 0;
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串行;//读取该行
ifstream myfile(“main menu.txt”);//请确保将其与所有.h和.cpp文件一起放在项目文件夹中
如果(myfile.is_open())
{
while(myfile.good())
{
getline(myfile,line);

迫不及待,所以如果我需要,比如说,第二行,我会怎么做?谢谢你的帮助。看看编辑是否有效。fgets将逐行获取文件。编辑基本上只获得第三行。非常感谢,你的示例帮助了我。不幸的是,我仍然很困惑。具体来说,如果我想阅读第三行,我该怎么办还是例子?我意识到SCANF可能是一个漏洞,但这不是我所遇到的问题。我知道字符串对于文件类型来说是正确的大小,我需要知道从文件的第二行和第三行读取数据的方法,以及第一个文件的读取方法。我不知道如何做。这个答案是C++而不是C。
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
    string line;//read the line
    ifstream myfile ("MainMenu.txt"); // make sure to put this inside the project folder with all your .h and .cpp files

    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
           }
    else cout << "Unable to open file";
   return 0;