[代码实例][C语言]读取文本文件

来源:互联网 发布:linux配置tomcat和jdk8 编辑:程序博客网 时间:2024/06/06 05:05
#include <stdio.h>#include <stdlib.h>#include <string.h>#define BUF_SIZE    1024char buf[BUF_SIZE + 1];int main(int argc, char * argv[]){    if(argc != 2)    {        printf("Usage : %s <input_file>\n", argv[0]);        return EXIT_FAILURE;    }    FILE * fp;    char * file_name = argv[1];    size_t count;    if((fp = fopen(file_name, "r")) == NULL)    {        perror("fopen");        return EXIT_SUCCESS;    }    if((count = fread(buf, 1, BUF_SIZE, fp)) > 0)    {        buf[count] = '\0';        printf("%s", buf);    }    if(fclose(fp) != 0)    {        perror("fclose");        return EXIT_FAILURE;    }    return EXIT_SUCCESS;}
0 0
原创粉丝点击