歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> C 讀文件 Windows 與 Linux 的差別

C 讀文件 Windows 與 Linux 的差別

日期:2017/3/3 12:05:07   编辑:Linux技術

同樣的源代碼,同樣的編譯器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會多輸出一行

本文出自 “魂斗羅” 博客,請務必保留此出處http://990487026.blog.51cto.com/10133282/1786917

Copyright © Linux教程網 All Rights Reserved