2011-04-29 132 views
2

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

while (!feof(in)) { 
    fscanf(in,"%s %s %s \n",string1,string2,string3); 
} 

^^这是我如何从一行加载数据,它工作正常。我只是不知道如何从第二行和第三行加载相同的数据。

同样,我意识到这可能是一个愚蠢的问题。

编辑:问题没有解决。我不知道如何从不在第一行的文件读取文本。我将如何做到这一点?对不起,这个愚蠢的问题。

+0

我仍然不幸的是,困惑。具体而言,例如,如果我想阅读第三行,我该怎么办? – 2011-04-29 05:06:33

回答

4

试着这么做:

/编辑/

char line[512]; // or however large you think these lines will be 

in = fopen ("multilinefile.txt", "rt"); /* open the file for reading */ 
/* "rt" means open the file for reading text */ 
int cur_line = 0; 
while(fgets(line, 512, in) != NULL) { 
    if (cur_line == 2) { // 3rd line 
    /* get a line, up to 512 chars from in. done if NULL */ 
    sscanf (line, "%s %s %s \n",string1,string2,string3); 
    // now you should store or manipulate those strings 

    break; 
    } 
    cur_line++; 
} 
fclose(in); /* close the file */ 

,或者甚至...

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 
+0

等等,所以如果我需要得到第二条线,我该怎么做?谢谢你的帮助。 – 2011-04-29 04:57:29

+0

看看编辑是否有效。 fgets将逐行获取文件。 编辑基本上只是获得第三行。 – jiman 2011-04-29 21:22:50

+0

非常感谢,你的榜样帮助我。 – 2011-04-29 22:13:38

3

\n设置为scanf格式的字符串对空间没有不同的影响。您应该使用fgets获取该行,然后使用字符串本身的sscanf。

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

另外,正如Delan Azabani所说......从这个片段中不清楚你是否已经这样做了,但是你必须定义空间[例如,在一个大型数组或一些带有malloc的动态结构中]来存储整个数据集,或者在循环中执行所有处理。

您还应该指定格式说明符中每个字符串有多少可用空间。在scanf中本身就是一个缺陷,可能是一个安全漏洞。

+0

我意识到scanf可能是一个漏洞,但这不是我的问题。我知道字符串对于文件类型是正确的大小,我需要知道从文件的第二行和第三行以及第一行读取数据的方式。我不知道该怎么做。 – 2011-04-29 21:13:58

1

每次拨打fscanf时,它会读取更多值。你现在面临的问题是你将每行重新读入相同的变量,所以最后,这三个变量有最后一行的值。尝试创建一个可以容纳所有需要的值的数组或其他结构。

2

首先,你不使用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); 

读取多个行,你需要决定是否需要对以前的线条。如果不是,一个单一的字符串(字符数组)将会做。如果你需要前面的行,那么你需要读入一个数组,可能是一个动态分配的指针数组。

0

我有一个令人困惑的方法,也不会有混淆片断一种更简单的解决方案(没有冒犯到规定以上)这里是:

#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; 

}

快乐编码