I/O C++从文本文件中读取
在我的程序中,我输入一个文件,文件内部如下所示:
11267 2 500.00 2.00
...这是一行。还有更多线路按相同顺序设置。我需要将第一个数字 11267
输入到 actnum
中。之后,2
进入 choice
等。我只是缺乏逻辑来弄清楚如何将前 5 个数字输入到第一个变量中。
actnum = 11267;
choice = 2;
编辑*
我拥有所有这些:
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
int trans = 0, account;
float ammount, bal;
cout << "ATM" << endl;
等等等等
我只是不知道如何让它只输入特定的数字。就像我执行 >>actnum >> 时一样choice 它怎么知道只将前 5 个数字放入其中?
In my program I'm inputting a file and inside the file is something like this:
11267 2 500.00 2.00
...that is one line. There are more lines set up in that same order. I need to input the first number, 11267
, into actnum
. After that, 2
into choice
, etc. I simply lack the logic to figure out how to input just the first 5 numbers into the first variable.
actnum = 11267;
choice = 2;
Edit*
I have all of that:
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
int trans = 0, account;
float ammount, bal;
cout << "ATM" << endl;
etc etc
I just dont know how to get it to only input the specific numbers into it. Like when I do the >>actnum >> choice how does it know to put just the first 5 numbers in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 C++
库。fscanf()
稍微过时了,您可能会从
获得更好的性能,更不用说代码更容易阅读:Use the C++
<fstream>
library.fscanf()
is slightly out of date and you will probably get better performance from<fstream>
, not to mention that the code is a lot easier to read:fscanf 就是您正在寻找的东西。它的工作方式与 scanf 相同,但用于文件。
fscanf is what you are looking for. it works the same as scanf, but is for files.