课外天地 李树青学习天地C语言程序设计 → [推荐]第十四课代码讲义:文件处理


  共有12333人关注过本帖树形打印复制链接

主题:[推荐]第十四课代码讲义:文件处理

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1939 积分:26594 威望:0 精华:34 注册:2003/12/30 16:34:32
[推荐]第十四课代码讲义:文件处理  发帖心情 Post By:2015/12/14 16:52:49 [只看该作者]

基本使用方法
录入键盘字符:
#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fp;
    char  ch , filename[10];
    scanf("%s", filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
    ch=getchar();
    ch=getchar();
    while(ch!='#')
    {
        fputc(ch,fp);
        putchar(ch);
        ch=getchar();
    }
    putchar(10);
    fclose(fp);
}

读文件:
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *in, *out;
    char ch,infile[10],outfile[10];
    printf("Enter the infile name:\n");
    scanf("%s",infile);
    if ((in = fopen(infile, "r"))== NULL)
    {
        printf("Cannot open infile.\n");
        exit(0);
    }
    while (!feof(in))
    {
        ch=fgetc(in);
        printf("%d,%c\n",ch,ch);
    }

    fclose(in);
    fclose(out);
}


复制文件:
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *in, *out;
    char ch,infile[10],outfile[10];
    printf("Enter the infile name:\n");
    scanf("%s",infile);
    printf("Enter the outfile name:\n");
    scanf("%s",outfile);
    if ((in = fopen(infile, "r"))== NULL)
    {
        printf("Cannot open infile.\n");
        exit(0);
    }
    if ((out = fopen(outfile, "w"))== NULL)
    {
        printf("Cannot open outfile.\n");
        exit(0);
    }
    while (!feof(in))
        fputc(fgetc(in), out);
    fclose(in);
    fclose(out);
}


结合字符串访问的登录提示
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    char filename[20]="d:\\name.txt";
    char name[100];
    if((file=fopen(filename,"r"))==NULL)
    {
        printf("你是第一次访问!\n请输入你的姓名:");
        file = fopen(filename, "w");
        scanf("%s",name);
        fputs(name, file);
        fclose(file);
    }
    else
    {
        fgets(name,100,file);
        printf("欢迎你!%s\n",name);
        fclose(file);
    }
}

统计登录次数:
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    char filename[20]="d:\\name.txt";
    char ch=1;
    if((file=fopen(filename,"r"))==NULL)
    {
        printf("你是第一次访问!\n");
        file = fopen(filename, "w");
        fputc(ch, file);
        fclose(file);
    }
    else
    {
        ch=fgetc(file);
        ch++;
        printf("你是第%d次访问!\n",ch);
        fclose(file);
        file = fopen(filename, "w");
        fputc(ch, file);
        fclose(file);
    }
}


读写二进制信息
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    float i=0;
    float j=1;
    char filename[20]="d:\\name.dat";
    char ch=1;
    if((file=fopen(filename,"wb"))==NULL)
    {
        return;
    }
    fwrite(&i,4,1,file);
    fclose(file);
    if((file=fopen(filename,"rb"))==NULL)
    {
        return;
    }
    printf("%f\n",j);
    fread(&j,4,1,file);
    printf("%f\n",j);
    fclose(file);

}

利用整数保存登录次数信息:
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    char filename[20]="d:\\name.dat";
    int count=1;
    if((file=fopen(filename,"rb"))==NULL)
    {
        printf("你是第一次访问!\n");
        file = fopen(filename, "wb");
        fwrite(&count,4,1,file);
        fclose(file);
    }
    else
    {
        fread(&count,4,1,file);
        count++;
        printf("你是第%d次访问!\n",count);
        fclose(file);
        file = fopen(filename, "wb");
        fwrite(&count,4,1,file);
        fclose(file);
    }
}

改进版本:
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    char filename[20]="d:\\name.dat";
    int count=1;
    if((file=fopen(filename,"rb"))==NULL)
    {
        printf("你是第一次访问!\n");
    }
    else
    {
        fread(&count,4,1,file);
        count++;
        printf("你是第%d次访问!\n",count);
        fclose(file);
    }
    file = fopen(filename, "wb");
    fwrite(&count,4,1,file);
    fclose(file);
}

使用专门为整数读写定义的函数
#include <stdio.h>
#include <stdlib.h>
void main()
{
    FILE *file;
    char filename[20]="d:\\name.dat";
    int count=1;
    if((file=fopen(filename,"rb"))==NULL)
    {
        printf("你是第一次访问!\n");
    }
    else
    {
        count=getw(file);
        count++;
        printf("你是第%d次访问!\n",count);
        fclose(file);
    }
    file = fopen(filename, "wb");
    putw(count,file);
    fclose(file);
}


结合插入结构体节点的文件读取练习
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)

struct student
{
    long num;
    float score;
    struct student *next;
};

char filename[20]="d:\\name.dat";

void main()
{
    struct student * insert(struct student *header,struct student *p0);
    void print(struct student *p);
    struct student *creat();
    struct student *read();
    void write(struct student * p);
    struct student *p;
    FILE *file;
    if((file=fopen(filename,"rb"))==NULL)
    {
        printf("你是第一次使用!\n");
        p=creat();
    }
    else
    {
        p=read();
    }
    print(p);
    //需要链表元素的学号升序排列
 struct student * p0=(struct student *)malloc(sizeof(struct student));
 printf("请输入要插入的学号和成绩:");
 scanf("%ld,%f",&p0->num,&p0->score);
    p=insert(p,p0);
    print(p);
    write(p);
}


void write(struct student * p)
{
    FILE *file;
    if((file=fopen(filename,"wb"))==NULL)
    {
        printf("打开失败!\n");
        return;
    }
    while(p!=NULL)
    {
        fwrite(p,LEN,1,file);
        p=p->next;
    }
    fclose(file);
}

struct student *read()
{
    FILE *file;
    if((file=fopen(filename,"rb"))==NULL)
    {
        printf("读取失败!\n");
        return;
    }
    struct student *head,*p1,*p2;
    head=p1=(struct student *) malloc(LEN);
    while(1)
    {
        fread(p1,LEN,1,file);
        if(feof(file))
            break;
        p2=p1;
        p1=(struct student *) malloc(LEN);
        p2->next=p1;
    }
    p2->next=NULL;
    fclose(file);
    return (head);
}

 

void print(struct student *p)
{
    while(p!=NULL)
    {
        printf("%ld  %5.1f\n",p->num,p->score);
        p=p->next;
    }
}

struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    int n=0;
    p1=p2=(struct student *) malloc(LEN);
    scanf("%ld,%f",&p1->num, &p1->score);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc (LEN);
        scanf("%ld,%f",&p1->num, &p1->score);
    }
    p2->next=NULL;
    return(head);
}

struct student * insert(struct student *header,struct student *p0)
{
    struct student *p1,*p2;
    p1=header;
 p2=header;
 while(1)
 {
  if(p1->num<p0->num)
  {
   p2=p1;
   p1=p1->next;
   if(p1==NULL)
   {
    p2->next=p0;
    p0->next=NULL;
    break;
   }
  }
  else if(p1->num>p0->num)
  {
   if(header==p1)
   {
    header=p0;
    p0->next=p1;
    break;
   }
   else
   {
    p2->next=p0;
    p0->next=p1;
    break;
   }
  }
  else
  {
   break;
  }

 }
 return header;
}

 

 

 

 

 

 

[此贴子已经被作者于2015-12-15 12:45:54编辑过]

 回到顶部