c 读取文件

c读取一个文件,简单的显示在console上

#include <stdio.h>

#include <stdlib.h>

void main()

{

 char *pchBuf = NULL;

 int  length = 0;



 FILE *pf  = fopen(__FILE__, "r"); 

 if(pf==NULL) //如果失败了

 {

    printf("错误!");

    exit(1); //中止程序

 } 



 fseek(pf, 0, SEEK_END);

 length = ftell(pf);

 rewind(pf);



 pchBuf = (char*) malloc(sizeof(char)*length+1);

 length = fread(pchBuf, sizeof(char), length, pf);

 pchBuf[length] = '\0'; 

 printf("%s\n", pchBuf);



 fclose(pf); 

 free(pchBuf);

 scanf("%d",length);

}

你可能感兴趣的:(读取文件)