C 语言实例 – 从文件中读取一行

C语言教程评论

C 语言实例 - 从文件中读取一行

从文件中读取一行。

文件 nowcoder.txt 内容:文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-congwenjianzhongduquyixing.html

1
2
3
$ cat nowcoder.txt
gongshiku.com
google.com

实例

#include <stdio.h>
#include <stdlib.h> //  exit() 函数
int main()
{
    char c[1000];
    FILE *fptr;

    if ((fptr = fopen("nowcoder.txt", "r")) == NULL)
    {
        printf("Error! opening file");
        // 文件指针返回 NULL 则退出
        exit(1);         
    }

    // 读取文本,直到碰到新的一行开始
    fscanf(fptr,"%[^\n]", c);

    printf("读取内容:\n%s", c);
    fclose(fptr);

    return 0;
}

输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-congwenjianzhongduquyixing.html

读取内容:
gongshiku.com
文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-congwenjianzhongduquyixing.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-congwenjianzhongduquyixing.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 
公式库网
  • 本文由 公式库网 发表于 2021年12月7日20:59:00
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-yuyanshili-congwenjianzhongduquyixing.html

发表评论