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& ...
随机推荐
- [转]CSS,font-family,常用网页字体
http://www.zreading.cn/ican/2014/10/css-font-family/ CSS,font-family,好看常用的中文字体 2014-10-14 例1(小米米官网): ...
- debug实战:COM组件GetToSTA导致高内存+GC被阻塞
最近花了好几周解决一个WPF高内存的问题,问题的表象是内存不断增加.未被回收,根源是GC的FinalizeThread被阻塞,导致整个GC挂掉.从以下几步来分析这个问题: 1.用ANTS Memory ...
- Python的平凡之路(11)
一. rabbitmq 1 进程Queue: 父进程与子进程进行交互,或者同属于同一父进程下多个子进程进行交互 2 队列通信: send1.py #!/usr/bin/env python#Au ...
- samba 配置
sudo apt-get install samba sudo apt-get install kdenetwork-filesharing vi /etc/samba/smb.conf [Share ...
- Socket编程基础——无连接UDP
与面向连接的网络连接相比,无连接的网络通信不需要在服务器与客户端之间建立连接.面向非连接的Socket通信是基于UDP的,服务器端不需要调用listen()和accept()函数来等待客户端的连接:客 ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- jsp状态管理
http无状态协议 服务器记不住你 每次浏览器访问,服务器不会特点保存相应信息,故记不住你 jsp状态存储的两种机制 cookie 存储在客户端 用途: 1.简化登陆 2.追踪特定对象 3.保存用户常 ...
- [debian]SublimeText>PrettyCode無效
怣 apt-get install node http://nodejs.org/#download.
- Codeforces Round #163 (Div. 2)
A. Stones on the Table \(dp(i)\)表示最后颜色为\(i\)的最长长度. B. Queue at the School 模拟. C. Below the Diagonal ...
- android之ActionBar
最近忙着做项目了,很久么来博客园看看了.最近项目中用到了actionbar,那就依我个人之建,来跟大家谈谈吧. 首先最重的是看你自己所见的项目的最小Api是为11,(在设置Minsdkversion最 ...