c笔记——c中的与流相关的函数

本文介绍了C语言中处理文件及字符串的相关函数,包括freopen、fprintf、sprintf等的使用方法,并通过实例演示了如何利用这些函数进行文件重定向、格式化输出及字符串拼接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面会涉及到的函数

freopen()

printf —— fprintf—— sprintf

scanf—— fscanf——sscanf

原型:
int fprintf (FILE *stream, const char *template, ...)
int sprintf (char *s, const char *template, ...)
int fscanf (FILE *stream, const char *template, ...)
int sscanf (const char *s, const char *template, ...)
f表示和流相关,s表示和字符串相关;


#include<stdio.h>
#include<cmath>
#define I 20    //爱心的上半部分两个小半圆的半径
#define R 340   //爱心的下半部分圆弧曲线半径
int main(){
	//FILE *pf;
	//pf=stdin;//标准输入
	//pf=stderr;//标准错误
	//pf=stdout;//标准输出

	/**
	文件:文本文件 和 二进制文件 的区别
	1、文本文件每个字节代表字符
	2、二进制文件每个字节不一定代表字符
	无论文本文件还是二进制文件,一个文件就是一个字节的序列,只不过文本文件每个字节都有特定的含义,可能是字符
	,可能是换行符(DOS操作系统 用 换行符LF(10)+回车CR(13)表示行尾,此外UNIX操作系统不区分二进制和文本文件)。
	*/
	//下面是函数freopen的应用(在文件中写入爱心),freopen可以将标准输出重定向到指定文件,这样输出结果会保存到文件。
	printf("%c",'\3');
	freopen("c:\\test.dat","w",stdout);
    int i,j,e;
    int a;
	//爱心的上半部分 为两个实心半圆
    for(i=1,a=I;i<I/2;i++,a--){
        for(j=(int)(I-sqrt((double)(I*I-(a-i)*(a-i))));j>0;j--){
            printf(" "); 
		}
        for(e=1;e<=2*sqrt((double)(I*I-(a-i)*(a-i)));e++){
            printf("\3");
		}
        for(j=(int)(2*( I-sqrt((double)(I*I-(a-i)*(a-i)))));j>0;j--){
            printf(" ");
		}
        for(e=1;e<=2*sqrt( (double) (I*I-(a-i)*(a-i)) );e++){
            printf("\3");
		}
        printf("\n");
    }
//爱心的中间一行 可以打印一些标记
    for(i=1;i<80;i++){
        if(i==25){
            printf("    I LOVE YOU!O(∩_∩)O~    ");
            i+=30;
        }
        printf("\3");
    }
    printf("\n");  
//爱心的下半部分 以R为半径的左右两条圆弧曲线 R设置为比较大
    for(i=1;i<=R/2;i++){
        if(i%2||i%3)continue;//因为R很大 两条曲线应该删去中间的一些点 这样就会更像爱心
        for(j=(int) (R-sqrt((double) (R*R-i*i)));j>0;j--) {
            printf(" ");
		}
        for(e=1;e<=2*( sqrt( (double)(R*R-i*i) ) - (R-2*I) );e++){
            printf("\3");
		}
        printf("\n");
	}
	fclose(stdout);

}

#include<stdio.h>
#include<string.h>
#include<ctime>
int main(){
	FILE* pf=fopen("c:\\test.txt","w+");
	//fprintf,与printf对应,向文件中或者标准流 输出
	//fprintf(pf,"zhaodawei");//直接写入,就像printf("zhaodawei");
	fprintf(pf,"%d%c%s",1111,'z',"haodawei");//就像printf("%d%c%s",1111,'z',"haodawei");

	
	fseek(pf,0L,SEEK_SET);
	int i;
	char ch;
	char buf[256];
	fscanf(pf,"%d%c%s",&i,&ch,buf);
	printf("%d\n%c\n%s\n",i,ch,buf);



	//与fprintf和fscanf对应的是 sprintf和sscanf
	char temp[256]="zhaodawe	1990	11.21";
	char name[10];
	int year;
	double date=0.0;
	sscanf(temp,"%s\t%d\t%lf",name,&year,&date);//从字符串中输入
	

	/**
	fsprintf的作用:
	1、向字符数组输出(可以加一下格式控制,和printf一样,只不过输出目标由stdout变成字符数组),例子在后面单独给出
	2、可以拼接字符串(同样可以加一些格式控制),例如:
	*/
	char str1[]="dave";
	char str2[]="a boy!";
	char des[256];
	sprintf(des,"%s must be %s",str1,str2);
	fprintf(stdout,"%s\n",des);
	//输出dave must be a boy!

	//问题1,如果字符数组没有带'\0'结束(一般不会,但也是有可能出现)
	//还是用上面的例子
	char s1[]={'d','a','v','e'};
	char s2[]={'a',' ','b','o','y','!'};
	char de[256];
	sprintf(de,"%.4s must be %.6s",s1,s2);//和printf一样,可以格式控制
	sprintf(de,"%.*s must be %.*s",sizeof(s1),s1,sizeof(s2),s2);
	fprintf(stdout,"%s\n",de);

	//问题2,sprintf没有检测des会不会产生越界,纯在安全隐患,安全版本的函数是sprintf_s(),snprintf()
	
	//格式化时间输出strftime
	time_t t = time(0);
	char s[32];
	strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t));
	printf("%s\n%d\n",s,t);

}

#include<stdio.h>
int main(void)
{
char buffer[200],s[]="computer",c='l';
int i=35,j;
float fp=1.7320534f;
j=sprintf(buffer,"String:%s\n",s);
j+=sprintf(buffer+j,"Character:%c\n",c);
j+=sprintf(buffer+j,"Integer:%d\n",i);
j+=sprintf(buffer+j,"Real:%f\n",fp);

printf("Output:\n%s\ncharactercount=%d\n",buffer,j);
//结果为53,sprintf返回值为写入字符数组的字符数,整形,浮点型每一位都是一个字符。
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值