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. Java中的集合框架(中)

    Map和HashMap Map接口 1.Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value 2.Map中的键值对以Entry类型的对 ...

  2. 2329: [HNOI2011]括号修复

    传送魔法 一开始以为可以直接线段树的,好像还是不行……还是得用Spaly,然后就没啥了. #include<cstdio> #include<algorithm> #defin ...

  3. HDU 1000 A + B Problem(指针版)

    A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. Codeforces Round #426 (Div. 2)【A.枚举,B.思维,C,二分+数学】

    A. The Useless Toy time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  5. BZOJ 1968: [Ahoi2005]COMMON 约数研究(新生必做的水题)

    1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 2351  Solved: 1797 [Submi ...

  6. BZOJ 3195: [Jxoi2012]奇怪的道路(状压dp)

    f[i][j][s]表示当前处理第i个点,前i-1个点已连j条边,第i个点开始k个点的奇偶性状态. #include<cstring>#include<algorithm>#i ...

  7. codeforces 746C 模拟

    C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  8. 审计日志中的AOP

    审计跟踪(也称为审核日志)是一个安全相关的时间顺序记录,记录这些记录的目的是为已经影响在任何时候的详细操作,提供程序运行的证明文件记录.源或事件 MVC 自定义一个过滤器 public class A ...

  9. Unity 小笔记

    1,Time.deltatime放在Update和fixedupdate中得到的值是不一样的.还以为是通过两个值来获取. 2,VR中绘制射线可以使用LineRender. 3,Unity中判断一个东西 ...

  10. Oracle_insert_delete_update

    Oracle_insert_delete_update --复制表格的结构 create table temp as (select * from emp where 1=2); select * f ...