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. VFS四大对象之三 struct dentry

    继上一篇文章介绍了inode结构体:继续介绍目录项dentry: http://www.cnblogs.com/linhaostudy/p/7427794.html 三.dentry结构体 目录项:目 ...

  2. 关于Gson定制的分析

    首先,为什么需要定制呢?很多同学可能觉得默认的不也挺好的嘛?最开始,我也是觉得的,而且我们一开始也是用默认的解析方式的,因为我们与外部约定的数据格式一直都比较稳定.但当外部数据不稳定,那么Gson默认 ...

  3. 声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码

    上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com ...

  4. 洛谷 P3370 【模板】字符串哈希

    洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...

  5. Codefoces 723A The New Year: Meeting Friends

    A. The New Year: Meeting Friends time limit per test:1 second memory limit per test:256 megabytes in ...

  6. Codeforces 777A Shell Game

    A. Shell Game time limit per test:0.5 seconds memory limit per test:256 megabytes input:standard inp ...

  7. 51Nod 1632 B君的连通(递归,快速幂)

    1632 B君的连通 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 B国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条交通线将其连接.A国是B ...

  8. c#中winform窗口的隐藏与显示

    最近在做一个C# 的winform客户端程序,要实现在打开新的窗口时将原来打开的窗口关闭,但是想在关闭新打开的窗口是将原来的那个窗口再次打开,在网上查找各种资料,找了很多代码,都是通过窗口.Hide( ...

  9. volatile 与 synchronized 区别

    在Java中,为了保证多线程读写数据时保证数据的一致性,可以采用两种方式: 同步 如用synchronized关键字,或者使用锁对象. volatile 使用volatile关键字用一句话概括vola ...

  10. 在tomcat中布置项目的介绍(一)

    一:为什么要在tomcat中单独布置项目 因为上线到服务器上需要项目的功能之间彼此独立,这个以后我会细说. 二:简单的步骤一个都不能少 conf文件里的配置文件需要配置好:logback.xml文件会 ...