刷题中读写文件和手动输入的来回切换

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
ifstream fin("hello.txt");
/*
ifstream in;
    in.open("1.txt");
    #ifndef debug
    #define cin in
    #endif // deubg
*/
也或者/* #ifdnef ifstream cin("1.txt");#endif*/

while (fin!=NULL)
{
int a;
char str[10];
fin>>a;
fin>>str;
cout<<a<<"+char"<<str<<endl;
}
}
以下是更加方便的操作方法,转载自别人的博客:http://blog.csdn.net/wangwei200508/article/details/5655095

觉得答案正确以后,得提交代码,这时候不能把这个测试也提交上去。可以通过以下2种方式解决:

1、把文件开始的 //#define NDEBUG 的注释符去掉

2、把所有在 #ifndef NDEBUG 和 #endif 之间的代码段(包括那2句)都删掉

 然后复制提交。


#include<iostream>  
//#define NDEBUG  
  
#ifndef NDEBUG  
#include<fstream>  
#endif  
  
using namespace std;  
  
  
int main()  
{  
#ifndef NDEBUG  
    // 打开必要的 files  
    ofstream outfile("out.txt",ofstream::app);  
    ifstream infile("in.txt",ifstream::in);   
// 将要从屏幕读取或输出到屏幕上的信息转移到 files上  
#define cin infile  
#define cout outfile  
    // 输出调试时间,方便对比  
    cout<<"Compiled on "<<__DATE__  
        <<" at "<<__TIME__<<endl  
        <<"----- start -----"<<endl;  
#endif  
  
    //以下是解题的过程  
    int i_temp = 0,  
        i_temp2 = 0;  
    while(cin>>i_temp)  
    {  
        if(i_temp%2)  
        {  
            i_temp2 = (i_temp+1)>>1;  
            cout<<i_temp2*i_temp<<endl<<endl;  
        }  
        else  
        {  
            i_temp2 = i_temp>>1;  
            cout<<i_temp2*(i_temp+1)<<endl<<endl;  
        }  
    }  
  
//这里收个尾,对outfile末尾进行标记,关闭files    
#ifndef NDEBUG  
    cout<<"----- end -----"<<endl<<endl;  
    outfile.close();  
    infile.close();  
#endif  
  
    return 0;  
}  
是在winXP下用vim编辑,用g++编译的,执行前必须在同一文件夹下建立 in.txt。其中内容是

1
100

执行完后,在out.txt 里会输出:

Compiled on Jun 8 2010 at 10:39:59
----- start -----
1



5050



----- end -----



这样我们就可以通过查看 out.txt 来核对答案。



觉得答案正确以后,得提交代码,这时候不能把这个测试也提交上去。可以通过以下2种方式解决:

1、把文件开始的 //#define NDEBUG 的注释符去掉

2、把所有在 #ifndef NDEBUG 和 #endif 之间的代码段(包括那2句)都删掉



然后复制提交。



另外,gvim在winXP64位的cmd控制台下用 搜狗输入法 输入“文件”2个字时会出错,把同一行中“件”前面的文字全部删除了,不解。只能把“文件”都用“file”代替了



相信高手们肯定有更好的办法,恳请赐教

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值