从输入文件读取多种数据类型 [英] Reading multiple data types from input file

查看:77
本文介绍了从输入文件读取多种数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题:

我试图从另一个文件获取输入,将它们初始化为变量,然后将变量输出到另一个文件.我可以将所有输入输入到单独的变量中,除了1行(该行应该是变量)之外.

I am trying to take input from another file, initialize them into variables, then output the variables to another file. I am able to get all input into separate variables except for 1 line (the line should be a variable).

我的输入文件包含以下数据:

My input file contains this data:

557.9012043 0.673621489 7210984732 1.891092837
238 a 789.234 b
Yes Please
cannot wait for hawaii

"Yes Please"行应作为一个完整的字符串,但是,"Yes"和"Please"将分开.

The line "Yes Please" should be taken as a whole string, however, "Yes" and "Please" are getting separated.

我的代码是:

outFile << fixed << showpoint;
        outFile << setprecision(10);

        inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> string1
                >> word1 >> word2 >> word3 >> word4;

        outFile << w << endl;
        outFile << x << endl;
        outFile << y << endl;
        outFile << z << endl;
        outFile << float1 << endl;
        outFile << int1 << endl;
        outFile << char1 << endl;
        outFile << char2 << endl;
        outFile << string1 <<endl;
        outFile << word1 << endl;
        outFile << word2 << endl;
        outFile << word3 << endl;
        ouFile << word4 << endl;
}

当我运行它时,我的outFile包含:

When I run it, my outFile consists of:

557.9012043000
0.6736214890
7210984732.0000000000
1.8910928370
789.2340000000
238
a
b
Yes
Please
cannot
wait
for

如何将整行是的"分配给我的变量string1?

How can I get the whole line, "Yes Please" assigned to my variable string1?

问题2:我的outFile在浮点变量中具有所有额外的0,因为我设置了精度.实际上,我的目标是使用变量来求解方程式.我希望方程式的答案仅输出10个有效数字.小数位数是10位后,如果没有精度怎么办?

Question 2: My outFile has all of the extra 0's in the floating point variables because I set the precision. My goal, really, is to use the variables to solve equations. I would like the answers to the equations to only print 10 significant digits. How would I accomplish this task without the precision after the decimal being 10 digits?

我对C ++还是很陌生,所以当您回答时,能否请您解释为什么给出您回答的原因.我不是想只收到答案,而是想学习.谢谢.

I am very new to C++, so when if you answer, can you please explain why you gave the answer you did. I'm not trying to only receive answers, I want to learn. Thank you.

推荐答案

我通过在getline()语句之前添加myFile.ignore()解决了在getline()语句后出现空白行的问题.

I solved the issue of getting a blank line after my getline() statement by adding myFile.ignore() before the getline() statement.

}
inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2;
inFile.ignore();
getline(inFile, string1);
inFile >> word1 >> word2 >> word3 >> word4;
}

现在,我的string1变量在整个字符串中都包含是",它们不再分开.

Now my string1 variable contains "Yes Please" as a whole string, they are no longer separated.

这篇关于从输入文件读取多种数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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