Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7534791
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-05-31 10:50:01

/*

 *  从键盘按字节读入至文件,再按字节从文件读出至屏幕

 *  Lzy    2011-5-28

 */

#include

 

int main(void)

{

    FILE *fp;

    char c;

 

    if((fp = fopen("F:\\lzy.txt","w+")) == NULL)      /*打开文件*/

    {

        printf("文件打开失败!\n");

        exit(0);

    }

 

    while((c = getchar()) != '\n')          /*从键盘得到字符*/

    {

        if(fputc(c,fp) == EOF)               /*按字节输入文件*/

        {

            printf("写入失败!\n");

            exit(0);

        }

    }

    rewind(fp);                               /*读写指针指向文件开头*/

 

    while(!feof(fp))                      /*判断是否读到文件未尾*/

        putchar(fgetc(fp));                  /*按字节读取文件,输出到屏幕*/

    putchar('\n');

 

    fclose(fp);                       /*关闭文件,释放空间*/

    return 0;

}

 

/*

 *  读取文件开头的第1 3 5 7 9个字节并输出

 *  Lzy    2011-5-28

 */

#include

 

int main(void)

{

    FILE *fp;

 

    if((fp = fopen("F:\\lzy.txt","r+")) == NULL)      /*打开文件*/

    {

        printf("文件打开失败!\n");

        exit(0);

    }  

 

    while(ftell(fp) < 10)                        /*读到第9个字节*/

    {

        putchar(fgetc(fp));                  /*按字节读取文件,输出到屏幕*/

        fseek(fp,1,SEEK_CUR);                /*向前移动读写指针,一个字节*/

    }

    putchar('\n');

 

    fclose(fp);                       /*关闭文件,释放空间*/

    return 0;

}

 

/*

 *  字符串写入、读取文件

 *  Lzy    2011-5-28

 */

#include

#include

 

int main(void)

{

    FILE *fp;

    char str[30] ={"I love this girl"};

 

    if((fp = fopen("F:\\lzy.txt","w+")) == NULL)      /*打开文件*/

    {

        printf("文件打开失败!\n");

        exit(0);

    }  

                           

    if(fputs(str,fp) != 0)           /*字符串写入文件*/

    {

        printf("写入失败!\n");

        exit(0);

    }

    memset(str,' ',sizeof(str));    /*清空缓冲区   */

    rewind(fp);                       /*重设读写位置*/

    fgets(str,30,fp);             /*从文件读出字符*/

    printf("%s\n",str);              /*打印到屏幕*/

    fclose(fp);                       /*关闭文件,释放空间*/

    return 0;

}

 

 

/*

 *  数据块输入输出

 *  Lzy    2011-5-28

 */

#include

#include

 

struct student

{

    int id;

    int score;

}stu;

 

int main(void)

{

    FILE *fp; 

 

    if((fp = fopen("F:\\lzy.txt","wb+")) == NULL)     /*打开文件*/

    {

        printf("文件打开失败!\n");

        exit(0);

    }  

    printf("输入学号 分数:");                    

    scanf("%d %d",&stu.id, &stu.score);

    fwrite(&stu, sizeof(stu), 1, fp);      /*数据写入文件*/

    memset(&stu,0,sizeof(stu));             /*结构体所有数据清0*/

    rewind(fp);                               /*文件读写指针指向文件头*/

    fread(&stu, sizeof(stu), 1, fp);       /*读出文件数据赋给stu*/

    printf("输入学号%d 分数%d\n",stu.id,stu.score);

 

    fclose(fp);                       /*关闭文件,释放空间*/

    return 0;

}

阅读(2801) | 评论(0) | 转发(3) |
0

上一篇:文件编程

下一篇:超市管理系统程序

给主人留下些什么吧!~~