fscanf和fprintf

fscanf的字符串是在键盘的缓冲区,fprintf是在显示器的缓冲区。

1.函数原型:

  • int fprintf(FILE *fp, const char *format[,argument, ...])
  • int fscantf(FILE *fp, const char *format[,address, ...])

2.功能:按格式对问件进行I/O操作

3.返回值:

  • 成功,返回I/O的个数,出错或文件尾,返回EOF。
  • fprintf()返回值就是写入成功的字符的个数。
  • fscanf()返回值是扫描到几个数据,这个字符串不管有多长,要扫描的每一种类型算一个数据,%s算一个,%d算一个。详见eg3.

4.例子:

fprintf(fp, "%d, %6.2f, i,t");  //将i和t按%d,%6.2f格式输出到fp文件

fscanf(fp, "%d,%f",  &i,&t)/  //若文件中有3,4.5,则将3送入i,4.5送入t。

eg1:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h> void main1()
{
//printf("hello");
//fprintf(stdout, "hello");//printf只是fprintf特例
char str[] = { };
//scanf("%s", str);
fscanf(stdin, "a=%s", str);//从字符串中取出,键盘缓冲区,输入时,要输入“a=abc”,下面才会打印。
fprintf(stdout, "%s", str);//int string映射到一个字符串 显示器缓冲区 getchar();
getchar(); }
//1 mndadmin@yahoo.com.cn xijaxi16ytja6t7ibcrj
void main2()
{ char str[] = { };
{
int i = ;
char email[] = "mndadmin@yahoo.com.cn";
char password[] = "xijaxi16ytja6t7ibcrj"; sprintf(str, "%d %s %s", i, email, password);
fprintf(stdout, "\n%s", str);
}
{
int j;
char emailx[] = { };
char passmd5[] = { };
sscanf(str, "%d%s%s", &j, emailx, passmd5);
fprintf(stdout, "\nj=%d eamilx=%s passmd5=%s ", j, emailx, passmd5); } system("pause");
}

eg2:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> typedef struct info7k7k
{
char user[];
char password[]; }INOF,* PINOF; void main2x()
{
FILE *pfr = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOK.txt", "r");
FILE *pfw = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOKwithid.txt", "w");
int i = ;
while (!feof(pfr))
{
i++;
INOF info1;
fscanf(pfr, "%s%s", info1.user, info1.password);//提取
fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);//组合
}
fclose(pfr);
fclose(pfw);
system("pause");
} void main3x()
{
//int num = fprintf(stdout, "helloword%s","1234");
//printf("\n%d", num);//fprintf返回值就是写入成功字符的个数
FILE *pf = fopen("C:\\x.txt", "r");
int num = fprintf(pf, "helloword%s", "");//写入失败返回-1
printf("\n%d", num);
system("pause");
}
void main()
{
//char str[128] = { 0 };
//int numa;
//int numb;
//int num = fscanf(stdin, "%s%d%d",str,&numa,&numb);
////返回值是扫描到几个数据,失败返回-1
//printf("\n%d", num);
FILE *pf = fopen("C:\\x.txt", "w");
char str[] = { };
int numa;
int numb;
int num = fscanf(pf, "%s%d%d",str,&numa,&numb);
printf("\n%d", num); system("pause"); }

eg3:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> typedef struct info
{
char user[];
char password[]; }INOF,* PINOF; void main2x()
{
FILE *pfr = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOK.txt", "r");
FILE *pfw = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOKwithid.txt", "w");
int i = ;
while (!feof(pfr))
{
i++;
INOF info1;
fscanf(pfr, "%s%s", info1.user, info1.password);//提取
fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);//组合
}
fclose(pfr);
fclose(pfw);
system("pause");
} void main3x()
{
//int num = fprintf(stdout, "helloword%s","1234");
//printf("\n%d", num);//fprintf返回值就是写入成功字符的个数
FILE *pf = fopen("C:\\x.txt", "r");
int num = fprintf(pf, "helloword%s", "");//写入失败返回-1
printf("\n%d", num);
system("pause");
}
void main()
{
//char str[128] = { 0 };
//int numa;
//int numb;
//int num = fscanf(stdin, "%s%d%d",str,&numa,&numb);
////返回值是扫描到几个数据,失败返回-1
//printf("\n%d", num);
FILE *pf = fopen("C:\\x.txt", "w");
char str[] = { };
int numa;
int numb;
int num = fscanf(pf, "%s%d%d",str,&numa,&numb);
printf("\n%d", num); system("pause"); }

fscanf和fprintf的更多相关文章

  1. 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)

    函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...

  2. C++之函数fgetc和fputc、fgets和fputs、fread和fwrite、fscanf和fprintf用法小结

    #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int ...

  3. c语言中的文件格式化读写函数fscanf和fprintf函数

    很多时候我们需要写入数据到文件中时都觉得很困扰,因为格式乱七八槽的,可读性太差了,于是我们就想有没有什么函数可以格式化的从文件中输入和输出呢,还真有.下面我将讲解一下fscanf和fprintf的强大 ...

  4. vs使用fscanf和fprintf错误警告处理

    严重性代码说明项目文件行 禁止显示状态错误 C4996 fopen('fscanf'.strcmp):This function or variable may be unsafe. 最全解决办法(转 ...

  5. fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结

    转载自:http://blog.csdn.net/xidianzhimeng/article/details/23541289 1 fopen 函数原型:FILE * fopen(const char ...

  6. 进程操作篇atexit execl exit fprintf fscanf getpid nice get priority printf setpid system vfork wait waitpid

    atexit(设置程序正常结束前调用的函数) 相关函数 _exit,exit,on_exit 表头文件 #include<stdlib.h> 定义函数 int atexit (void ( ...

  7. printf与fprintf函数的区别

    printf是标准输出流的输出函数,用来向屏幕这样的标准输出设备输出,而fprintf则是向文件输出,将输出的内容输出到硬盘上的文件或是相当于文件的设备上 printf是有缓冲的输出,fprintf没 ...

  8. c语言之I/O函数

    c语言中常用的I/O函数 最常用的字符串的标准I/O函数有getchar().putchar().gets().puts().scanf().printf().fputs().fgets().getc ...

  9. C语言的标准输入输出

    1. 标准输入输出 标准输入.输出主要由缓冲区和操作方法两部分组.缓冲区实际上可以看做内存中的字符串数组,而操作方法主要是指printf.scanf.puts.gets,getcha.putcahr等 ...

随机推荐

  1. UVA11636-Hello World!-水题

    Hello World! Time limit: 1.000 seconds When you first made the computer to print the sentence "H ...

  2. RMQ算法

    1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...

  3. os系统

    任务延时函数OSTimeDly 功能:调用该函数的任务将自己延时一段时间并执行一次任务调度,一旦规定的延时时间完成或有其它的任务通过调用OSTimeDlyResume()取消了延时,调用OSTimeD ...

  4. Spring框架学习笔记(10)——Spring中的事务管理

    什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...

  5. Spring框架学习笔记(1)——HelloWorld

    1.创建一个新的项目,并添加Spring框架 2.创建HelloWorld.java package com.broadtext.beans.helloworld; public class Hell ...

  6. 遍历数组中的元素(含es6方法)

    假如有这样一个数组.arr = [12,34,45,46,36,58,36,59],现在要遍历该数组. 方法1:以前我们可能会这样做: for(var i=0;i<arr.length;i++) ...

  7. 利用JAVA API远程进行HDFS的相关操作

    学习HDFS有一段时间了,现在把自己总结的HDFS的相关操作代码展示给大家. 主要有HDFS的增删改查,文件的追加,windows本地文件的上传,hdfs文件的下载,文件重命名,创建目录,文件是否存在 ...

  8. Git学习(1)-本地版本库的创建

    我用的是Git-2.14.3-64-bit版本,在windows64位上运行的,把软件分享下链接:http://pan.baidu.com/s/1jIoZ7Xc 密码:13q2. 安装及配置自行百度, ...

  9. 基于 HTML5 WebGL 的 3D 仪表数据监控

    工控仪表重点发展基于现场总线技术的主控系统装置及智能化仪表.特种和专用自动化仪表:全面扩大服务领域,推进仪器仪表系统的数字化.智能化.网络化,完成 自动化仪表从模拟技术向数字技术的转变:推进具有自主版 ...

  10. 源码讲解 node+mongodb 建站攻略(一期)第二节

    源码讲解 node+mongodb 建站攻略(一期)第二节 上一节,我们完成了模拟数据,这次我们来玩儿真正的数据库,mongodb. 代码http://www.imlwj.com/download/n ...