C++ C++;-使用>;读取文件,直到到达行尾&燃气轮机;操作员
我已经环顾了很多地方,仍然没有找到如何做到这一点,所以,请容忍我 假设我必须读取一个包含不同类型数据的txt文件,其中第一个浮点数是一个id,然后有几个(不总是相同数量)代表其他内容的其他浮点数。。。例如,时间是成对的 因此,该文件看起来像:C++ C++;-使用>;读取文件,直到到达行尾&燃气轮机;操作员,c++,operators,ifstream,peek,end-of-line,C++,Operators,Ifstream,Peek,End Of Line,我已经环顾了很多地方,仍然没有找到如何做到这一点,所以,请容忍我 假设我必须读取一个包含不同类型数据的txt文件,其中第一个浮点数是一个id,然后有几个(不总是相同数量)代表其他内容的其他浮点数。。。例如,时间是成对的 因此,该文件看起来像: 1 0.2 0.3 2.01 3.4 5.6 5.7 3 2.0 4.7 ... 经过大量研究,我最终得到了这样一个函数: vector<Thing> loadThings(char* filename){ vector<Thi
1 0.2 0.3
2.01 3.4 5.6 5.7
3 2.0 4.7
...
经过大量研究,我最终得到了这样一个函数:
vector<Thing> loadThings(char* filename){
vector<Thing> things;
ifstream file(filename);
if (file.is_open()){
while (true){
float h;
file >> h; // i need to load the first item in the row for every thing
while ( file.peek() != '\n'){
Thing p;
p.id = h;
float f1, f2;
file >> f1 >> f2;
p.ti = f1;
p.tf = f2;
things.push_back(p);
if (file.eof()) break;
}
if (file.eof()) break;
}
file.close();
}
return things;
}
vector loadThings(char*filename){
向量事物;
ifstream文件(文件名);
if(file.is_open()){
while(true){
浮动h;
file>>h;//我需要为每件事加载行中的第一项
而(file.peek()!='\n'){
事物p;
p、 id=h;
浮球f1、f2;
文件>>f1>>f2;
p、 ti=f1;
p、 tf=f2;
事物。推回(p);
如果(file.eof())中断;
}
如果(file.eof())中断;
}
file.close();
}
归还物品;
}
但是带有(file.peek()!='\n')
条件的while循环永远不会自行结束,我的意思是。。。peek从不等于'\n'
istream& eatwhites(istream& stream)
{
const string ignore=" \t\r"; //list of character to skip
while(ignore.find(stream.peek())){
stream.ignore();
}
return stream;
}
有人知道为什么吗?或者使用>
操作符以其他方式读取文件?!
多谢各位 只是建议另一种方法,为什么不使用
// assuming your file is open
string line;
while(!file.eof())
{
getline(file,line);
// then do what you need to do
}
只是建议另一种方式,为什么不使用
// assuming your file is open
string line;
while(!file.eof())
{
getline(file,line);
// then do what you need to do
}
要跳过任何字符,在到达
while(file.peek()!='\n')
更好的解决方案是将整行内容读入字符串,而不是使用istringstream
解析它
float f;
string line;
std::getline(file, line);
istringstream fin(line)
while(fin>>f){ //loop till end of line
}
要跳过任何字符,在到达
while(file.peek()!='\n')
更好的解决方案是将整行内容读入字符串,而不是使用istringstream
解析它
float f;
string line;
std::getline(file, line);
istringstream fin(line)
while(fin>>f){ //loop till end of line
}
在您和其他朋友的帮助下,我最终将代码改为使用getline()。这里是结果,希望它能帮助别人
typedef struct Thing{
float id;
float ti;
float tf;
};
vector<Thing> loadThings(char* filename){
vector<Thing> things;
ifstream file(filename);
if (file.is_open()){
string line;
while(getline(file, line))
{
istringstream iss(line);
float h;
iss >> h;
float f1, f2;
while (iss >> f1 >> f2)
{
Thing p;
p.id = h;
p.ti = f1;
p.tf = f2;
things.push_back(p);
}
}
file.close();
}
return things;
}
typedef结构物{
浮动id;
浮钛;
浮动tf;
};
矢量加载对象(字符*文件名){
向量事物;
ifstream文件(文件名);
if(file.is_open()){
弦线;
while(getline(文件,行))
{
istringstream iss(线);
浮动h;
iss>>h;
浮球f1、f2;
而(iss>>f1>>f2)
{
事物p;
p、 id=h;
p、 ti=f1;
p、 tf=f2;
事物。推回(p);
}
}
file.close();
}
归还物品;
}
谢谢你抽出时间 在您和其他朋友的帮助下,我最终将代码改为使用getline()。这里是结果,希望它能帮助别人
typedef struct Thing{
float id;
float ti;
float tf;
};
vector<Thing> loadThings(char* filename){
vector<Thing> things;
ifstream file(filename);
if (file.is_open()){
string line;
while(getline(file, line))
{
istringstream iss(line);
float h;
iss >> h;
float f1, f2;
while (iss >> f1 >> f2)
{
Thing p;
p.id = h;
p.ti = f1;
p.tf = f2;
things.push_back(p);
}
}
file.close();
}
return things;
}
typedef结构物{
浮动id;
浮钛;
浮动tf;
};
矢量加载对象(字符*文件名){
向量事物;
ifstream文件(文件名);
if(file.is_open()){
弦线;
while(getline(文件,行))
{
istringstream iss(线);
浮动h;
iss>>h;
浮球f1、f2;
而(iss>>f1>>f2)
{
事物p;
p、 id=h;
p、 ti=f1;
p、 tf=f2;
事物。推回(p);
}
}
file.close();
}
归还物品;
}
谢谢你抽出时间 在检查是否打开
'\n'
之前,您需要跳过空格选项卡等。使用操纵器std::ws:(std::cin>>std::ws).peek()。@zackstd::ws
也将删除'\n'
@SHR遗漏的部分。我的错!在检查是否打开'\n'
之前,您需要跳过空格选项卡等。使用操纵器std::ws:(std::cin>>std::ws).peek()。@zackstd::ws
也将删除'\n'
@SHR遗漏的部分。我的错!你说得对,我就是不知道怎么做。我将在另一条评论中发布我的代码,以防其他人需要它。谢谢你说得对,我就是不知道怎么做。我将在另一条评论中发布我的代码,以防其他人需要它。在C++中,当C++来处理基于文本的文件时,