c语言的fprintf函数的用法,fprintf 和fscanf 函数

c语言的fprintf函数的用法,fprintf 和fscanf 函数C语言的fprintf和fscanf函数fprintf()函数用于将一组字符写入文件。它将格式化的输出发送到流。fprintf()函数的语法如下:intfprintf(FILE*stream,constchar*format[,argument,…])示例:创建一个源文件:fprintf-write-file.c,其代码如下-#includemain(){FILE…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

C语言 的 fprintf 和fscanf 函数

fprintf()函数用于将一组字符写入文件。它将格式化的输出发送到流。

fprintf()函数的语法如下:

int fprintf(FILE *stream, const char *format [, argument, …])

示例:

创建一个源文件:fprintf-write-file.c,其代码如下 –

#include

main() {

FILE *fp;

fp = fopen(“file.txt”, “w”);//opening file

fprintf(fp, “Hello file by fprintf…\n”);//writing data into file

fclose(fp);//closing file

printf(“Write to file : file.txt finished.”);

}

执行上面示例代码,得到以下结果 –

Write to file : file.txt finished.

打开filehadling 目录下,应该会看到一个文件:file.txt 。

读取文件:fscanf()函数

fscanf()函数用于从文件中读取一组字符。它从文件读取一个单词,并在文件结尾返回EOF。

fscanf()函数的语法如下:

int fscanf(FILE *stream, const char *format [, argument, …])

示例:

创建一个源文件:fscanf-read-file.c,其代码如下 –

#include

main(){

FILE *fp;

char buff[255];//creating char array to store data of file

fp = fopen(“file.txt”, “r”);

while(fscanf(fp, “%s”, buff)!=EOF){

printf(“%s “, buff );

}

fclose(fp);

}

执行上面示例代码,得到以下结果 –

Hello file by fprintf…

文件存取示例:存储员工信息

下面来看看一个文件处理示例来存储从控制台输入的员工信息。要存储雇员的信息有:身份ID,姓名和工资。

示例:

创建一个源文件:storing-employee.c,其代码如下 –

#include

void main()

{

FILE *fptr;

int id;

char name[30];

float salary;

fptr = fopen(“emp.txt”, “w+”);/* open for writing */

if (fptr == NULL)

{

printf(“File does not exists \n”);

return;

}

printf(“Enter the Emp ID:”);

scanf(“%d”, &id);

fprintf(fptr, “Id= %d\n”, id);

printf(“Enter the name: “);

scanf(“%s”, name);

fprintf(fptr, “Name= %s\n”, name);

printf(“Enter the salary: “);

scanf(“%f”, &salary);

fprintf(fptr, “Salary= %.2f\n”, salary);

fclose(fptr);

}

执行上面示例代码,得到以下结果 –

Enter the Emp ID:10010

Enter the name: Maxsu

Enter the salary: 15000

现在从当前目录打开文件。将看到有一个emp.txt文件,其内容如下 –

emp.txt

Id= 10010

Name= Maxsu

Salary= 15000.00

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/180454.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • termux更改镜像源_pycharm自带python

    termux更改镜像源_pycharm自带python方法1在使用pip命令进行安装的时候,传入镜像地址如下#pipinstallpandas-ihttps://mirrors.aliyun.com/pypi/simple#pipinstalldjango==1.11.*-ihttps://pypi.tuna.tsinghua.edu.cn/simple可选的镜像地址有豆瓣https://pypi.douban.com/simple阿里云https://mirror…

    2022年8月29日
    0
  • Docker安装Nexus3私服以及使用

    Docker安装Nexus3私服以及使用一、Nexus简介二、安装步骤(使用docker)1、搜索Nexus3镜像dockersearchnexus​2、拉取Nexus3镜像dockerpullsonatype/nexus3​3、创建Nexus3容器dockerrun-itd-p8081:8081–privileged=true–namenexus3\-v/data/nexus-data:/var/nexus-data–restar…

    2022年7月13日
    10
  • form 为什么上传文件enctype现场

    form 为什么上传文件enctype现场

    2022年1月11日
    35
  • Sub-process /usr/bin/dpkg returned an error code_sub-process /usr/bin/dpkg

    Sub-process /usr/bin/dpkg returned an error code_sub-process /usr/bin/dpkgE:Sub-process/usr/bin/dpkgreturnedanerrorcode(1)解决办法安装libapache2-svn出现了这个错误,是由于apt-get安装软件时出现了类似于:dpkg:errorprocessingpackagelibapache2-mod-svn(–configure):subprocessinstalledpost-i…

    2022年10月7日
    0
  • 最全中文停用词表

    最全中文停用词表本文将github上包括哈工大停用词、四川大学机器智能实验室停用词、百度停用词、中文停用https://github.com/goto456/stopwords以及最全中文停用词表(1893)https://blog.csdn.net/shijiebei2009/article/details/39696571进行整合得到新的中文停用词表,总共2462个为止纵然all例如[④e…

    2022年6月15日
    30
  • 微信开发-40029 invalid code「建议收藏」

    微信开发-40029 invalid code「建议收藏」微信开发过程中,根据回调code获取snsapi_userinfo的时候,遇到了错误提示{“errcode”:40029,”errmsg”:”invalidcode”},后面发现是因为自己内部服务调试过程中,将该code使用了两次,导致提示该错误。所以,如果有谁遇到了该提示,请务必检查是否code被使用了两次。

    2022年4月30日
    92

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号