874复习第十一天-写入文件

32 篇文章 2 订阅

写入文件

#include<stdio.h>
/*
	1、按照字符进行文件的读写
		a.写文件
		 
*/ 
void write_file(){
	//1、打开文件
	FILE *fp;
	//w:表示写入,如果文件不存在,就创建 
	fp=fopen("S:\\study\\874\\Cprogram\\0xt\\Unit12\\3.txt","w");
	if(fp==NULL){
		perror("fopen");
		return;
	} 
	//2、写文件
	//putc是一次写入一个字符,写完一个字符,光标自动移向导该光标的末尾abcd
	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp); 
	//3、关闭文件 
	fclose(fp);
	fp=NULL;
}
int main(){
	//写入文件
	write_file(); 
	return 0;
} 

在这里插入图片描述

#include<stdio.h>
/*
	1、按照字符进行文件的读写
		a.写文件
		 
*/ 
void write_file(){
	//1、打开文件
	FILE *fp;
	//w:表示写入,如果文件不存在,就创建 
	fp=fopen("S:\\study\\874\\Cprogram\\0xt\\Unit12\\3.txt","w");
	if(fp==NULL){
		perror("fopen");
		return;
	} 
	//2、写文件
	//putc是一次写入一个字符,写完一个字符,光标自动移向导该光标的末尾abcd
	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp); 
	
	fputc('d',stdout);
	printf("\n");
	
	//3、关闭文件 
	fclose(fp);
	fp=NULL;
}
int main(){
	//写入文件
	write_file(); 
	return 0;
} 

在这里插入图片描述

#include<stdio.h>
/*
	1、按照字符进行文件的读写
		a.写文件
		 
*/ 
void write_file(){
	//1、打开文件
	FILE *fp;
	//w:表示写入,如果文件不存在,就创建 
	fp=fopen("S:\\study\\874\\Cprogram\\0xt\\Unit12\\3.txt","w");
	if(fp==NULL){
		perror("fopen");
		return;
	} 
	//2、写文件
	//putc是一次写入一个字符,写完一个字符,光标自动移向导该光标的末尾abcd
	/* 
	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp); 
	
	fputc('d',stdout);
	printf("\n");
	*/
	char ch='a';
	while(ch<='g'){
		fputc(ch,fp);
		ch++;
	}
	
	//3、关闭文件 
	fclose(fp);
	fp=NULL;
}
int main(){
	//写入文件
	write_file(); 
	return 0;
} 

在这里插入图片描述

#include<stdio.h>
#include<string.h>
/*
	1、按照字符进行文件的读写
		a.写文件
		 
*/ 
void write_file(){
	//1、打开文件
	FILE *fp;
	//w:表示写入,如果文件不存在,就创建 
	fp=fopen("S:\\study\\874\\Cprogram\\0xt\\Unit12\\3.txt","w");
	if(fp==NULL){
		perror("fopen");
		return;
	} 
	//2、写文件
	//putc是一次写入一个字符,写完一个字符,光标自动移向导该光标的末尾abcd
	/* 
	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp); 
	
	fputc('d',stdout);
	printf("\n");
	*/
	
	/*
	char ch='a';
	while(ch<='g'){
		fputc(ch,fp);
		ch++;
	}
	
	*/
	
	char ch='a';
	int i;
	char buf[]="abcdefgh";
	for(i=0;i<strlen(buf);i++){
		fputc(buf[i],fp);
	}
	
	//3、关闭文件 
	fclose(fp);
	fp=NULL;
}
int main(){
	//写入文件
	write_file(); 
	return 0;
} 

在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
#include<string.h>
/*
	1、按照字符进行文件的读写
		a.写文件
		 
*/ 
void write_file(){
	//1、打开文件
	FILE *fp;
	//w:表示写入,如果文件不存在,就创建 
	fp=fopen("S:\\study\\874\\Cprogram\\0xt\\Unit12\\3.txt","w");
	if(fp==NULL){
		perror("fopen");
		return;
	} 
	
	
	//2、写文件
	//putc是一次写入一个字符,写完一个字符,光标自动移向导该光标的末尾abcd
	/* 
	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp); 
	
	fputc('d',stdout);
	printf("\n");
	*/
	
	/*
	char ch='a';
	while(ch<='g'){
		fputc(ch,fp);
		ch++;
	}
	
	*/
	
	
	//用for循环 
	
	/* 
	char ch='a';
	int i;
	char buf[]="abcdefgh";
	for(i=0;i<strlen(buf);i++){
		fputc(buf[i],fp);
	}
	
	*/
	
	fputc(1,fp);
	fputc(2,fp);
	fputc(65,fp);
	fputc(97,fp);
	
	//3、关闭文件 
	fclose(fp);
	fp=NULL;
}
int main(){
	//写入文件
	write_file(); 
	return 0;
} 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向上Claire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值