本文代码都在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. stark - 2 ⇲路由分发

    在介绍前面三个注意点后,开始写stark组件内容. from django.apps import AppConfig from django.utils.module_loading import ...

  2. P3648 [APIO2014]序列分割

    传送门 首先容易证明,得分和切的顺序没有关系 所以直接默认先切左边再切右边就好了 然后显然可以 $dp$ 一开始想的是设 $f[i][j]$ 表示切了 $i$ 次,此次把 $j$ 和 $j+1$ 分开 ...

  3. 在线词云制作tagxedo

    最近在用python制作词云的时候发现了一个更加方便快捷很好玩的词云制作网站 http://www.tagxedo.com/app.html 所以今天就来大致介绍下是怎么使用的 1.先来介绍下tagx ...

  4. maven——添加插件和添加依赖有什么区别?

    依赖:运行时开发时都需要用到的jar包,比如项目中需要一个Json的jar包,就要添加一个依赖,这个依赖在项目运行时也需要,因此在项目打包时需要把这些依赖也打包进项目里: 插件:在项目开的发时需要,但 ...

  5. SQL语句exists用法

    首先头脑中有三点概念: 1 .  EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2 . 建立程序循环的概念,这是一个动态的查 ...

  6. (转)MySQL- 5.7 sys schema笔记,mysql-schema

    原文:http://www.bkjia.com/Mysql/1222405.html http://www.ywnds.com/?p=5045 performance_schema提供监控策略及大量监 ...

  7. idea 下 启动maven项目,mybatis报错 Error parsing SQL Mapper Configuration. Cause: java.io.IOException。。。。。

    我的具体报错日志是   Error parsing SQL Mapper Configuration. Cause: java.io.IOException  Could not find resou ...

  8. Firebird Case-Insensitive Searching 大小写不敏感查找

    Firebird 默认是大小写敏感,在检索的时候. 要想不敏感检索,两种方法: 1.where upper(name) = upper(:flt_name) 2.检索时指定字符集collation,例 ...

  9. uploadify 图片上传

    遇到的问题总结: 1.//图片排序 $("#pics").sortable(); 2.//上传的文件对象名,与后台所传参数名保持一致,最初因为这个名称错误浪费了许久时间 fileO ...

  10. SQL 除去回车符 除去空格符

    update table set fa=replace(fa,chr(13),'') ; --- 除去回车符 update table set fa=replace(fa,' ','') ; --- ...