您的位置:首页 > 运维架构 > Linux

C 读文件 Windows 与 Linux 的差别

2016-06-07 13:57 351 查看
同样的源代码,同样的编译器gcc

源代码:
main.c

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *args[])
{
FILE *p1 = fopen("main.c","r");
char buf[1024] = {0};
while(!feof(p1))
{
fgets(buf,sizeof(buf),p1);
printf("%s",buf);
}
fclose(p1);
return 0;
}
// hello world


windows 运行:
D:\>gcc main.c & a.exe
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *args[])
{
FILE *p1 = fopen("main.c","r");
char buf[1024] = {0};
while(!feof(p1))
{
fgets(buf,sizeof(buf),p1);
printf("%s",buf);
}
fclose(p1);
return 0;
}
// hello world


Linux 运行:
chunli@ubuntu:~/tmp$ gcc  main.c && ./a.out
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *args[])
{
FILE *p1 = fopen("main.c","r");
char buf[1024] = {0};
while(!feof(p1))
{
fgets(buf,sizeof(buf),p1);
printf("%s",buf);
}
fclose(p1);
return 0;
}
// hello world
// hello world


可以看见,Linux比Windows会多输出一行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Windows C 读文件