- 论坛徽章:
- 0
|
本帖最后由 FaintKnowledge 于 2012-12-31 10:51 编辑
回复 1# qw8990032
打开后依次读取每一行,一旦到达文件末尾就打印上次读取到缓冲区的数据。然后这个就是最后一行了。。。实现:- #include "stdlib.h"
- #include "stdio.h"
- int main()
- {
- FILE *fp;
- char a[1024];// gets the line,you can define 1024 2048 or ++ or --
- fp = fopen("test", "r");//open the file ,,you can replace test with temp.txt
- while(!feof(fp)) // if not reach the end ,then gets the next line
- {
- fgets(a,sizeof(a),fp); //get the current line
- if(feof(fp)) //if reach the end,then printf the last gets
- printf("%s",a);
- }
- fclose(fp);//close file
- }
复制代码 //添加了注释! |
|