文档操作错误示例

实现在文件data.dat文件写入099数字,然后在读取数字并进行显示。

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

     ofstream output("data.dat");

     int i;

     i = 0;

     if (output == NULL)

     {

         cout << "Open Outfile is lose!" << endl;

         return ;

     }

     for (;i != 100;i++)

     {

         output << i;

     }

     output.close();

     ifstream input("data.dat");

     if (input == NULL)

     {

         cout << "Open Infile if lose!" << endl;

         return ;

     }

     while (!input.eof())

     {

         input >> i;

         cout << i << endl;

     }

     input.close();

}

运行结果:

data.dat文件结果:

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

显示结果: 100

 

从现实结果来看,此程序是错误的。原因:在写入文件的时候没有加入字符结束标示符,因此读取文件的时候,把data.dat文件中的所有数据当成一个int数据读取,造成输出错误。但为什么输出100?因为在读取文件失败的时候,ifstream返回一个TRUE值并不往i中赋值,所以i保留的还是以前的值。将程序改为以下三种方法都是正确的。

1. 增加回空格来分开写入的数据,使读取文件的时候保证文件里面的数据符合赋值的类型。

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

     ofstream output("data.dat");

     int i;

     i = 0;

     if (output == NULL)

     {

         cout << "Open Outfile is lose!" << endl;

         return ;

     }

     for (;i != 100;i++)

     {

         output << i << " " ;

     }

     output.close();

     ifstream input("data.dat");

     if (input == NULL)

     {

         cout << "Open Infile if lose!" << endl;

         return ;

     }

     while (!input.eof())

     {

         input >> i;

         cout << i << endl;

     }

     input.close();

}

2增加回车符来分开写入的数据,使读取文件的时候保证文件里面的数据符合赋值的类型。

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

     ofstream output("data.dat");

     int i;

     i = 0;

     if (output == NULL)

     {

         cout << "Open Outfile is lose!" << endl;

         return ;

     }

     for (;i != 100;i++)

     {

         output << i ;

         output << endl;

     }

     output.close();

     ifstream input("data.dat");

     if (input == NULL)

     {

         cout << "Open Infile if lose!" << endl;

         return ;

     }

     while (!input.eof())

     {

         input >> i;

         cout << i << endl;

     }

     input.close();

}

3增加判断读取是否成功的判断机制,如果成功则读取和显示文件的数据,不成功则直接返回。

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

     ofstream output("data.dat");

     int i;

     i = 0;

     if (output == NULL)

     {

         cout << "Open Outfile is lose!" << endl;

         return ;

     }

 

     for (;i != 100;i++)

     {

         output << i;

     }

     output.close();

     ifstream input("data.dat");

     if (input == NULL)

     {

         cout << "Open Infile if lose!" << endl;

         return ;

     }

     while (!input.eof())

     {

         if (!(input >> i)) //判断读取数据是否成功,成功就赋值给i;不成功则执行break,不给i赋值。

         {

              break;

         }

        

         cout << i << endl;

     }

     input.close();

}

上面程序因为判断读取数据是否成功也可以作为while循环判断语句来用,此程序还可以改为:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

     ofstream output("data.dat");

     int i;

     i = 0;

     if (output == NULL)

     {

         cout << "Open Outfile is lose!" << endl;

         return ;

     }

 

     for (;i != 100;i++)

     {

         output << i << endl;

     }

     output.close();

     ifstream input("data.dat");

     if (input == NULL)

     {

         cout << "Open Infile if lose!" << endl;

         return ;

     }

     while (1)

     {

         if (!(input >> i)) //判断读取数据是否成功,成功就赋值给i;不成功则执行break,不给i赋值。

         {

              break;

         }

        

         cout << i << endl;

     }

     input.close();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值