| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 599 人关注过本帖
标题:关于vc++文件的读写
只看楼主 加入收藏
韩雪
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-4-12
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
关于vc++文件的读写
哪位高手帮忙看一下这个程序哪错了,为什么读不出文件的内容
程序代码:#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int a[3];
    ofstream outfile("f1.dat",ios::out);         //准备f1.dat中写数据
    if(!outfile)                                 //如无法正常打开文件,输出open error!
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cout<<"enter 3 integer numbers:"<<endl;      //输入提示
    for(int i=0;i<3;i++)                         //向文件里输入数据
    {
        cin>>a[i];
        outfile<<a[i]<<" ";
    }
    outfile.close;                              //关闭文件
    ifstream infile("f1.dat",ios::in);          //准备读文件
    if(!infile)                                 //如无法正常打开文件,输出open error!
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    int b[3];
    cout<<"the file's number is:";              //输出提示
    for(int k=0;k<3;k++)                        //读文件
    {
        infile>>b[k];
        cout<<b[k]<<" ";
    }
    cout<<endl;
    infile.close;                              //关闭文件              
      
    return 0;
运行结果:输入的是3 4 5
         读出的却是:-858993460 -858993460 -858993460




搜索更多相关主题的帖子: 文件 
2010-06-16 15:28
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:0 
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int a[3];
    ofstream outfile("f1.dat",ios::out);         //准备f1.dat中写数据
    if(!outfile)                                 //如无法正常打开文件,输出open error!
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cout<<"enter 3 integer numbers:"<<endl;      //输入提示
    for(int i=0;i<3;i++)                         //向文件里输入数据
    {
        cin>>a[i];
        outfile<<a[i]<<' ';
    }
    outfile.close();                              //关闭文件
    ifstream infile("f1.dat",ios::in);          //准备读文件
    if(!infile)                                 //如无法正常打开文件,输出open error!
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    int b[3];
    cout<<"the file's number is:";              //输出提示
    for(int k=0;k<3;k++)                        //读文件
    {
        infile>>b[k];
        cout<<b[k]<<' ';
    }
    cout<<endl;
    infile.close();                              //关闭文件

    return 0;
}

★★★★★为人民服务★★★★★
2010-06-16 16:17
lvan100
Rank: 2
等 级:论坛游民
帖 子:6
专家分:40
注 册:2010-6-16
收藏
得分:10 
回复 楼主 韩雪
楼主犯了低级错误啦0.0
你看看,问题出在这里啦。
outfile.close;
infile.close;
实际上close()是一个函数。
应该是
outfile.close();
infile.close();
2010-06-16 22:01
韩雪
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-4-12
收藏
得分:0 
哦,谢谢啦
2010-06-18 21:00
快速回复:关于vc++文件的读写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011276 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved