返回列表 回复 发帖

如何读取文件中的内容并输出到屏幕上

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

const char out_file_name[]="F:a.txt";
void main()
{
    fstream outdata;
    fstream out_data;
    void creat_file(fstream &out,const char file_name[]);
    void in_screen(fstream &in,const char file_name[]);
   
    cout<<"\nCreating output file"<<out_file_name<<"...\n";
    creat_file(outdata,out_file_name);
    cout<<"\n"<<out_file_name<<"\nhave been created.\n";
   
    in_screen(out_data,out_file_name);
   
}
void creat_file(fstream &out,const char file_name[])
{
    char ch;
    char newline='\n';
   
    out.open(file_name,ios:ut);
   
    if(out.fail())
    {
        cerr<<"\n***>Open error on output file"<<file_name;
        exit(-1);
    }
   
    cout<<"Enter a record(\\ terminates input):";
    cin.get(ch);
    while(ch!='\\')
    {
        
        out.put(ch);
        if(ch==newline)
            cout<<"Enter a record(\\ terminates input):";
        cin.get(ch);
   
    }
    out.close;
}

void in_screen(fstream &in,const char file_name[])
{
    char ch;
    in.open(file_name,ios::in);
    if(in.fail())
    {
        cout<<"\nOpen error reading input file "<<file_name;
        exit(-1);
    }
   
    in.get(ch);//这里得到的却是文件的结束符  不知道为什么????????????????

   
    while(!in.eof())
    {
        cout<<ch;
        in.get(ch);
    }
    in.close;
}
程序可以把由键盘输入的内容写入到文件里,可是却不能把刚刚写入文件的内容重新读出来并输出到屏幕上,各位看看哪里的问题

[ 本帖最后由 800 于 2007-4-3 11:26 编辑 ]
我有香烟我有啤酒我有我的影子陪我抽烟陪我喝酒
const char out_file_name[]="F:\\a.txt";
这样写才行
使用\\来转义
程序讨论欢迎进入http://westsoftware.blog.163.com
在调用第二个函数的时候, 打开文件 ,为什么读到的却是文件结束符呢???????

[ 本帖最后由 800 于 2007-4-3 11:27 编辑 ]
我有香烟我有啤酒我有我的影子陪我抽烟陪我喝酒
各位大虾帮忙看看呀
我有香烟我有啤酒我有我的影子陪我抽烟陪我喝酒
不好意思,在调用close函数的时候漏掉了一对括号(),比较低级的错误 给各位添麻烦了
我有香烟我有啤酒我有我的影子陪我抽烟陪我喝酒
Once upon a man.
返回列表