我的C/C++之路-011课(读取文件)

来源:岁月联盟 编辑:猪蛋儿 时间:2012-05-05

上一篇:http://www.2cto.com/kf/201205/130088.html

现在讲些文件操作,一个个字节读取文件,其实很简单,相信大家都会,例子分开讲,不然大家没心情看很长的代码

 

[cpp]
#include<stdlib.h> 
#include<stdio.h> 
 
void readFile() 

    FILE *file;//文件指针 
    char c;//一个个字符读取 
    file = fopen("c://test.txt","r");//打开文件 
    if(NULL==file)//判断是否打开成功 
    { 
        printf("can't open this file,make sure the file exist!!!/n"); 
        exit(1); 
    } 
    puts("open file successful!!!"); 
    puts("READ FILE NOW:"); 
 
    c = fgetc(file); 
    while(c!=EOF)//文件尾的标志为EOF 
    { 
        putchar(c); 
        c = fgetc(file); 
    } 
    printf("/n"); 
    fclose(file);//记得关闭文件 


/



摘自 mzlqh的专栏

图片内容