fgets读取文件时的注意事项
1 文本文件 a.txt 内容如下
2 c代码
FILE *fil;
if (!(fil = fopen("/home/rudy/projects/paser/a.txt", "rb"))) {
printf("File I/O error,the File is not exist\n");
exit(0);
}
int line = 0; while(!feof(fil)) {
if (fgetc(fil) == '\n') {
++line;
}
}
++line; fclose(fil);
if (!(fil = fopen("/home/rudy/projects/paser/a.txt", "rb"))) {
printf("File I/O error,the File is not exist\n");
exit(0);
}
char ** all =(char **) malloc( line * sizeof(char *)); for (int iCurrLineIndex = 0; iCurrLineIndex < line; ++iCurrLineIndex) { all[iCurrLineIndex] = (char *) malloc(20 ); fgets(all[iCurrLineIndex], 21, fil);
} for (int iCurrLineIndex = 0; iCurrLineIndex < line; ++iCurrLineIndex) {
printf("this is len = %d \n",strlen(all[iCurrLineIndex]));
for(int i = 0;i <11;i++) {
if (all[iCurrLineIndex][i] == '\0')
{
printf("%s\n ","this is 0");
}else if ( all[iCurrLineIndex][i]== '\n')
{
printf("%s\n ","this is n");
}else{
printf("%c ",all[iCurrLineIndex][i]);
}
}
printf("\n"); }
输出结果
this is len = 10
1 2 3 4 5 6 7 8 9 this is n
this is 0 this is len = 10
a b c d e f g h i this is n
this is 0 this is len = 9
1 2 3 4 5 6 7 8 9 this is 0
this is 0
总结: fgets读取时,如果指定的读取大小,小于实际行大小 那么 不添加\n做结尾,使用\0 ,然后接着读取没读完的当前行数据作为新的一行开始
fgets读取时,如果指定读取大小.大于实际行大小,那么将\n添加到末端.再添加\0
\0不算做有效长度里的元素,\n算有效长度里元素
fgets读取文件时的注意事项的更多相关文章
- php函数fgets读取文件
如果一个文件比较大,可以考虑用fgets函数 下面是个例子: #文件作用:fgets读取文件 $start_time = microtime(true); $file_name = "a.t ...
- 【转】C#读取文件时的共享方式
string sFileName = @"C:\Exchange.dat";System.IO.StreamReader file = new System.IO.StreamRe ...
- python在读取文件时出现 'gbk' codec can't decode byte 0x89 in position 68: illegal multibyte sequence
python在读取文件时出现“UnicodeDecodeError:'gbk' codec can't decode byte 0x89 in position 68: illegal multiby ...
- No known class method for selector 'setImage:andName:'错误分析.//删除.h与.m文件时的注意事项
CHENYILONG Blog No known class method for selector 'setImage:andName:'错误分析.//删除.h与.m文件时的注意事项 ...
- Python读取文件时出现UnicodeDecodeError 'gbk' codec can't decode byte 0x80 in position x
Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据出现错误: UnicodeDecode ...
- Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案
Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据 出现错误: UnicodeDecod ...
- 读取文件时,使用file.eof()判断结尾注意事项
今天写一个小功能需要读取文件,在判断文件结尾时使用了以下语句: while(infile.eof() && infile.good()) { infile.read((); encod ...
- Java读取文件时第一行出现乱码“?”问号
我们在使用Java在读取文件(txt.dat等)时,如果文件不是utf-8格式的话,读取结果会出现,中文字符变乱码的情况,所以一般在读取时转为UTF-8格式读取. 但这时会出现一种情况,第一次读取第一 ...
- nodejs读取文件时相对路径的正确写法(使用fs模块)
在开发nodejs中,我们往往需要读取文件或者写入文件,最常用的模块就是fs核心模块.一个最简单的写入文件的代码如下(暂时不考虑回调函数): fs.readFile("./test.txt& ...
随机推荐
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- CSS 高级技巧汇总
在我们平时写代码的时候没有没有掌握一些CSS技巧呢? 今天给大家分享一个<CSS 高级技巧汇总让你的代码简洁高效>.大家务必掌握这些小技巧,会让你非常高效率的写出网页的. ◆使用 :not ...
- 解决html中 在不同浏览器中占位大小不统一的问题
直接在html文档中使用 来表示空格,在不同浏览器中的占位大小是不一样的. 为什么呢,因为不同浏览器默认的字体是不一样的,不同字体下的空格表示 占位大小不一致. 这就好办了嘛,我们对 指定使用同样的字 ...
- 如何在CentOS 7.x中安装OpenERP(Odoo)
各位好,这篇教程关于的是如何在CentOS 7中安装Odoo(就是我们所知的OpenERP).你是不是在考虑为你的业务安装一个不错的ERP(企业资源规划)软件?那么OpenERP就是你寻找的最好的程序 ...
- 设置ubuntu12.04桌面版开机进入命令行模式
1)命令:sudo gedit /etc/default/grub 找到GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 将"quite ...
- 另一种图片上传 jquery.fileupload.js
今天遇到另外一种上传图片方法 用jquery.fileupload.js <input type="file" name="file[]" multipl ...
- understanding Nhibernate Hilo
http://stackoverflow.com/questions/2738671/please-explain-nhibernate-hilo http://stackoverflow.com/q ...
- H.264 / MPEG-4 Part 10 White Paper-翻译
1. Introduction Broadcast(广播) television and home entertainment(娱乐) have been revolutionised(彻底改变) b ...
- HDU 3652 B-number
也是数位dp.考虑反面会简单很多. #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- reverse-daily(1)-audio_visual_receiver_code
本人第一篇随笔,就以一篇CTF逆向分析的文章开始吧! 链接:http://pan.baidu.com/s/1eS6xFIa 密码:u14d 因为re的分析比较琐碎,所以主要就挑一些重点东西来说. 据说 ...