2.标准IO的字符输入和输出

字符的输入(读单个字符):

int  fgetc(FILE *stream);

 int  getc(FILE *stream);   //宏

 int  getchar(void);

成功时返回读取的字符;若到文件末尾或出错时返回EOF(-1),

getchar()等同于fgetc(stdin)

getc和fgetc区别是一个是宏一个是函数

注意事项:

1函数返回值是int类型不是char类型,主要是为了扩展返回值的范围。

2 tdin 也是FILE *的指针,是系统定义好的,指向的是标准输入(键盘输入)

3 打开文件后读取,是从文件开头开始读。读完一个后读写指针会后移。读写注意文件位置!

4 调用getchar会阻塞,等待你的键盘输入

字符的输出(写单个字符):

int  fputc(int c, FILE *stream);

int  putc(int c, FILE *stream);

int  putchar(int c);

成功时返回写入的字符;出错时返回EOF

putchar(c)等同于fputc(c, stdout)

注意事项:

1返回和输入参数都是int类型

2遇到这种错误:Bad file descriptor,  很可能是文件打开的模式错误(只读模式去写,只写模式去读)

行输入(读取整个行)

char  *gets(char *s);  读取标准输入到缓冲区s

char *fgets(char *s, int size, FILE *stream);

成功时返回s,到文件末尾或出错时返回NULL

遇到’\n’或已输入size-1个字符时返回,总是包含’\0’

注意事项:

1 gets函数已经被淘汰,因为会导致缓冲区溢出

2 fgets 函数第二个参数,输入的数据超出size,size-1个字符会保存到缓冲区,最后添加’\0’,如果输入数据少于size-1 后面会添加换行符。

行输出(写整行)

int  puts(const char *s);

int fputs(const char *s,  FILE *stream);

成功时返回非负整数;出错时返回EOF

puts将缓冲区s中的字符串输出到stdout,并追加’\n’

fputs将缓冲区s中的字符串输出到stream,不追加  ‘\n’

二进制读写

文本文件和二进制的区别:

存储的格式不同:文本文件只能存储文本。

计算机内码概念:文本符号在计算机内部的编码(计算机内部只能存储数字0101001....,所以所有符号都要编码)

二进制读写函数格式:

size_t fread(void *ptr, size_t size, size_t n, FILE *fp);

void *ptr  读取内容放的位置指针

  size_t size 读取的块大小

size_t n 读取的个数

  FILE *fp  读取的文件指针

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp);

void *ptr  写文件的内容的位置指针

size_t size 写的块大小

size_t n 写的个数

  FILE *fp  要写的文件指针

注意事项:

文件写完后,文件指针指向文件末尾,如果这时候读,读不出来内容。

解决办法:移动指针(后面讲解)到文件头;关闭文件,重新打开

fflush

#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[]){

//   printf("abcdefg");
//   fflush(stdout);
   FILE *fp;
   fp=fopen("1.txt","w");
   if(fp==NULL){
      perror("fopen");
      return 0;
   } 

   fwrite("abcdef",7,1,fp);
   

   while(1){

      sleep(1);
   }


}

fget_put

#include <stdio.h>

int main(int argc,char *argv[]){
   
    FILE *fp;
    int rec;
    fp = fopen("1.txt","a+");
    if(fp==NULL){
        perror("fopen");
        return 0;
    }

/*

    rec = fgetc(fp);
    if(rec==-1){
       perror("fgetc");
       fclose(fp);
       return 0;
    }
    printf("Get char=%c\n",rec);
    
    rec = getchar();
    printf("Get STD input=%c\n",rec);
*/

/*    fclose(fp);
    fp = fopen("1.txt","r");
    if(fp==NULL){
        perror("fopen");
        return 0;
    }

    
    rec = fgetc(fp);
    printf("Get char=%c\n",rec);
    rec = fgetc(fp);
    printf("Get char=%c\n",rec);
    rec = fgetc(fp);
    printf("Get char=%c\n",rec);

*/
    int wrc='w';
    rec = fputc(wrc,fp);
    if(rec==-1){
       perror("fputc");
       fclose(fp);
       return 0;
    }
    putchar(wrc);


    fclose(fp);
} 

fgets

#include <stdio.h>

int main(int argc,char *argv[]){
    FILE *fp;
    char *ret;
    int retn;
    char buff[100];
    fp = fopen("1.txt","a+");
    if(fp==NULL){
	perror("fopen");
        return 0;

    }

/*    
    ret = fgets(buff,5,fp);
    if(ret==NULL){
	perror("fgets");
        fclose(fp);
        return 0;
    }
    printf("buff=%s\n",buff);

*/

    retn = fputs("hello world",fp);
    if(retn==-1){
	perror("fputs");

    }
    printf("hahaha\n");
    
    fclose(fp);

}

fread

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
   FILE *fp;
   char *buff;   
   size_t ret;
   fp=fopen("1.txt","r");
   if(fp==NULL){
      perror("fopen");
      return 0;
   }
   
   buff=(char*)malloc(100);
   if(buff==NULL){
      return 0;

   }

   ret = fread(buff,10,1,fp);
   if(ret==-1){
       perror("fread");
       goto end;
   }
   
   printf("buf=%s\n",buff); 

end:
   free(buff);
   fclose(fp);

}

fwrite

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student{
   char name[16];
   int age;
   char sex[8];
};

int main(int argc,char *argv[]){
   FILE *fp;
   size_t ret;
   
   struct student stu;
   struct student stu2;

   fp=fopen("1.bin","w");
   if(fp==NULL){
      perror("fopen");
      return 0;
   }


   strcpy(stu.name,"zhangsan");
   stu.age = 49;
   strcpy(stu.sex,"male");
   
   ret = fwrite(&stu,sizeof(stu),1,fp);
   if(ret ==-1){
      perror("fwrite");
      goto end;

   }else{
     printf("write struct student success!\n");
   }
   fclose(fp);

   fp=fopen("1.bin","r");
   if(fp==NULL){
      perror("fopen");
      return 0;
   }
  

 
   ret = fread(&stu2,sizeof(stu),1,fp);
   if(ret ==-1){
      perror("fread");
      goto end;
   }
   
   printf("name=%s,age=%d,sex=%s\n",stu2.name,stu2.age,stu2.sex);


  

end:
   fclose(fp);


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会编程喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值