C++中文件打开和关闭的操作

zhengweiyu 2005-06-27 09:51:12
一个计算学生平均分的程序;
输入文件(test.txt的内容是):

T 87 89 65 37 98

输出文件(average.txt的内容是):

student ID: T
Test scores: 87 89 65 37 98
Average test score: 75.2

以下是我写的程序:
===========================================================================

/*
Filename: ex_3_08.cpp
Author: xexplorer
CtrateDate: 2005-06-15
Version: 1.0
Description: Test

ModifiedDate: yyyy-mm-dd
*/

/***************[ head file ]***************/
#include <iostream>
#include <fstream>
#include <iomanip>

int main()
{
/***************[ declear variables ]***************/
char studentID = ' ';
double test1 = 0;
double test2 = 0;
double test3 = 0;
double test4 = 0;
double test5 = 0;
double average = 0;

/***************[ open files ]***************/
ifstream inFile("E:\\project\\CVSClient\\Admin\\CVSRoot\\CStudy\\Chapter03\\test.txt");
ofstream outFile("E:\\project\\CVSClient\\Admin\\CVSRoot\\CStudy\\Chapter03\\average.txt");

/***************[ statments ]***************/
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

cout<<"Processing data"<<endl;
inFile>>studentID;
outFile<<"student ID: " <<studentID<<endl;

inFile>>test1>>test2>>test3>>test4>>test5;
outFile<<"Test scores: "
<<setw(4)<<test1
<<setw(4)<<test2
<<setw(4)<<test3
<<setw(4)<<test4
<<setw(4)<<test5
<<endl;

average = static_cast<double>(test1+test2+test3+test4+test5)/5.0;
outFile<<"Average test score: "
<<setw(4)<<average
<<endl;

return 0;
}

===========================================================================

按照教材上的采用inFile.open(),outFile.open(),inFile.colse(),outFile.close()编译无法通过;
请问是什么原因.另外,上面的程序中执行到最后文件流的状态究竟是怎么样的,是打开还是关闭的?

...全文
292 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2005-06-27
  • 打赏
  • 举报
回复
用vc或dev c++
foochow 2005-06-27
  • 打赏
  • 举报
回复
运行也正常,没错误:-)
foochow 2005-06-27
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std; //你是不是少写了这个,你的程序我在VC6.0下编译正常
zhengweiyu 2005-06-27
  • 打赏
  • 举报
回复
用的是c-free 2.0,介绍一些带标准模板库的小巧好用的C++编译器么.
darkstar21cn 2005-06-27
  • 打赏
  • 举报
回复
fstream::is_open ()可以查询文件流的状态。

不知楼主用的是什么编译器,可能是这个编译器自身根本没有带stl
zhengweiyu 2005-06-27
  • 打赏
  • 举报
回复
为什么不能结帖啊?下面是系统给出的消息.
/*
15,1,60,1,1,20,1,1
只有未结、非0分贴子才可以结贴给分
*/
zhengweiyu 2005-06-27
  • 打赏
  • 举报
回复
谢谢大家,确实是名字空间的问题.贴出改过的程序,结帖给分.

/*
Filename: ex_3_08.cpp
Author: xexplorer
CtrateDate: 2005-06-15
Version: 1.0
Description: Test

ModifiedDate: yyyy-mm-dd
*/

/***************[ head file ]***************/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
/***************[ declear variables ]***************/
char studentID = ' ';
double test1 = 0;
double test2 = 0;
double test3 = 0;
double test4 = 0;
double test5 = 0;
double average = 0;

ifstream inFile;
ofstream outFile;

/***************[ statments ]***************/
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);

/***************[ open files ]***************/
inFile.open("E:\\project\\CVSClient\\Admin\\CVSRoot\\CStudy\\Chapter03\\test.txt");
outFile.open("E:\\project\\CVSClient\\Admin\\CVSRoot\\CStudy\\Chapter03\\average.txt");

cout<<"Processing data ... ..."<<endl;
inFile>>studentID;
outFile<<"student ID: " <<studentID<<endl;

inFile>>test1>>test2>>test3>>test4>>test5;
outFile<<"Test scores: "
<<setw(4)<<test1
<<setw(4)<<test2
<<setw(4)<<test3
<<setw(4)<<test4
<<setw(4)<<test5
<<endl;

average = static_cast<double>(test1+test2+test3+test4+test5)/5.0;
outFile<<"Average test score: "
<<setw(4)<<average
<<endl;

cout<<"Check fstream status: "<<inFile.is_open()<<endl;

/***************[ close files ]***************/
inFile.close();
outFile.close();

cout<<"Check fstream status: "<<inFile.is_open()<<endl;

return 0;
}
zhengweiyu 2005-06-27
  • 打赏
  • 举报
回复
我贴的程序是可以正常运行的.问题是不知道怎么进行文件打开和关闭的操作.
alwaysfreeboy 2005-06-27
  • 打赏
  • 举报
回复
头文件不加。h
就要用using namespace std;打开名字空间

像foochow那样写

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧