filein2.cpp
来自「c语言教程源码」· C++ 代码 · 共 33 行
CPP
33 行
//这个程序在本书所带软盘中,文件名为filein2.CPP
//这个程序演示文件的输入以及文件结束符的检查。
#include <fstream.h>
#include <stdlib.h>
void main(void)
{
ifstream in_file; //定义一个输入文件
int count = 0;
float price, avg, total = 0.0;
in_file.open("price.txt", ios::in); //打开这个文件
if(!in_file) //检查文件打开是否成功
{
cout << "文件打开错误,检查文件后再试..." << endl;
exit(1);
}
while (!(in_file >> price).eof())
{
total += price;
count++;
}
avg = total / count;
cout << "购买总值:" << total << endl;
cout << "平均价钱:" << avg << endl;
in_file.close();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?