gpt4 book ai didi

c++ - 混合 ifstream getline 和 >>

转载 作者:行者123 更新时间:2023-11-28 03:40:44 25 4
gpt4 key购买 nike

<分区>

在使用 >>> 输入 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); // here it gets trash, why?

settingsStream>>octaves; // trash

getline(settingsStream,line);

settingsStream>>amplitude;

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

   settingsStream>>frequency;

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

getline(settingsStream,line); // fine here

settingsStream>>octaves; // trash here

getline(settingsStream,line);

settingsStream>>amplitude;

示例输入:

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com