zoukankan      html  css  js  c++  java
  • 6月12日C代码

      1 //-------------------------------------------1
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 void main()
      5 {
      6     FILE *fp;
      7     int x;
      8     if((fp=fopen("f1.txt","w+"))==NULL)//打开(创建)文件为写入数据做准备
      9     {
     10         printf("Can't open this file!\n");
     11         exit(1);
     12     }
     13     printf("input x:");
     14     scanf("%d",&x);
     15     fprintf(fp,"%6d",x);//将变量x中的值写入文件
     16     fclose(fp);//关闭文件
     17 
     18     if((fp=fopen("f1.txt","r+"))==NULL)//再打开文件为写入数据做准备
     19     {
     20         printf("Can't open this file!\n");
     21         exit(2);
     22     }
     23     fscanf(fp,"%d",&x);//由文件读出整数给变量x;
     24     printf("%6d",x);
     25     printf("\n");
     26     fclose(fp);//再关闭文件
     27 }
     28 //---------------------------------------------------------2
     29 /*
     30 打开文件函数fopen().关闭文件函数fclose().
     31 字符读取函数fgetc和fputc
     32 字符串读取函数fgets和fputs
     33 格式化读取函数fscanf和fprintf
     34 数据块读取函数freed和fwrite
     35 检测文件结束函数feof(),eof()
     36 */
     37 #include<stdio.h>
     38 #include<stdlib.h>
     39 void main()
     40 {
     41     FILE *fp;//定义文件指针
     42     char ch,filename[10];//定义字符变量和字符数组,字符数组用来放文件名
     43     gets(filename);//接受来自键盘输入的文件名
     44     if((fp=fopen(filename,"w"))==NULL)//打开文件并判断是否成功
     45     {
     46         printf("can not open file\n");
     47         exit(1);//打开文件失败结束过程,exit()函数在stdlib.h中
     48     }
     49     printf("Please input string:");
     50     while ((ch=getchar())!='#')//循环接受字符,并判断是否是结束符
     51         fputc(ch,fp);//将字符写入文件
     52         fclose(fp);//关闭文件
     53 }
     54 //-------------------------------------------3
     55 #include"stdio.h"
     56 #include<stdlib.h>
     57 void main()
     58 {
     59     FILE *in,*out;
     60     char ch,infile[10],outfile[10];
     61     scanf("%s",infile);
     62     scanf("%s",outfile);
     63     if((in=fopen(infile,"r"))==NULL)
     64     {
     65         printf("can not open infile\n");
     66         exit(1);
     67     }
     68         if((out=fopen(outfile,"w"))==NULL)
     69         {
     70             printf("cannot open outfile\n");
     71             exit(1);
     72         }
     73         while((ch=fgetc(in))!=EOF)
     74             fputc(ch,out);
     75         fclose(in);
     76         fclose(out);
     77 }
     78 //-------------------------------------4
     79 #include<stdio.h>
     80 #include<string>
     81 void main()
     82 {
     83     FILE *fp;
     84     char string[81];//
     85     if((fp=fopen("file1.txt","w"))==NULL)
     86     {
     87         printf("can't open file");
     88         exit(1);
     89     }
     90     while(strlen(gets(string))>0)
     91     {
     92         fputs(string,fp);
     93         fputs("\n",fp);
     94     }
     95     fclose(fp);
     96     if((fp=fopen("file1.txt","r"))==NULL)
     97     {
     98         printf("can't open file");
     99         exit(1);
    100     }
    101     while(fgets(string,81,fp)!=NULL)//while(!feof(fp))
    102 
    103         fputs(string,stdout);//将string指向的字符串输出到stdout(显示器)上
    104     fclose(fp);
    105 }
    106 //--------------------------------------5
    107 #include<stdio.h>
    108 #include<string>
    109 #define N 10
    110 void main()
    111 {
    112     FILE *fp;
    113     int i,x;
    114     if((fp=fopen("f1.txt","w+"))==NULL)
    115     {
    116         printf("Can't open this file!\n");
    117         exit(1);
    118     }
    119     for(i=0;i<N;i++)
    120     {
    121         scanf("%d,&x");
    122         fprintf(fp,"%6d",x);
    123     }
    124     fclose(fp);
    125     if((fp=fopen("f1.txt","r+"))==NULL)
    126     {
    127         printf("Can't open this file!\n");
    128         exit(2);
    129     }
    130     for(i=0;i<N;i++)
    131     {
    132         fscanf(fp,"%d",&x);
    133         printf("%6d",x);
    134     }
    135     printf("\n");
    136     fclose(fp);
    137 }
    138 //-----------------------------------------------6
    139 #include<stdio.h>
    140 #include<string>
    141 struct stu
    142 {
    143     int num;
    144     char name[10];
    145     int score[3];
    146 }student;
    147 void main()
    148 {
    149     int i;
    150     FILE *fp;
    151     if((fp=fopen("list1","ab"))==NULL)
    152     {
    153         printf("cannot open file\n");
    154         exit(1);
    155     }
    156     printf("student information:\n");
    157     printf("num:\n");
    158     scanf("%d",&student.num);
    159     printf("name:\n");
    160     scanf("%s",&student.name);
    161     printf("score1:\n");
    162     scanf("%d",&student.score[0]);
    163     printf("score2:\n");
    164     scanf("%d",&student.score[1]);
    165     printf("score3:\n");
    166     scanf("%d",&student.score[2]);
    167     if(fwrite(&student,sizeof(struct stu),1,fp)!=1)//写一个学生数据块文件
    168         printf("file write error:\n");
    169     fclose(fp);
    170 }
    171 //----------------------------------------------------7
    172 #include<stdio.h>
    173 #include<stdlib.h>
    174 #define N 3
    175 struct stu
    176 {
    177     int num;
    178     char name[10];
    179     int score[3];
    180 }students[N];
    181 void main()
    182 {
    183     FILE*fp;
    184     int i;
    185     if((fp=fopen("list ","rb"))==NULL)//以读方式打开二进制文件
    186     {
    187         printf("cannot open file!\n");
    188         exit(1);
    189     }
    190     for(i=0;i<N;i++)
    191     {
    192         fseek(fp,i*sizeof(struct stu),0);//以起点为基准,按学生数据块移动位置指针
    193         fread(&students[i],sizeof(struct stu),1,fp);//读出一个学生数据
    194         printf("%d,%s, %d,%d\n",students[i].num,students[i].name),students[i].score[0],students[i].score[1],students[i].score[2];);
    195     }
    196     fclose(fp);
    197 }
    198 //-------------------------------------------8
    199 #include"stdio.h"
    200 main()
    201 {
    202     FILE *fp1,*fp2;
    203     char c;
    204     fp1=fopen("work1","r");
    205     fp2=fopen("f2.txt","w");
    206     while((c=getc(fp1))!=EOF)
    207         putchar(c);//读出work1中的信息并写到屏幕上
    208     rewind(fp1);//把文件的内部指针移到文件的开始处
    209     while((c=fgetc(fp1))!=EOF)//读出work1中的信息并写入到f2中
    210         fputc(c,fp2);
    211     fclose(fp1);
    212     fclose(fp2);
    213 }
    214 //-----------------------------------9
    215 #include<stdio.h>
    216 #include<stdlib.h>
    217 #define N 3
    218 struct stu
    219 {
    220     int num;
    221     char name[10];
    222     int score[3];
    223 }student[N];
    224 void main()
    225 {
    226     FILE*fp;
    227     int i;
    228     for(i=0;i<N;i++)
    229     {
    230         printf("%d student information:\n");
    231         printf("num:\n");
    232         scanf("%d",&students[i].num);
    233         printf("name:\n");
    234         scanf("%s",students[i].name);
    235         printf("score1:\n");
    236         scanf("%d",students[i].score[0]);
    237         printf("score2:\n");
    238         scanf("%d",students[i].score[1]);
    239         printf("score3:\n");
    240         scanf("%d",students[i].score[2]);
    241     }
    242     if(fwrite(&students,sizeof(struct stu),N,fp)!=N)
    243         printf("file write error\n");
    244     fclose(fp);
    245     if((fp=fopen("list","rb"))==NULL)
    246     {
    247         printf("Can't open this file!\n");
    248             exit(1);
    249     }
    250     if(fread(&students,sizeof(struct stu),N,fp)!=N)
    251         printf("file write error\n");
    252     for(i=0;i<N;i++)
    253     {
    254         printf("%d,%s, %d,%d,%d\n",students[i].num,students[i].name,students[i].score[0],students[i].score[1],students[i].score[2]);
    255     }
    256     fclose(fp);
    257 }
    258 //-------------------------------------------10
    259 #include<stdio.h>
    260 #include<stdlib.h>
    261 #define N 3
    262 struct stu
    263 {
    264     int num;
    265     char name[10];
    266     int score[3];
    267 }students[N];
    268 void main()
    269 {
    270     FILE*fp;
    271     int i;
    272     for(i=0;i<N;i++)
    273     {
    274         printf("%d student information:\n");
    275         printf("num:\n");
    276         scanf("%d",&students[i].num);
    277         printf("name:\n");
    278         scanf("%s",students[i].name);
    279         printf("score1:\n");
    280         scanf("%d",students[i].score[0]);
    281         printf("score2:\n");
    282         scanf("%d",students[i].score[1]);
    283         printf("score3:\n");
    284         scanf("%d",students[i].score[2]);
    285     }
    286     if((fp=fopen("data.txt","w+"))==NULL)
    287     {
    288         printf("Can't open this file!\n");
    289         exit(1);
    290     }
    291     for(i=0;i<N;i++)
    292     {
    293         printf("%d,%s, %d,%d,%d\n",students[i].num,students[i].name,students[i].score[0],students[i].score[1],students[i].score[2]);
    294     }
    295     fclose(fp);
    296 }
  • 相关阅读:
    CodeForces 450
    CodeForces 400
    CodeForces 1
    [HDU POJ] 逆序数
    [HDU 1166] 敌兵布阵
    [转] 树状数组学习
    关于1月4日到1月7日
    [HDU 1565+1569] 方格取数
    [POJ 1459] Power Network
    [转] 网络流算法--Ford-Fulkerson方法及其多种实现
  • 原文地址:https://www.cnblogs.com/herizai/p/3132873.html
Copyright © 2011-2022 走看看