C语言教程:fprintf()和fscanf()函数

2018-10-0906:44:33编程语言入门到精通Comments4,526 views字数 1453阅读模式

fprintf()函数用于将一组字符写入文件。它将格式化的输出发送到流。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

fprintf()函数的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

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

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

创建一个源文件:fprintf-write-file.c,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

#include <stdio.h>  
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.");
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

Write to file : file.txt finished.
C

打开filehadling 目录下,应该会看到一个文件:file.txt文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

读取文件:fscanf()函数

fscanf()函数用于从文件中读取一组字符。它从文件读取一个单词,并在文件结尾返回EOF文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

fscanf()函数的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

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

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

创建一个源文件:fscanf-read-file.c,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

#include <stdio.h>  
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);  
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

Hello file by fprintf...
C

文件存取示例:存储员工信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

下面来看看一个文件处理示例来存储从控制台输入的员工信息。要存储雇员的信息有:身份ID,姓名和工资。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

创建一个源文件:storing-employee.c,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

#include <stdio.h>  
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);
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

Enter the Emp ID:10010
Enter the name: Maxsu
Enter the salary: 15000
C

现在从当前目录打开文件。将看到有一个emp.txt文件,其内容如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

emp.txt文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html

Id= 10010
Name= Maxsu
Salary= 15000.00
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6466.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6466.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定