IO-day2

1.向文件中输出时间,并且是每秒更新一次,按ctrl+c停止输出后,下次再运行./a.out会继续向文件输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h> 		//引入时间库

int get_rows(FILE *fp);//计算行数
int input(FILE *fp,int rows,const char *str);//写入


int main(int argc, const char *argv[])
{
	//以追加形式打开文件
	FILE *fp=fopen("./systime.txt","a+");
	//判断是否打开成功
	if(NULL==fp){
		perror("open fail");
		return -1;
	}
	int rows=get_rows(fp);
	time_t tloc1;
	time_t tloc2=0;
	char time_str[30];
	struct tm *t;
	do{
		time(&tloc1);
		if(tloc1!=tloc2){//判断时间是否增加
			printf("tloc1=%ld\n",tloc1);
			t=localtime(&tloc1);
			sprintf(time_str,"%4d-%02d-%02d %02d:%02d:%02d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
			printf("%s",time_str);
			input(fp,rows++,time_str);
		}
		tloc2=tloc1;
	}while(1);
	
	//关闭文件
	fclose(fp);
	return 0;
}

/*
 * function:    计算行数
 * @param [ in] 文件指针
 * @param [out] 
 * @return      成功返回层数,失败返回-1
 */
int get_rows(FILE *fp){
	if(NULL==fp){
		printf("file error");
		return -1;
	}
	int count=0;
	char str[128];
	while(fgets(str,sizeof(str),fp)!=NULL){
		if(str[strlen(str)-1]=='\n')
			count++;
	}
	return count;
}

/*
 * function:    向文件写入系统时间
 * @param [ in] 文件地址 行数 要输入的字符串
 * @param [out] 
 * @return      成功返回0,失败返回-1
 */
int input(FILE *fp,int rows,const char *str){
	if(NULL==fp){
		perror("file error");
		return -1;
	}
	char buf[128];
	snprintf(buf,sizeof(buf),"%d: %s",rows,str);
	fputs(buf,fp);
	fflush(fp);//刷新缓存区
	return 0;
}

2.使用fread、fwrite完成两个文件的拷贝

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	FILE *fp1,*fp2;//fp1为源文件,fp2为目标文件
	if(argc<3){
		printf("%d\n",argc);
		printf("fail!\nusage: ./a.out srcfile dstfile\n");
		return -1;
	}
	fp1=fopen(argv[1],"r");
	fp2=fopen(argv[2],"w");
	if(NULL==fp1){
		perror("open fail");
		return -1;
	}
	if(NULL==fp2){
		perror("open fail");
		return -1;
	}
	char str[128];
	int ret=0;
	while((ret=fread(str,1,sizeof(str),fp1))!=0){
		fwrite(str,1,ret,fp2);
	}

	fclose(fp1);
	fclose(fp2);
	return 0;
}

3. 重新敲一遍代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	//追加打开文件
	FILE *fp=fopen("./file.txt","a+");
	char userName[100];
	char pwd[20];
	if(NULL==fp){
		perror("open fail");
		return -1;
	}
	//编写账号密码
	printf("创建账号密码");
	scanf("%s %s",userName,pwd);
	//写入
	fprintf(fp,"%s %s\n",userName,pwd);
	//关闭
	//将光标移动到文件起始位置
	//fseek(fp,0,SEEK_SET);
	rewind(fp);
	
	char login[100],passwd[20];
	printf("输入登录账号和密码");
	scanf("%s %s",login,passwd);
	while(fscanf(fp,"%s %s",userName,pwd)!=EOF){//读取文件中的值
		if(strcmp(userName,login)==0 && strcmp(pwd,passwd)==0){
			printf("登录成功\n");
			fclose(fp);//关闭文件
			return 0;
		}
	}
	printf("登录失败\n");
	fclose(fp);
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("./file.txt","a+");//打开文件
	if(NULL==fp){
		perror("open fail");
		return -1;
	}
	
	char a[]="hello world\n";
	//将该字符串写入文件中
	fwrite(a,1,sizeof(a),fp);
	//将光标移动到起始位置
	rewind(fp);
	char buf1[1024]="";
	int ret=fread(buf1,1,sizeof(buf1),fp);
	fwrite(buf1,1,ret,stdout);
	
	//关闭文件
	fclose(fp);
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	char buf[5];
	snprintf(buf,sizeof(buf),"aaaaaaaaaaaaaaa");//编译报警告,不影响使用,只写入4个a
	printf("%s\n",buf);
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	FILE *fp;
	if((fp=fopen("./file2.txt","w"))==NULL){
		perror("");
		return -1;
	}
	int num=520;
	fwirte(&num,sizeof(num),1,fp);
	//起始位置
	rewind(fp);
	int key=0;
	int ret=fread(&key,sizeof(key),1,fp);
	printf("key=%d\n",key);

	fclose(fp);
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	//验证行缓存大小1024字节
	//如果没有使用缓存区,那么其大小为0
	printf("行缓存的大小为:%ld\n",stdin->_IO_buf_end - stdin->_IO_buf_base);
	//使用标准输入指针
	char s[128];
	fgets(s,sizeof(s),stdin);
	printf("s=%s",s);
	//使用该缓存区后,大小变为1024
	printf("行缓存stdin的大小:%ld\n",stdin->_IO_buf_end - stdin->_IO_buf_base);
	printf("行缓存stdout的大小:%ld\n",stdout->_IO_buf_end - stdout->_IO_buf_base);

	//验证全缓存区大小:
	FILE *fp=fopen("./file.txt","w");
	if(NULL==fp){
		perror("");
		return -1;
	}
	//向文件中写入数据
	fputc('H',fp);
	printf("全缓存fp大小:%ld\n",fp->_IO_buf_end-fp->_IO_buf_base);
	fclose(fp);

	//不缓存的大小
	fputs("error\n",stderr);
	printf("不缓存的大小:%ld\n",stderr->_IO_buf_end-stderr->_IO_buf_base);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值