c/c++ 按照行读取文件
本文代码都在Windows/VC++6.0下测试过, 在linux/g++下也没有问题。
但是请一定注意linux和Windows文件格式的区别,比如:
1. 当linux上的代码读取Windows文件格式时, 读取结果的每行都会多一个\r, 想想为什么。
2. 当Windows上的代码读取linux格式文件时, 读取的结果会显示只有一行, 想想为什么。
先用C语言写一个丑陋的程序:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- FILE *fp;
- if(NULL == (fp = fopen("1.txt", "r")))
- {
- printf("error\n");
- exit(1);
- }
- char ch;
- while(EOF != (ch=fgetc(fp)))
- {
- printf("%c", ch);
- }
- fclose(fp);
- return 0;
- }
你只能看到结果,却没法利用每一行。 我们来改为:
- // VC++6.0
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char szTest[1000] = {0};
- int len = 0;
- FILE *fp = fopen("1.txt", "r");
- if(NULL == fp)
- {
- printf("failed to open dos.txt\n");
- return 1;
- }
- while(!feof(fp))
- {
- memset(szTest, 0, sizeof(szTest));
- fgets(szTest, sizeof(szTest) - 1, fp); // 包含了换行符
- printf("%s", szTest);
- }
- fclose(fp);
- printf("\n");
- return 0;
- }
这样, 我们就是整行读取了。
感觉C的读取方法有点丑陋,还是看看C++吧(只要文件格式Windows/linux和编译平台Windows/linux对应一致, 就放心用吧):
- #include <fstream>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- ifstream in("1.txt");
- string filename;
- string line;
- if(in) // 有该文件
- {
- while (getline (in, line)) // line中不包括每行的换行符
- {
- cout << line << endl;
- }
- }
- else // 没有该文件
- {
- cout <<"no such file" << endl;
- }
- return 0;
- }
当然,你可以对上述程序进行修改,让1.txt中的每一行输入到2.txt中,如下:
- #include <fstream>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- ifstream in("1.txt");
- ofstream out("2.txt");
- string filename;
- string line;
- if(in) // 有该文件
- {
- while (getline (in, line)) // line中不包括每行的换行符
- {
- cout << line << endl;
- out << line << endl; // 输入到2.txt中
- }
- }
- else // 没有该文件
- {
- cout <<"no such file" << endl;
- }
- return 0;
- }
结果, 2.txt和1.txt中的内容完全一致,你可以用Beyond Compare比较一下,我比较过了。
看来上述程序还能实现文件的复制呢,如下:
- #include <fstream>
- #include <string>
- #include <iostream>
- using namespace std;
- void fileCopy(char *file1, char *file2)
- {
- // 最好对file1和file2进行判断
- ifstream in(file1);
- ofstream out(file2);
- string filename;
- string line;
- while (getline (in, line))
- {
- out << line << endl;
- }
- }
- int main()
- {
- fileCopy("1.txt", "2.txt");
- return 0;
- }
当然了,上述程序只能针对文本文件(不仅仅是.txt),对其它类型的文件,不适合
c/c++ 按照行读取文件的更多相关文章
- C++/Php/Python/Shell 程序按行读取文件或者控制台
写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...
- Python跳过第一行读取文件内容
Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...
- python_基础学习_01_按行读取文件的最优方法
python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...
- Java利用内存映射文件实现按行读取文件
我们知道内存映射文件读取是各种读取方式中速度最快的,但是内存映射文件读取的API里没有提供按行读取的方法,需要自己实现.下面就是我利用内存映射文件实现按行读取文件的方法,如有错误之处请指出,或者有更好 ...
- python 按每行读取文件怎么去掉换行符
python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...
- Shell按行读取文件的3种方法
Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- shell脚本,按行读取文件的几种方法。
第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...
- C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...
- C++ 按行读取文件并打印
#include<iostream> #include<fstream> #include<string> #include <vector> #inc ...
随机推荐
- stark - 2 ⇲路由分发
在介绍前面三个注意点后,开始写stark组件内容. from django.apps import AppConfig from django.utils.module_loading import ...
- P3648 [APIO2014]序列分割
传送门 首先容易证明,得分和切的顺序没有关系 所以直接默认先切左边再切右边就好了 然后显然可以 $dp$ 一开始想的是设 $f[i][j]$ 表示切了 $i$ 次,此次把 $j$ 和 $j+1$ 分开 ...
- 在线词云制作tagxedo
最近在用python制作词云的时候发现了一个更加方便快捷很好玩的词云制作网站 http://www.tagxedo.com/app.html 所以今天就来大致介绍下是怎么使用的 1.先来介绍下tagx ...
- maven——添加插件和添加依赖有什么区别?
依赖:运行时开发时都需要用到的jar包,比如项目中需要一个Json的jar包,就要添加一个依赖,这个依赖在项目运行时也需要,因此在项目打包时需要把这些依赖也打包进项目里: 插件:在项目开的发时需要,但 ...
- SQL语句exists用法
首先头脑中有三点概念: 1 . EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2 . 建立程序循环的概念,这是一个动态的查 ...
- (转)MySQL- 5.7 sys schema笔记,mysql-schema
原文:http://www.bkjia.com/Mysql/1222405.html http://www.ywnds.com/?p=5045 performance_schema提供监控策略及大量监 ...
- 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 ...
- Firebird Case-Insensitive Searching 大小写不敏感查找
Firebird 默认是大小写敏感,在检索的时候. 要想不敏感检索,两种方法: 1.where upper(name) = upper(:flt_name) 2.检索时指定字符集collation,例 ...
- uploadify 图片上传
遇到的问题总结: 1.//图片排序 $("#pics").sortable(); 2.//上传的文件对象名,与后台所传参数名保持一致,最初因为这个名称错误浪费了许久时间 fileO ...
- SQL 除去回车符 除去空格符
update table set fa=replace(fa,chr(13),'') ; --- 除去回车符 update table set fa=replace(fa,' ','') ; --- ...