混合ifstream getline和>> [英] Mixing ifstream getline and >>

查看:96
本文介绍了混合ifstream getline和>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用>> 输入 float 值后,getline停止工作并读取垃圾桶。如何避免此问题?

  string settingsFile =TerrainSettings.txt; 

ifstream settingsStream;

settingsStream.open(settingsFile.data());
assert(settingsStream.is_open());

string line;

getline(settingsStream,line); // fine here
getline(settingsStream,line); // fine here
getline(settingsStream,line); // fine here

float frequency;
float octaves;
float amplitude;

settingsStream>> frequency; // fine here

getline(settingsStream,line); //这里是垃圾桶,为什么?

settingsStream>>八度; // trash

getline(settingsStream,line);

settingsStream>> amplitude;

编辑:包含ignore(),会产生以下错误:

  settingsStream>> frequency; 

settingsStream.ignore(256,'\\\
');

getline(settingsStream,line); // fine here

settingsStream>> octaves; // trash here

getline(settingsStream,line);

settingsStream>> amplitude;

示例输入:

  / *地形渲染设置请输入各自的行* / 
/ ** /
频率:
24
八度:
120
幅度:
1.25


解决方案

首先,使用 std :: string s和 std :: getline 开始。其次,垃圾的原因是缓冲区中还有一个换行符。如果你希望用户一次输入一行,你应该使用 istream :: ignore 跳过所有直到并包括下一个换行符。例如:

  std :: string s; 
float f;
std :: cin>> F;
std :: cin.ignore(big_num,'\\\
');
std :: getlines(std :: cin,s);

什么 big_num 应该取决于您的期望关于输入。如果你不介意编写很多,并且想要安全,使用 std :: numeric_limits< std :: streamsize> :: max()



例如,上面的代码将解析下面的代码,使得 f = 5.0f s = Hello!

  5 
您好!但是,它将按照完全相同的方式解析:






  5嗨! 
你好!

如果您要保留 Hi! ,你不应该忽略的东西,而是定义一个正确的语法,你将用来解析文档。


After using a >> to input a float value, getline ceases to work correctly and reads trash. How can I avoid this?

   string settingsFile = "TerrainSettings.txt";

   ifstream settingsStream;

   settingsStream.open(settingsFile.data());
   assert (settingsStream.is_open());

   string line;

   getline(settingsStream,line); // fine here
   getline(settingsStream,line); // fine here
   getline(settingsStream,line); // fine here

   float frequency;
   float octaves;
   float amplitude;

   settingsStream>>frequency; // fine here

   getline(settingsStream,line); // here it gets trash, why? 

   settingsStream>>octaves; // trash

   getline(settingsStream,line);

   settingsStream>>amplitude;

Edit: An inclusion of ignore(), generates the following error:

   settingsStream>>frequency;

   settingsStream.ignore(256,'\n');

   getline(settingsStream,line); // fine here 

   settingsStream>>octaves; // trash here

   getline(settingsStream,line);

   settingsStream>>amplitude;

sample input:

/*Settings for Terrain Rendering, please input values on their respective lines*/
/**/
Frequency:                
24
Octaves:                   
120
Amplitude:                 
1.25

解决方案

First of all, start using std::strings and std::getline. Secondly, the reason for the trashing is that there's still a newline in the buffer. If you expect the user to be entering values one line at a time, you should use istream::ignore to skip everything up to and including the next newline character. For example:

std::string s;
float f;
std::cin >> f;
std::cin.ignore(big_num, '\n');
std::getline(std::cin, s);

What big_num should be depends on your expectations about the input. If you don't mind writing a lot and want to be on the safe side, use std::numeric_limits<std::streamsize>::max(). If you do mind writing a lot, make a constant of the suitable type that you use everywhere.

For example, the above code will parse the following so that f = 5.0f, s = Hello!.

5
Hello!

However, it will parse the following exactly the same way:

5 Hi!
Hello!

If you want to preserve the Hi!, you shouldn't ignore things, and instead define a proper grammar that you'll use to parse the document.

这篇关于混合ifstream getline和&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆