从.txt文件读取输入输出C++
input output C++ reading from a .txt file
如何将其打印到输出文件中,并在4周内按周和整周计算这些人的工资总和和平均值。。。
示例txt文件。。
doe jane
williams tom
lons adams
45.7 56.3 345.6 344.7 // week 1
43.6 89.0 543.6 12.5 // week 1 person 2
90.5 78.0 345.4 345.6 //week 1 person 3
67.5 34.5 56.6 34.5 // week2 person 1
etc....for 4 weeks..
我知道有一种更简单的方法可以使用循环吗?我可以得到一些帮助,谢谢:)
这就是我目前拥有的
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
double s1, s2, s3 , s4 ,s5;
double t1, t2,t3,t4,t5;
double w1, w2,w3,w4,w5;
string personlast,personfirst,personlast2,personfirst2,personlast3,personfirst3;
double sum, average;
int numberpeople, numberofweeks;
infile.open("data.txt");
outfile.open("output.txt");
outfile<< fixed<< showpoint;
outfile<< setprecision(2);
infile>> numberpeople >> personlast >> personfirst >> personlast2 >> personfirst2>>
personlast3 >> personfirst3 >> numberofweeks;
outfile<< " The number of salespeople are " << numberpeople <<"they are" <<
personlast << personfirst << "and " <<
personlast2 << personfirst2 <<
"and " << personlast3 << personfirst3 <<"Number of weeks = " << numberofweeks;
infile>> s1 >> s2 >> s3 >> s4 >> s5;
outfile <<" sales for week 1 "<< " for" << personlast << personfirst << s1 << s2
<< s3 << s4 << s5 << endl;
sum= s1+s2+s3+s4+s5;
outfile <<"Sum of first week is " << sum<<endl;
infile >> t1 >> t2 >> t3 >> t4 >> t5;
outfile <<" sales for week 1 "<< " for" << personlast2 << personfirst2 << t1 << t2
<< t3 << t4 << t5 <<endl;
infile.close();
outfile.close();
return 0;
我不会为你做家庭作业,因为我不想,但我会给你一些开始的东西。基本概念是在文件上循环,读取每一行。使用类似(伪代码)的控件;
while (string = readline() != EOF)
{
//split string on delimiters in this case spaces
if (string piece is not an int)
{
// this is a name
// set first and last name here
}
if (string piece is an int)
{
// set ints,
}
}
您还需要一些结构(如果您已经了解了这些结构,那么这些结构可能就是类)来保存数据。如果可以使用字符串,请使用它们而不是char*
。如果使用char*
,则必须动态声明一个char数组。如果允许的话,你也可以把它们做成char[64] first_name;
。
struct person {
char *first_name;
char *last_name;
numbers[4] numbers;
};
struct numbers {
float item1;
float item2;
float item3;
float item4;
};
当您读取数据时,您需要将其读取到人员结构的实例中。
相关文章:
- 字符的输入/输出操作
- C++ 中文件输入输出中的长长 v/s int
- 要求用户输入C++输出文件名
- 在C++中重载输入/输出运算符
- 从用户获取输入/输出
- 如何选择waveIn/waveOut函数C++使用的输入/输出设备
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 从 2 位输入输出 4 位数字年份
- 标准输入输出操作如何C++完成
- Windows 中应用程序的每秒输入/输出操作数
- 如何初始化可以设置为默认数组并将输入/输出作为数组的类函数?
- 输入/输出和类
- 使用 Octave GNU 访问输入/输出文件
- (C++)分解这个简单的输入/输出代码:
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 干净取消在输入/输出调用中被阻止的 std::线程
- 将线程之间的标准输入/输出重定向
- 输入/输出异或字符串的 C++ 问题
- C++输入/输出错误
- C++输入输出