本文代码都在Windows/VC++6.0下测试过, 在linux/g++下也没有问题。

但是请一定注意linux和Windows文件格式的区别,比如:

1. 当linux上的代码读取Windows文件格式时, 读取结果的每行都会多一个\r,  想想为什么。

2. 当Windows上的代码读取linux格式文件时, 读取的结果会显示只有一行, 想想为什么。

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

  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. char ch;
  12. while(EOF != (ch=fgetc(fp)))
  13. {
  14. printf("%c", ch);
  15. }
  16. fclose(fp);
  17. return 0;
  18. }

你只能看到结果,却没法利用每一行。 我们来改为:

  1. // VC++6.0
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. char szTest[1000] = {0};
  7. int len = 0;
  8. FILE *fp = fopen("1.txt", "r");
  9. if(NULL == fp)
  10. {
  11. printf("failed to open dos.txt\n");
  12. return 1;
  13. }
  14. while(!feof(fp))
  15. {
  16. memset(szTest, 0, sizeof(szTest));
  17. fgets(szTest, sizeof(szTest) - 1, fp); // 包含了换行符
  18. printf("%s", szTest);
  19. }
  20. fclose(fp);
  21. printf("\n");
  22. return 0;
  23. }

这样, 我们就是整行读取了。

感觉C的读取方法有点丑陋,还是看看C++吧(只要文件格式Windows/linux和编译平台Windows/linux对应一致, 就放心用吧):

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

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

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

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

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

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

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

c/c++ 按照行读取文件的更多相关文章

  1. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  2. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

  3. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  4. Java利用内存映射文件实现按行读取文件

    我们知道内存映射文件读取是各种读取方式中速度最快的,但是内存映射文件读取的API里没有提供按行读取的方法,需要自己实现.下面就是我利用内存映射文件实现按行读取文件的方法,如有错误之处请指出,或者有更好 ...

  5. python 按每行读取文件怎么去掉换行符

    python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...

  6. Shell按行读取文件的3种方法

    Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...

  7. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  8. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

  9. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  10. C++ 按行读取文件并打印

    #include<iostream> #include<fstream> #include<string> #include <vector> #inc ...

随机推荐

  1. Python爬虫常用之登录(二) 浏览器模拟登录

    浏览器模拟登录的主要技术点在于: 1.如何使用python的浏览器操作工具selenium 2.简单看一下网页,找到帐号密码对应的框框,要知道python开启的浏览器如何定位到这些 一.使用selen ...

  2. V1-bug Alpha阶段项目展示

    V1-bug Alpha阶段项目展示 团队成员简介 Name Summary Sefie wxmwy V1-bug制造公司资深工程师精通各种抱大腿方式团队吉祥物 182 面面俱到流派一丝不苟 Powe ...

  3. Linux C代码 获取IP地址

    Ubuntu 16.04下,可编译通过: #include <stdio.h> #include <ifaddrs.h> #include <arpa/inet.h> ...

  4. python爬虫专栏学习

    知乎的一个讲python的专栏,其中爬虫的几篇文章,偏入门解释,快速看了一遍. 入门 爬虫基本原理:用最简单的代码抓取最基础的网页,展现爬虫的最基本思想,让读者知道爬虫其实是一件非常简单的事情. 爬虫 ...

  5. day1-Python擅长的领域+学习内容

    Python擅长的领域 WEB开发 Django   Pyramid     Tornado       Bottle    Flask    WebPy 网络编程 Twisted        Re ...

  6. Oracle Schema

    1.这是Schema的definition: A schema is a collection of database objects (used by a user.) Schema objects ...

  7. MySQL修改数据表

    ALTER [IGNORE] table tb_name alter_spec,alter_spec......... alter_specification: ADD [COLUMN] create ...

  8. maven多层项目配置

    今天遇到一个maven项目有3个子项目的配置问题,一开始项目结构是混乱的,而且包引入不能正常解析. 主项目上右键,选择configure->configure and detect nested ...

  9. Git学习系列之一些常用的Git命令收录更新ing

    不多说,直接上干货!  前言 对于Git工具,有必要整理和总结一些常用实用的命令. http://p.primeton.com/articles/53cce3a3e138236138000026 ht ...

  10. Orcale 之子查询

    子查询和连接查询一样,都提供了使用单个查询访问多个表中的数据的方法.子查询在其他查询的基础上,提供一种进一步有效的方式来访问数据. IN 关键字 使用 IN 关键字可以将原表中特定的的值与子查询中返回 ...