C++ 在C+中读取每行文本文件+;,具有未知的行长度
我有一个文本文件,格式如下:C++ 在C+中读取每行文本文件+;,具有未知的行长度,c++,C++,我有一个文本文件,格式如下: 1 3 4 5 6 6 7 8 4 12 16 17 18 19 20 20 0 line=0; i=0; while(!file.eof()){ while(!endLine){ array[0][i++]=file.readChar(); } line++;i=0; } 一行可以包含1到10000个整数。我需要做的是逐行阅读所有的内容 伪代码如下: 1 3 4 5 6 6 7 8 4 12 16 17 18 19 20 20 0 line=0; i
1 3 4 5 6
6 7 8
4 12 16 17 18 19 20
20
0
line=0;
i=0;
while(!file.eof()){
while(!endLine){
array[0][i++]=file.readChar();
}
line++;i=0;
}
一行可以包含1到10000个整数。我需要做的是逐行阅读所有的内容
伪代码如下:
1 3 4 5 6
6 7 8
4 12 16 17 18 19 20
20
0
line=0;
i=0;
while(!file.eof()){
while(!endLine){
array[0][i++]=file.readChar();
}
line++;i=0;
}
我有一个数组,我想读入其中的每一行,每一行都由这些整数组成
我遇到的问题是,如何检查一行的末尾是否已到达
注意,我不能使用字符串
是的,这是一个家庭作业,但作业的主要任务是建立一棵树,然后转换它。我可以这样做,但我不知道如何从文件中读取整数。有一个名为getline()的函数,它将读取整行 您需要一个函数从文件读取值,或指示文件的行末或结束条件,例如:
result_type GetNextValue (input_file, &value)
{
if next thing in file is a number, set value and return number_type
if next thing in file is an end of line, return end_of_line_type
if end of file found, return end_of_file_type
}
然后,阵列构建循环变成:
line = 0
item = 0
eof = false
while (!eof)
{
switch (GetNextValue (input_file, value))
{
case value_type:
array [line][item++] = value
case end_of_line_type:
line++;
item = 0;
case end_of_file_type:
eof = true
}
}
我会把细节留给你,因为这是我的家庭作业。大概是这样的:
1 3 4 5 6
6 7 8
4 12 16 17 18 19 20
20
0
line=0;
i=0;
while(!file.eof()){
while(!endLine){
array[0][i++]=file.readChar();
}
line++;i=0;
}
在读取一个int之后,我手动跳过空格、制表符、回车和行尾(对于这个,您必须实现您的逻辑)。
要读取一个int,我直接使用C++的函数来读取它。我不会一个字符一个字符地读取它,然后将其重新组合为字符串:-)
请注意,我将\r
跳过为“空格”。我的行尾是\n
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::ifstream file("example.txt");
std::vector<std::vector<int>> ints;
bool insertNewLine = true;
int oneInt;
//The good() here is used to check the status of
//the opening of file and for the failures of
//peek() and read() (used later to skip characters).
while (file.good() && file >> oneInt)
{
if (insertNewLine)
{
std::vector<int> vc;
ints.push_back(vc);
//With C++11 you can do this instead of the push_back
//ints.emplace_back(std::vector<int>());
insertNewLine = false;
}
ints.back().push_back(oneInt);
std::cout << oneInt << " ";
int ch;
while ((ch = file.peek()) != std::char_traits<char>::eof())
{
if (ch == ' '|| ch == '\t' || ch == '\r' || ch == '\n')
{
char ch2;
if (!file.read(&ch2, 1))
{
break;
}
if (ch == '\n' && !insertNewLine)
{
std::cout << std::endl;
insertNewLine = true;
}
}
else
{
break;
}
}
}
//Here we should probably check if we exited for eof (good)
//or for other file errors (bad! bad! bad!)
return 0;
}
#包括
#包括
#包括
int main()
{
std::ifstream文件(“example.txt”);
std::向量整数;
bool insertNewLine=true;
int-oneInt;
//此处的good()用于检查
//文件的打开以及
//peek()和read()(稍后用于跳过字符)。
while(file.good()&&file>>oneInt)
{
如果(插入换行符)
{
std::向量vc;
内部推回(vc);
//使用C++11,您可以执行此操作,而不是向后推
//ints.emplace_back(std::vector());
insertNewLine=false;
}
int.back().push_back(oneInt);
std::cout您可以读取字符中的数字,并对照回车进行检查。我刚才尝试过的代码片段如下所示:
ifstream ifile;
ifile.open("a.txt");
char ch;
while((ch = ifile.get()) != EOF)
{
std::cout<<ch<<"\n";
if (ch == '\n')
std::cout<<"Got New Line";
}
ifile.close();
ifrifile;
ifile.open(“a.txt”);
char ch;
而((ch=ifile.get())!=EOF)
{
std::Coutt OP说注意,我不能使用字符串。
getline()函数返回char*而不是字符串。@Skizz:谁说你需要将std::string
与getline()一起使用
?如果您点击链接,您将看到答案是指istream::getline
,它接受一个普通的char*
和sizeAllo,getline需要分配并传入一个数组,这意味着需要预定义最大行长度。否则,您可能会将一行一分为二(更糟糕的是,将一个数字一分为二!)为什么你认为OP的意思是std::string?getline给你一个以nul结尾的字符序列,通常称为字符串。如果作业的主要任务是构建树,为什么不允许你使用字符串?讲师正在吸烟,我当然想试试。是的,但我如何检查行的结尾是否已经到了文件中的下一件事是行尾,返回行尾类型,我如何检查它?@JanisPeisenieks:当你一次读取一个字符时,你会得到一个数字、一个空格或一个行尾字符。行尾字符是一种痛苦,因为有些系统使用0x0a后跟0x0d,有些系统使用另一种方式,有些则是oGETNEXValk需要包含关于它读取的状态的信息。更好的是,当你使用C++时,它需要是一个带有输入文件作为构造函数参数的对象,而GETNEXT值是一个成员函数(不需要这样传递任何参数)。还有一个额外的GETRead值。是的!这正是我需要的,谢谢!更多的“C”答案,而不是“C++”的结构(你可以很容易的替换STD::IFFSUP带文件*)——你应该把解析逻辑和树构建逻辑封装成类,使它真正的C++,代码>,(.MyFrase.Efof())
Very,Very BAD!!搜索原因。由于我输入文件中的最后一个文件始终是一个0,并且0不能出现在其他任何地方,我只是将其替换为,而(myInt!=0)
@Xeo是的……我忘记了。查找SO文章更复杂:-)现在我使用good
。