美文网首页
026. C语言基础

026. C语言基础

作者: PYGY | 来源:发表于2017-06-21 20:10 被阅读0次
//   251.c
#include<stdio.h>
void main()
{
   FILE *fp;
   char x;
   fp=fopen("abc.txt","r");
   while((x=fgetc(fp))!=EOF)
   {
      putchar(x);
   }
   fclose(fp);
   getch();
}
//   252.c
#include<stdio.h>
void main(int argc,char *argv[])
{
   FILE *fp1,*fp2;
   fp1=fopen(argv[1],"rb");
   fp2=fopen(argv[2],"wb");
   while(!feof(fp1))
   {
      fputc(fgetc(fp1),fp2);
   }
   fclose(fp1);
   fclose(fp2);
   getch();
}
//  253.c
#include<stdio.h>
void main()
{
  FILE *fp;
  int a[10]={1,2,3,4,5,6,7,8,9,10},*p;
  fp=fopen("abc.txt","wb");
  p=a;
  while(p<a+10)
  {
     fwrite(p,sizeof(int),1,fp);
     p++;
  }
  fclose(fp);
  getch();
}
//   254.c
#include<stdio.h>
void main()
{
   FILE *fp;
   int a[10],*p;
   p=a;
   fp=fopen("abc.txt","rb");
   while(p<a+10)
   {
      fread(p,sizeof(int),1,fp);
      p++;
   }
   fclose(fp);
   for(p=a;p<a+10;p++)
     printf("%3d",*p);
   getch();
}
//   255.c
#include<stdio.h>
struct node
{
   int num;
   char name[20];
   int score;
};
void main()
{
   struct node x[3]={1,"x001",89,
                     2,"x002",98,
                     3,"x003",89},*p;
   FILE *fp;
   if((fp=fopen("abc.txt","wb"))==NULL)
   {
      printf("error");exit(1);
   }
   for(p=x;p<x+3;p++)
     fwrite(p,sizeof(struct node),1,fp);
   fclose(fp);
}
//  256.c
#include<stdio.h>
struct node
{
   int num;
   char name[20];
   int score;
};
void main()
{
   struct node x[3],*p;
   FILE *fp;
   if((fp=fopen("abc.txt","rb"))==NULL)
   {
      printf("error");exit(1);
   }
   for(p=x;p<x+3;p++)
     fread(p,sizeof(struct node),1,fp);
   for(p=x;p<x+3;p++)
     printf("%d:%s %d\n",p->num,p->name,p->score);
   fclose(fp);
   getch();
}
//   257.c
#include<stdio.h>
void main()
{
   FILE *fp;
   int n;
   //scanf("%d",&n);
   if((fp=fopen("abc.txt","rb"))==NULL)
     {printf("error");exit(1);}
   //putw(n,fp);
   n=getw(fp);
   printf("%d",n);
   fclose(fp);
   getch();
}
//   258.c
#include<stdio.h>
void putfloat(float n,FILE *fp)
{
   char *a;
   int i;
   a=(char *)&n;
   for(i=0;i<4;i++)
     fputc(a[i],fp);
}
float getfloat(FILE *fp)
{
   float n;
   int i;
   char *a;
   a=(char *)&n;
   for(i=0;i<4;i++)
     a[i]=fgetc(fp);
   return n;
}
void main()
{
   float n;
   FILE *fp;
   //scanf("%f",&n);
   fp=fopen("abc.txt","rb");
   //putfloat(n,fp);
   n=getfloat(fp);
   printf("%f",n);
   fclose(fp);
   getch();
}
//   259.c
#include<stdio.h>
void main()
{
   FILE *fp;
   char a[20];
   fp=fopen("abc.txt","r");
   fgets(a,15,fp);
   puts(a);
   getch();
}

//  260.c
#include<stdio.h>
void main()
{
   FILE *fp;
   //char a[20];
   fp=fopen("abc.txt","w");
   fputs("how do you turn this on",fp);
   //puts(a);
   fclose(fp);
   getch();
}

相关文章

网友评论

      本文标题:026. C语言基础

      本文链接:https://www.haomeiwen.com/subject/tuwtcxtx.html