读取字符串和双精度
reading in strings and doubles
我遇到了一个问题,从文件中读取字符串,然后从文件中读取double。我的教授建议我在每个输入行后面放一个特殊的getline,但是它不起作用,我推断这是程序的问题。有没有更简单的双打方式?
输入文件的例子是:
John Smith
019283729102380
300.00
2000.00
Andrew Lopez
293481012100121
400.00
1500.00
代码如下:
while(! infile.eof())
{
getline(infile,accname[count],'n');
getline(infile, refuse, 'n');
getline(infile,accnum[count],'n');
getline(infile, refuse, 'n');
infile>>currbal[count];
getline(infile, refuse, 'n');
infile>>credlim[count];
getline(infile, refuse, 'n');
count++;
}
编辑:根据OP的澄清更新。
根据您提供的示例,这应该可以工作:
while(1) {
std::string s1;
if(!std::getline(infile, s1))
break;
std::string s2;
if(!std::getline(infile, s2))
break;
double d1, d2;
if(!(infile >> d1 >> d2))
break;
accname[count] = s1;
accnum[count] = s2;
currball[count] = d1;
credlim[count] = d2;
count++;
}
这段代码将适用于如下输入:
Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
但是对于像
这样的输入就不起作用了Adam Sandler
0112233
5 100
Ben Stein
989898
100000000
1
我认为Rob Adams的响应在解析多个记录时有一个小错误。在第一次提取d2
之后,示例输入流将包含nBen Steinn989898n100000000n1
,因此下一次调用std::getline()
将提取空字符串而不是Ben Stein
。因此,似乎有必要在每次while循环迭代结束时使用空字符串。
#include <iostream>
#include <sstream>
int main(int argc, char** argv) {
using std::stringstream;
stringstream infile(stringstream::in | stringstream::out);
infile << "John Smithn";
infile << "019283729102380n";
infile << "300.00n";
infile << "2000n";
infile << "Andrew Lopezn";
infile << "293481012100121n";
infile << "400.00n";
infile << "1500.00n";
std::string account_name;
std::string account_number;
double current_balance;
double credit_limit;
int count = 0;
while (std::getline(infile, account_name) &&
std::getline(infile, account_number) >> current_balance >>
credit_limit) {
std::cout << account_name << std::endl;
std::cout << account_number << std::endl;
std::cout << current_balance << std::endl;
std::cout << credit_limit << std::endl;
/*
accname[count] = account_name;
accnum[count] = account_number;
currball[count] = current_balance;
credlim[count] = credit_limit;
*/
count++;
// Consume the leading newline character, which seprates the credit limit
// from the next account name, when infile contains multiple records.
//
// For example, after consuming the record for John Smith, the stream will
// contain: "nAndrew Lopezn293481012100121n400.00n1500.00n". If you
// don't consume the leading newline character, calling std::getline() to
// parse the next account name will extract an empty string.
std::string blank;
std::getline(infile, blank);
}
return 0;
}
string str;
while(!infile.eof())
{
getline(infile, accname[count]);// you do not need 'n' it will always read to
// end of the line
getline(infile, accnum[count]);
getline(infile, str);
currbal[count] = atof(str.c_str());
getline(infile, str);
credlim[count] = atof(str.c_str());
count++;
}
*
for the file
John Smith
019283729102380
300.00
2000.00
Andrew Lopez
293481012100121
400.00
1500.00
*
相关文章:
- 如何在C++中的同一函数中使用字符串和双精度
- C 字符串返回字符串的整数/双精度/长整型值
- 如何从字符串转换为双精度*
- C++如何将字符串逐行转换为双精度
- 如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将双精度值转换为 char 变量时字符串流如何工作
- 在 c++ 中从双精度转换为字符串
- 从字符串转换为双精度和整数 - C++
- 如何从char16_t字符串文字中读取双精度?
- 分析字符串中的双精度和单词
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- C++将包含非人类可读数据的字符串转换为 200 双精度
- 将大双精度转换为科学记数法以用作字符串?
- 字符串和双精度的麻烦,等值后再得到一个数字
- 如何从字符串流中提取双精度
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- 在C++中使用atof将带小数的字符串转换为双精度字符串