fputc()和fgetc()应用

//从控制开输入字符,回显出来,并写入文件中
#include<stdio.h>
int main(int argc,char* argv[])
{
    
    FILE* fpOutput =fopen("out1","w");
    if(fpOutput==NULL)
    {
        printf("Open error\n");
        return 1;
    }

    int ch;
    //    while((ch=fgetc(stdin))!='\n')
    while((ch=getchar())!='\n')
    {
        fprintf(fpOutput,"%c",ch);
        //fputc(ch,fpOutput);
        //fputc(ch,stdout);
        putchar(ch);
    }
    
    fclose(fpOutput);
    
    return 0;
}

//从一个文件中读入内容,显示在控制台上,并另存到另一个文件中
#include<stdio.h>

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        printf("%s filename copyname\n",*argv);
        return 0;
    }

    FILE* fpInput =fopen(argv[1],"r");
    if(fpInput==NULL)
    {
        printf("Open error\n");
        return 1;
    }

    FILE* fpOutput =fopen(argv[2],"w");
    if(fpOutput==NULL)
    {
        printf("Write error\n");
        return 2;
    }

    int ch; 
    while((ch=fgetc(fpInput))!=EOF)//从一个文件中读
    {
        fprintf(fpOutput,"%c",ch);//存到另一个文件中
        fputc(ch,stdout); //输出到控制台
    }
    
    fclose(fpInput);
    fclose(fpOutput);
    
    return 0;
}
//内核会对每次读取的字符记住位置,读写记住的位置相同
#include<stdio.h>
int main(int argc,char* argv[])
{
   //新建文件,里面放上0到9; 
    FILE* fp = fopen("file","r+");//边读边写,采用r+方式打开
    if(fp==NULL)
    {
        printf("Open error\n");
        return 1;
    }
 
    int ch;
    while((ch=fgetc(fp))!=EOF)
    {
        fputc(ch,fp);
        putchar(ch);
    }
    
    fclose(fp);
    
    return 0;
}
//[root@orz 23:37 /home/user/pro]$echo {0..9} | tr -d ' ' >file
//[root@orz 23:37 /home/user/pro]$./a.out
//02468
//[root@orz 23:38 /home/user/pro]$cat file
//0022446688
//对比可以看出,每次读完之后,内核将偏移位置计数+1,写完之后内核将偏移位置计数也+1
//内核会对每次读取的字符记住位置,读写记住的位置相同
#include<stdio.h>
int main(int argc,char* argv[])
{
   //新建文件,里面放上0到9; 
    FILE* fp = fopen("file","r+");//边读边写,采用r+方式打开
    if(fp==NULL)
    {
        printf("Open error\n");
        return 1;
    }
    char ch;
    ch=getc(fp);
    putchar(ch);
    ch=getc(fp);
    putchar(ch);
    ch=getc(fp);
    putchar(ch);
    return 0;
}

[root@orz 01:42 /home/user/pro]$gcc main.c
[root@orz 01:43 /home/user/pro]$echo -n {a..z} |tr -d ' ' >file
[root@orz 01:43 /home/user/pro]$./a.out
abc[root@orz 01:43 /home/user/pro]$

int fseek(FILE *stream, long offset, int whence);

whence表示计数起点

SEEK_SET:行首

SEEK_CUR:当前位置

SEEK_END:行尾

offset:为偏移量

//内核会对每次读取的字符记住位置,读写记住的位置相同
#include<stdio.h>
int main(int argc,char* argv[])
{
   //新建文件,里面放上0到9; 
    FILE* fp = fopen("file","r+");//边读边写,采用r+方式打开
    if(fp==NULL)
    {
        printf("Open error\n");
        return 1;
    }
    char ch;
    fseek(fp,0,SEEK_SET);//文件从0开始计数
    ch=getc(fp);
    putchar(ch);
    
    fseek(fp,1,SEEK_SET);
    ch=getc(fp);
    putchar(ch);

    fseek(fp,-1,SEEK_END);//末尾换行符
    ch=getc(fp);
    putchar(ch);

    fseek(fp,-2,SEEK_END);
    ch=getc(fp);
    putchar(ch);
    
    fseek(fp,-3,SEEK_END);
    ch=getc(fp);
    putchar(ch);
    return 0;
}

[root@orz 01:32 /home/user/pro]$echo {a..z} |tr -d ' ' >file
[root@orz 01:32 /home/user/pro]$./a.out
ab
zy[root@orz 01:32 /home/user/pro]$

echo -n 没有换行符
[root@orz 01:34 /home/user/pro]$echo -n {a..z} |tr -d ' ' >file
[root@orz 01:35 /home/user/pro]$./a.out
abzyx[root@orz 01:35 /home/user/pro]$
//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
//fread/fwrite(内存地址,每个元素字节数,元素个数,文件) 
#include<stdio.h>
typedef struct person{
    char name[20];
    char gender;
    int age;
    double salary;
}person;

int main(int argc,char* argv[])
{
    person a[3]={
        {"zhangsan",'M',20,10000},
        {"lisi",'M',21,10086},
        {"wangwu",'M',22,10010}};
    FILE* fp = fopen("person.data","wb");//b,在任何系统里,原样存储,不作任何转换
    if(fp==NULL){
        printf("error open \n");
        return 1;
    }
    fwrite(a,sizeof(person),5,fp);
    fclose(fp);
    
    fp=fopen("person.data","rb");
    if(fp==NULL){
        printf("error open \n");
        return 1;
    }
    person b[3]={};
    fread(b,sizeof(person),3,fp);
    int i;
    for(i=0;i<3;i++)
        printf("name:%s,gender:%c,age:%d,salary:%g \n",b[i].name,b[i].gender,b[i].age,b[i].salary);
    fclose(fp);

    return 0;
}

#include<stdio.h>

int main(int argc,char* argv[])
{
    printf("digit and alpha mixtured Input:");
    char str[1000];
    double data;
    register int c;
    for(;;){
        c = getchar();
        if(c==EOF) break;
        if(isspace(c)) continue;
        ungetc(c,stdin);//getchar取走了一个字符,用ungetc把这个字符还回去
        if(c>='0' && c<='9'){
            scanf("%lf",&data);
            printf("data = %lf\n",data);
        }
        else{
            scanf("%s",str);
            printf("str = %s \n",str);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值