使用ifstream来读取txt文件,控制…

#include
#include

using namespace std;

int main()
{
ifstream fin("hello.txt");
while (fin!=NULL)
{
int a;
char str[10];
fin>>a;
fin>>str;
cout<
}
}

使用ifstream来读取txt文件,控制格式很方便

使用ifstream来读取txt文件,控制格式很方便


附:
问:
ifstream指针如何回到开始?
答:
增加该语句:fin.seekg(ios::beg);

以下是更加方便的操作方法,转载自别人的博客:http://blog.csdn.net/wangwei200508/article/details/5655095

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

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

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

 然后复制提交。

  1. #include  
  2. //#define NDEBUG  
  3.   
  4. #ifndef NDEBUG  
  5. #include  
  6. #endif  
  7.   
  8. using namespace std;  
  9.   
  10.   
  11. int main()  
  12. {  
  13. #ifndef NDEBUG  
  14.     // 打开必要的 files  
  15.     ofstream outfile("out.txt",ofstream::app);  
  16.     ifstream infile("in.txt",ifstream::in);   
  17. // 将要从屏幕读取或输出到屏幕上的信息转移到 files上  
  18. #define cin infile  
  19. #define cout outfile  
  20.     // 输出调试时间,方便对比  
  21.     cout<<"Compiled on "<<__DATE__  
  22.         <<" at "<<__TIME__<
  23.         <<"----- start -----"<
  24. #endif  
  25.   
  26.     //以下是解题的过程  
  27.     int i_temp = 0,  
  28.         i_temp2 = 0;  
  29.     while(cin>>i_temp)  
  30.     {  
  31.         if(i_temp%2)  
  32.         {  
  33.             i_temp2 = (i_temp+1)>>1;  
  34.             cout<
  35.         }  
  36.         else  
  37.         {  
  38.             i_temp2 = i_temp>>1;  
  39.             cout<
  40.         }  
  41.     }  
  42.   
  43. //这里收个尾,对outfile末尾进行标记,关闭files    
  44. #ifndef NDEBUG  
  45.     cout<<"----- end -----"<
  46.     outfile.close();  
  47.     infile.close();  
  48. #endif  
  49.   
  50.     return 0;  
  51. }  

你可能感兴趣的:(使用ifstream来读取txt文件,控制…)