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读取文件时的注意事项的更多相关文章

  1. php函数fgets读取文件

    如果一个文件比较大,可以考虑用fgets函数 下面是个例子: #文件作用:fgets读取文件 $start_time = microtime(true); $file_name = "a.t ...

  2. 【转】C#读取文件时的共享方式

    string sFileName = @"C:\Exchange.dat";System.IO.StreamReader file = new System.IO.StreamRe ...

  3. 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 ...

  4. No known class method for selector 'setImage:andName:'错误分析.//删除.h与.m文件时的注意事项

    CHENYILONG Blog No known class method for selector 'setImage:andName:'错误分析.//删除.h与.m文件时的注意事项         ...

  5. Python读取文件时出现UnicodeDecodeError 'gbk' codec can't decode byte 0x80 in position x

    Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据出现错误: UnicodeDecode ...

  6. Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案

    Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据 出现错误: UnicodeDecod ...

  7. 读取文件时,使用file.eof()判断结尾注意事项

    今天写一个小功能需要读取文件,在判断文件结尾时使用了以下语句: while(infile.eof() && infile.good()) { infile.read((); encod ...

  8. Java读取文件时第一行出现乱码“?”问号

    我们在使用Java在读取文件(txt.dat等)时,如果文件不是utf-8格式的话,读取结果会出现,中文字符变乱码的情况,所以一般在读取时转为UTF-8格式读取. 但这时会出现一种情况,第一次读取第一 ...

  9. nodejs读取文件时相对路径的正确写法(使用fs模块)

    在开发nodejs中,我们往往需要读取文件或者写入文件,最常用的模块就是fs核心模块.一个最简单的写入文件的代码如下(暂时不考虑回调函数): fs.readFile("./test.txt& ...

随机推荐

  1. [原创]C#按比例缩放窗体控件及字体

    按照比例缩放窗体控件及字体,如需等比例缩放,只需将x,y的比例设置成相同即可. 为了减小误差,建议使用原始尺寸来计算比例. private float X, Y; private bool b = f ...

  2. 与您共享Linux Kernel 4.8分支首个维护版本

    导读 Linux Kernel 4.8正式版于10月2日由Linus Torvalds发布,带来了包括AMDGPU OverDrive支持.NVIDIA Pascal支持.AMDGPU PowerPl ...

  3. 【56测试】【字符串】【dp】【记忆化搜索】【数论】

    第一题:神秘大门 大意: 两个字符串A,B,按字典序最大的顺序输出B 的每个字符在A 中的位置,如果B不全在A中,输出No,否则Yes. 解: 这道题就是一遍的扫描,因为要按字典序最大的输出,所以从后 ...

  4. Ubuntu user switch

    To list all users you can use: cut -d: -f1 /etc/passwd To add a new user you can use: sudo adduser n ...

  5. QT中QWS的含义 (转至 宋金时的专栏

    QT 编程和文档中的术语QWS的全称是Qt windows system,是QT自行开发的窗口系统,体系结构类似X Windows,是一个C/S结构,由QWS Server在物理设备上显示,由QWS ...

  6. Android常见控件— — —ProgressBar

    ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据. <?xml version="1.0" encoding="utf-8" ...

  7. CSS实现垂直水平居中

    HTML结构: <div class="wrapper"> <div class="content"></div> < ...

  8. 【Selenium2+Python】常用操作

    Webdriver中比较常用的操作元素的方法: clear()    清除输入框的默认内容 send_keys("xxx")    在一个输入框里输入xx内容 ——如果输入中文,则 ...

  9. 初试微信小程序

    2016年11月3日,微信小程序终于公测了,大家可以正式开发了.早在这之前,应公司要求,和同事就早早的试了一下微信小程序的开发,特此记录一下: 微信官方小程序文档:https://mp.weixin. ...

  10. LPTHW 笨方法学习python 16章

    根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾 ...