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& ...
随机推荐
- 总结七条助你成为Linux高手的超棒忠告
起初Linux对于我来说其实是很纠结的,因为很早以前就听说过.也曾见各种技术大牛使用过,但是一直觉得非常高深而没有去正式接触.两年前随着自己工作愈发的乏味,又看到了一篇叫做"虽然我是医生,但 ...
- What is Requirement ?
The IEEE 610 standard defines a requirement as: (1). a condition or capability needed by a user to s ...
- [蟒蛇菜谱]Python获取任意xml节点的值
# -*- coding: utf-8 -*- import xml.dom.minidom ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE class Simple ...
- Linux filesystem detection
16 down vote accepted The reason you can't find it is because, for the most part, it's not in the ke ...
- js:setTimeout 与 setInterval 比较
在javascript中有两个非常有用的函数:setTimeout 和setInterval ,都是定时器:但是两者存在着一些区别: 1. setTimeout函数 用法:setTimeout(fn, ...
- JQuery源码解析(十一)
内存泄露 什么是内存泄露? 内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束.在C++中,因为是手动管理内存,内存泄露是经常出现的事情.而现在流行的C#和Java等语言采用了自动 ...
- JavaScript原型学习笔记
1 理解JavaScript原型 什么是原型? 原型是一个对象,其他对象可以通过它实现属性继承. 任何一个对象都可以成为原型么? 是 哪些对象有原型 所有的对象在默认的情况下都有一个原型,因为原型本身 ...
- xpcall 安全调用
-- xpall (调用函数f, 错误函数fe[, 参数]) function fun(a,b) -- 这里的参数没什么实际作用,就是展示下用法 return a / bend -- xpc ...
- CSS transform-style属性实现3D效果
相对于transform-style:flat,在2d平面呈现,transform-style:preserve-3d则将所有子元素呈现在3d空间中. 实例: HTML: <div class= ...
- 数据库使用fmdb
#import "SQLdataManger.h" #import "FMDatabaseAdditions.h" static SQLdataManger * ...