首页 > 编程语言 >【c&c++】C语言:fgets和fgetc函数读取文件

【c&c++】C语言:fgets和fgetc函数读取文件

时间:2023-02-10 15:34:35浏览次数:38  
标签:fp return fgetc c++ C语言 file fgets include

C语言:fgets和fgetc函数读取文件

1、fgetc 是 file get char 的缩写,意思是从指定的文件中读取一个字符。
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

2、fgets函数 char *fgets(char *str, int n, FILE *stream) 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

 

3、代码测试

3.1 c文件fget_demo.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
 
 
int fgets_function_demo(const *file_name)
{
    
    char line[1024], *ptr = NULL;
    FILE *fp = NULL;
    int n=0;
    printf("\n%s\n", __FUNCTION__);
    fp = fopen(file_name, "r");
    if (fp == NULL) {
        printf("Couldn't open %s\n",file_name);
        return 0;
    }
 
    while(fgets(line, 1024, fp)) {
        n++;
        printf("line %3d:%s\n",n,line);
    }
 
    fclose(fp);
    return 0;
}
 
 
int fgetc_function_demo(const *file_name)
{
    FILE *fp;
    char ch;
    printf("\n%s\n", __FUNCTION__);
    //如果文件不存在,给出提示并退出
   if( (fp=fopen(file_name,"rt")) == NULL ){
        printf("Fail to open file!");
        fclose(fp);
        return -1;
    }
    //每次读取一个字节,直到读取完毕
    while( (ch=fgetc(fp)) != EOF ){
        printf("%c ",ch);
    }
    printf("\n");  //输出换行符
    fclose(fp);
    return 0;
 
}
 
 
int main()
{
    fgets_function_demo("net_dev";
    fgetc_function_demo("net_dev");
    return 1;
}

    3、读取的net_dev文件内容。

static int create_bt_test_file_for_brcm(void)
{
    FILE* fp;
    if (fp != 0) {
        system("chmod 777 /userdata/bt_pcba_test");
        system("mount --bind /userdata/bt_pcba_test /usr/bin/bt_pcba_test");
        return 0;
    }
    return -1;
}

   4、编译执行结果

 

 

 



标签:fp,return,fgetc,c++,C语言,file,fgets,include
From: https://www.cnblogs.com/opensmarty/p/17109109.html

相关文章

  • c语言填空:用函数判断是否为质数
    #include<stdio.h>//键盘输入任意整数,判断其是否为质数intpdzs(intn){inta;for(a=2;a<n;a++)if(n%2==0)【1】;【2】;}main(){int......
  • 【c&c++】 C语言:access函数的使用
    一、access()函数用来判断用户是否具有访问某个文件的权限(或判断某个文件是否存在).二、需要包含#include<unistd.h>三、参数和返回值intaccess(constchar*path......
  • 【c&c++】C语言实现判断大端小端存储方式的代码
    一、大小端简介大小端是计算机存储的两种方式。小端表示法(Little-endian):所谓的小端模式,是指数据的高位保存在内存的高地址中,而数据的低位保存在内存的低地址中,这种存储......
  • Modern C++ ——constexpr的各种用法
    ModernC++——constexpr的用法Reference《现代C++语言核心特性解析》为什么引入constexprconst可以定义常量,但也可以用来定义只读变量。const变量的值不一定是在......
  • c++指针基础
    指针是一个变量,其存储的是值的地址,而不是值本身。如何找到常规变量的地址?只需对变量应用地址运算符(&),就可以获得它的位置;intdonuts=3;&donuts就是取donuts变量的地址。......
  • 【快速学】指针是什么?指针常量、常量指针是什么?(C++)
    0、先上总结指针是什么?指针是个数据类型,它里面存储的是个地址如inta=3;,定义了一个int类型的变量a,值为3,它在内存中的地址为&a同理,int*b=3;,定义了一个int*类型的变量b......
  • c++引用lib
    如果需要引用的是第三方的lib,没有把源码引入到工程中,则需要通过传统方式引用。1.在需要引用的项目上右键属性,在vc++目录中引用目录和引用库目录。引用目录是lib头文件的......
  • C语言-基础知识
     1:*C语言是一种通用的、面向过程式的计算机程序设计语言。    C语言是一种通用的高级语言。    最新的C语言标准是C18.  2:**预处理器指令 ......
  • 【c&c++】结构 | 结构与函数 | C语言
                 ......
  • 【c&c++】C语言snprintf()函数用法
    一、函数原型snprintf(),为函数原型intsnprintf(char*str,size_tsize,constchar*format,...)。二、函数介绍将可变个参数(...)按照format格式化成字符串,然后将其......