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 ...
随机推荐
- Python爬虫常用之登录(二) 浏览器模拟登录
浏览器模拟登录的主要技术点在于: 1.如何使用python的浏览器操作工具selenium 2.简单看一下网页,找到帐号密码对应的框框,要知道python开启的浏览器如何定位到这些 一.使用selen ...
- V1-bug Alpha阶段项目展示
V1-bug Alpha阶段项目展示 团队成员简介 Name Summary Sefie wxmwy V1-bug制造公司资深工程师精通各种抱大腿方式团队吉祥物 182 面面俱到流派一丝不苟 Powe ...
- Linux C代码 获取IP地址
Ubuntu 16.04下,可编译通过: #include <stdio.h> #include <ifaddrs.h> #include <arpa/inet.h> ...
- python爬虫专栏学习
知乎的一个讲python的专栏,其中爬虫的几篇文章,偏入门解释,快速看了一遍. 入门 爬虫基本原理:用最简单的代码抓取最基础的网页,展现爬虫的最基本思想,让读者知道爬虫其实是一件非常简单的事情. 爬虫 ...
- day1-Python擅长的领域+学习内容
Python擅长的领域 WEB开发 Django Pyramid Tornado Bottle Flask WebPy 网络编程 Twisted Re ...
- Oracle Schema
1.这是Schema的definition: A schema is a collection of database objects (used by a user.) Schema objects ...
- MySQL修改数据表
ALTER [IGNORE] table tb_name alter_spec,alter_spec......... alter_specification: ADD [COLUMN] create ...
- maven多层项目配置
今天遇到一个maven项目有3个子项目的配置问题,一开始项目结构是混乱的,而且包引入不能正常解析. 主项目上右键,选择configure->configure and detect nested ...
- Git学习系列之一些常用的Git命令收录更新ing
不多说,直接上干货! 前言 对于Git工具,有必要整理和总结一些常用实用的命令. http://p.primeton.com/articles/53cce3a3e138236138000026 ht ...
- Orcale 之子查询
子查询和连接查询一样,都提供了使用单个查询访问多个表中的数据的方法.子查询在其他查询的基础上,提供一种进一步有效的方式来访问数据. IN 关键字 使用 IN 关键字可以将原表中特定的的值与子查询中返回 ...