fgetc/fputc 和 fgets/fputs 的详细用法

一.fgetc/fputc函数使用

int fgetc(FILE *stream);
功能:从文件中读取一个字符
参数:
    @stream:文件指针
返回值:成功返回字符的ascii,失败返回-1(EOF)

 int fputc(int c, FILE *stream);
功能:向文件中写入一个字符
参数:
 @c:被操作的字符
 @stream:文件指针
返回值:成功返回被写入字符的ascii,失败返回EOF(-1)

1.1fgetc函数使用实例

head.h ===>将head.h放入到/usr/include目录下

#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>

#define PRINT_ERR(msg) \
    do {               \
        perror(msg);   \
        return -1;     \
    } while (0)

#endif
#include <head.h>  

int main(int argc, const char* argv[])
{
    FILE* fp;
    int c;
    if ((fp = fopen("./hello.txt", "r")) == NULL)
        PRINT_ERR("fopen error");

    c = fgetc(fp);  //读取到hello.txt第一个字符,光标会向后移动一个字符的位置
    printf("c = %c\n",c);//将读取到的字符打印到终端上

    c = fgetc(fp); //读取hello.txt的第二个字符,光标会向后移动一个字符的位置
    printf("c = %c\n",c);//将读取到的字符打印到终端上

    fclose(fp);
    return 0;
}

1.2使用fgetc函数统计一个文件有多少行?

#include <head.h>

int main(int argc,const char * argv[])
{
    FILE *fp;
    int ch,count=0;
    //1.判断用户传参是否正确
    if(argc != 2){
        printf("input error,try again\n");
        printf("usage: ./a.out filename\n");
        return -1;
    }

    //2.打开文件
    if((fp = fopen(argv[1],"r"))==NULL)
        PRINT_ERR("fopen error");

    //3.从文件中读取换行符,让计数变量++
    while((ch=fgetc(fp)) != EOF){
        if(ch == '\n')count++;
    }
    printf("line = %d\n",count);

    //4.关闭文件
    fclose(fp);

    return 0;
}

1.3fputc函数使用实例

#include <head.h>

int main(int argc,const char * argv[])
{
    FILE *fp;

    if((fp = fopen("./hello.txt","w"))==NULL)
        PRINT_ERR("fopen error");
    
    fputc('h',fp);
    fputc('e',fp);
    fputc('l',fp);
    fputc('l',fp);
    fputc('o',fp); //会将上述的字符写入到hello.txt中

    fputc('w',stdout); //写到终端上
    fputc('e',stderr); //写到终端上

    fclose(fp);

    return 0;
}

1.4使用fgetc和fputc完成文件的拷贝过程

./a.out srcfile destfile

#include <head.h>

int main(int argc, const char* argv[])
{
    FILE *sfp, *dfp;
    int ch;
    // 1.判断用户传递的参数是否正确
    if (argc != 3) {
        printf("input error,try again\n");
        printf("usage:./a.out srcfile destfile\n");
        return -1;
    }
    // 2.打开源文件和目标文件
    if ((sfp = fopen(argv[1], "r")) == NULL)
        PRINT_ERR("open srcfile error");

    if ((dfp = fopen(argv[2], "w")) == NULL)
        PRINT_ERR("open destfile error");

    //3.循环读写字符
    while((ch = fgetc(sfp)) != EOF){
        fputc(ch,dfp);
    }

    //4.关闭文件
    fclose(sfp);
    fclose(dfp);
    
    return 0;
}

二.fgets/fputs函数使用

char *fgets(char *s, int size, FILE *stream);
功能:从文件读读取字符串到s中,遇到EOF或者'\n'的时候停止读
    并且会将'\n'也一并读取到存储在s对应的缓冲区中,在最后的位置
    补充'\0'
参数:
    @s:保存字符串的地址
 @size:向读取的大小
 @stream:文件指针
返回值:成功返回s地址,失败返回NULL

int fputs(const char *s, FILE *stream);
功能:将s中的字符串写入到文件中
参数:
    @s:字符串的首地址
 @stream:文件指针
返回值:成功返回大于0的数,失败返回EOF(-1)

2.1fgets函数的使用实例(通过终端向程序输入字符串)

#include <head.h>

int main(int argc,const char * argv[])
{
    char buf[128] = {0};

    //hello\n\0
    //从标准输入中读取字符串到buf中
    fgets(buf,sizeof(buf),stdin);
    //将缓冲区中的'\n换成'\0'
    buf[strlen(buf)-1] = '\0';

    printf("result = %s\n",buf);
    
    return 0;
}

2.2fgets函数的使用实例(从文件中读取字符串)

#include <head.h>

int main(int argc,const char * argv[])
{
    FILE *fp;
    char buf[128] = {0};

    if((fp = fopen("./hello.txt","r"))==NULL)
        PRINT_ERR("fopen error");

    fgets(buf,6,fp);

    printf("buf = %s\n",buf);

    fclose(fp);

    return 0;
}

2.3练习:使用fgets统计一个文件有多少行?

#include <head.h>

int main(int argc,const char * argv[])
{
    FILE *fp;
    int line=0;
    char buf[10] = {0};
    //1.判断用户传参是否正确
    if(argc != 2){
        printf("input error,try again\n");
        printf("usage: ./a.out filename\n");
        return -1;
    }

    //2.打开文件
    if((fp = fopen(argv[1],"r"))==NULL)
        PRINT_ERR("fopen error");

    //3.从文件中读取换行符,让计数变量++
    while(fgets(buf,sizeof(buf),fp) != NULL){
        if(strlen(buf)==(sizeof(buf)-1)){
            if(buf[sizeof(buf)-2] != '\n'){
                continue;
            }
        }
        line++;
    }

    printf("line = %d\n",line);

    //4.关闭文件
    fclose(fp);

    return 0;
}

2.4fputs函数的使用实例

#include <head.h>

int main(int argc, const char* argv[])
{
    FILE* fp;
    //使用fputs向标准输出和标准出错中显示内容
    fputs("hello world\n", stderr);
    fputs("123123123\n", stdout);

    if ((fp = fopen("./hello.txt", "a")) == NULL)
        PRINT_ERR("fopen error");

    fputs("this is IO process .....\n",fp);

    fclose(fp);
    return 0;
}

2.5使用fputs和fgets拷贝文件

#include <head.h>

int main(int argc, const char* argv[])
{
    FILE *sfp, *dfp;
    char buf[10] = {0};
    // 1.判断用户传递的参数是否正确
    if (argc != 3) {
        printf("input error,try again\n");
        printf("usage:./a.out srcfile destfile\n");
        return -1;
    }
    // 2.打开源文件和目标文件
    if ((sfp = fopen(argv[1], "r")) == NULL)
        PRINT_ERR("open srcfile error");

    if ((dfp = fopen(argv[2], "w")) == NULL)
        PRINT_ERR("open destfile error");

    //3.循环读写字符
    while(fgets(buf,sizeof(buf),sfp)!=NULL){
        fputs(buf,dfp);
    }

    //4.关闭文件
    fclose(sfp);
    fclose(dfp);
    
    return 0;
}
  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值