如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)

原地址:http://blog.csdn.net/stpeace/article/details/12404925

项目中要用到,故此先练练。

 

     先用C语言写一个丑陋的程序:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int main()  
  4. {  
  5.     FILE *fp;  
  6.     if(NULL == (fp = fopen("1.txt""r")))  
  7.     {  
  8.         printf("error\n");  
  9.         exit(1);  
  10.     }  
  11.   
  12.     char ch;  
  13.     while(EOF != (ch=fgetc(fp)))  
  14.     {  
  15.         printf("%c", ch);  
  16.     }  
  17.   
  18.     fclose(fp);  
  19.   
  20.     return 0;  
  21. }  

     你只能看到结果,却没法利用每一行。C太丑陋了,还是用C++吧:

[cpp]  view plain copy
  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     string filename;  
  10.     string line;  
  11.   
  12.     if(in) // 有该文件  
  13.     {  
  14.         while (getline (in, line)) // line中不包括每行的<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E6%8D%A2%E8%A1%8C%E7%AC%A6&ie=utf-8&src=se_lighten_f">换行符</a>  
  15.         {   
  16.             cout << line << endl;  
  17.         }  
  18.     }  
  19.     else // 没有该文件  
  20.     {  
  21.         cout <<"no such file" << endl;  
  22.     }  
  23.   
  24.     return 0;  
  25. }  

     当然,你可以对上述程序进行修改,让1.txt中的每一行输入到2.txt中,如下:

[cpp]  view plain copy
  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     ofstream out("2.txt");  
  10.     string filename;  
  11.     string line;  
  12.   
  13.     if(in) // 有该文件  
  14.     {  
  15.         while (getline (in, line)) // line中不包括每行的换行符  
  16.         {   
  17.             cout << line << endl;  
  18.             out << line << endl; // 输入到2.txt中  
  19.         }  
  20.     }  
  21.     else // 没有该文件  
  22.     {  
  23.         cout <<"no such file" << endl;  
  24.     }  
  25.   
  26.     return 0;  
  27. }  

      结果, 2.txt和1.txt中的内容完全一致,你可以用Beyond Compare比较一下,我比较过了。

 

     看来上述程序还能实现文件的复制呢,如下:

[cpp]  view plain copy
  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. void fileCopy(char *file1, char *file2)  
  7. {  
  8.     // 最好对file1和file2进行判断  
  9.       
  10.     ifstream in(file1);  
  11.     ofstream out(file2);  
  12.     string filename;  
  13.     string line;  
  14.   
  15.     while (getline (in, line))  
  16.     {   
  17.         out << line << endl;  
  18.     }  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     fileCopy("1.txt""2.txt");  
  24.     return 0;  
  25. }  

     当然了,上述程序只能针对文本文件(不仅仅是.txt),对其它类型的文件,不适合。

要将上述字符转成整形数需要使用atoi(line.c_str());


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值