C fputs() and fgets() example函数详

首页 / C语言入门教程 / C fputs() and fgets() example函数详

C编程中的fputs()和fgets()用于从流中写入和读取字符串。让我们看看使用fgets()和fgets()函数编写和读取文件的示例。

fputs()函数

fputs()函数将一行字符写入文件。它将字符串输出到流。

语法:

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

示例:

#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
clrscr();

fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);

fclose(fp);
getch();
}

myfile2.txt

hello c programming

fgets()函数

fgets()函数从文件读取一行字符。它从流中获取字符串。

语法:

char* fgets(char *s, int n, FILE *stream)

示例:

#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
clrscr();

fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));

fclose(fp);
getch();
}

输出:

hello c programming

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

持续交付36讲 -〔王潇俊〕

从0开始学微服务 -〔胡忠想〕

黄勇的OKR实战笔记 -〔黄勇〕

消息队列高手课 -〔李玥〕

苏杰的产品创新课 -〔苏杰〕

大厂广告产品心法 -〔郭谊〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

AI大模型之美 -〔徐文浩〕

好记忆不如烂笔头。留下您的足迹吧 :)