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);
}
posted @ 2012-08-01 15:20  爱喝可乐  阅读(206)  评论(0编辑  收藏  举报