I/O c++从文本文件中读取
I/O C++ Reading from a text file
在我的程序中,我输入一个文件,文件内部是这样的:
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>>选项时它怎么知道只放前5个数字呢?
使用c++ <fstream>
库。fscanf()
有点过时了,你可能会从<fstream>
中获得更好的性能,更不用说代码更容易阅读:
#include <fstream>
using namespace std;
ifstream fileInput("C:foo.txt");
fileInput >> actnum >> choice >> float1 >> float2;
input_file_stream >> actnum >> choice >> ...
fscanf就是你要找的。它的工作原理与scanf相同,但适用于文件。
unsigned int actnum, choice;
float float1, float2;
FILE *pInputFile = fopen("input.txt", "r");
fscanf(pInputFile, "%u %u %f %f", &actnum, &choice, &float1, &float2);
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量