~文件流(fstream)操作出错~
模仿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文件中,可是编译没法通过,请问我的函数是不是写得有问题,还是主函数调用有误???
#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文件中,可是编译没法通过,请问我的函数是不是写得有问题,还是主函数调用有误???
...全文
相关推荐
C++Fstream文件流与freopen重定向操作教程
"C++ Fstream文件流与freopen重定向操作教程"本文主要介绍了C++中的Fstream文件流和freopen重定向操作。
C++流操作之fstream用法介绍
C++的流操作是编程中处理输入输出的重要部分,特别是在文件操作方面,fstream库提供了方便且灵活的接口。本文主要关注C++中的fstream类及其派生类ifstream和ofstream的使用。
fstream头文件
fstream头文件的功能fstream头文件提供了对文件的读取和写入功能,包括文件的打开、关闭、读取和写入操作。该头文件定义了filebuf类,用于处理文件流的缓冲区。
ofstream,ifstream,fstream读写文件
fstream 是一个通用的文件流类,它可以同时进行读写操作。当你需要在一个文件中既读又写时,可以使用 fstream。
C++中fstream,ifstream及ofstream用法浅析
**fstream类**: `fstream`类是最基础的文件流类,它可以进行读写操作。创建一个`fstream`对象后,使用`open()`函数打开文件。


