~文件流(fstream)操作出错~

liu6tot 2004-03-05 09:29:14
模仿c++程序设计(钱能版)教材中的一个程序,向磁盘中一个文件写入程序的返回结果,但是在TC++3.0下编译出错,请看程序:
#include<iostream.h>
#include<fstream.h>
void fn(int a, int b)
{
if(b==0)
cerr <<"zero encountered. "
<<"The message cannot be redirected";
else
ofstream liu("e:\\rising\\cpp\\demo.txt");//编译程序提示
//“declaration not
//allowed here”
liu <<a/b; //提示语法错误,这个地方小弟觉得有错,可是又没有靠得
//住依据
cout <<a/b <<endl; //a is never used here
}

void main()
{
fn(20,2);
fn(20,0);
}
本来是想在程序可以整除的情况下,吧整除的结果输入到绝对路径下的demo.txt文件中,可是编译没法通过,请问我的函数是不是写得有问题,还是主函数调用有误???
...全文
62 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h>    参数化输入/输出 #include <iostream h>    数据输入/输出 #include <limits h>     定义各种数据类型最值常量 #include <locale h>     定义本地化函数 #include <math h>      定义数学函数 #include <stdio h>     定义输入/输出函数 #include <stdlib h>     定义杂项函数及内存分配函数 #include <string h>     字符串处理 #include <strstrea h>    基于数组的输入/输出 #include <time h>      定义关于时间的函数 #include <wchar h>     宽字符处理及输入/输出 #include <wctype h>     宽字符分类 int spawnvpe int mode char pathname char argv[] char envp[] spawn函数族在mode模式下运行子程序pathname 并将参数 arg0 arg1 arg2 argv[] envp[] 传递给子程序 出错返回 1 mode为运行模式 mode为 P WAIT 表示在子程序运行完后返回本程序 P NOWAIT 表示在子程序运行时同时运行本程序 不可用 P OVERLAY表示在本程序退出后运行子程序 在spawn函数族中 后缀l v p e添加到spawn后 所指定的函数将具有某种操作能力 有后缀 p时 函数利用DOS的PATH查找子程序文件 l时 函数传递的参数个数固定 v时 函数传递的参数个数不固定 ">#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h& [更多]

64,676

社区成员

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

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