20240102 (IO) 作业

1> 使用fread、fwrite完成两个文件的拷贝
#include <stdio.h>

int main(int argc, const char *argv[])
{
	if(argc<3){
		printf("!!! the arguments you entered less than 3,please enter again !!!\n");
		return -1;
	}

	FILE * sfp=NULL;
	if((sfp=fopen(argv[1],"r"))==NULL){
		perror("open source file error");
		return -1;
	}

	FILE * dfp=NULL;
	if((dfp=fopen(argv[2],"w"))==NULL){
		perror("open destination file error");
		return -1;
	}

	fseek(sfp,0,SEEK_END);
	int fileSize=ftell(sfp);
	printf("fileSize=%d\n",fileSize);
	char buf[fileSize];
//	printf("fileSize=%d   sizeof(buf)=%ld\n",fileSize,sizeof(buf));
	int ret=fread(buf,1,sizeof(buf),sfp);
	for(int i=0;i<fileSize;i++){
		printf("%c",buf[i]);
	}
	puts("");
	fwrite(buf,1,fileSize,dfp);

	fclose(dfp);
	fclose(sfp);


	return 0;
}
2> 将注册登录框架重新实现一遍
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int do_regist(){
	char userName[20]="";
	char password[20]="";
	printf("please enter your user name within 19 characters:\n");
	fgets(userName,sizeof(userName),stdin);
	//while(getchar()!='');

	printf("please enter your password within 19 characters:\n");
	fgets(password,sizeof(password),stdin);

	//change the \n to \0 at the end of each string inputed
	if(userName[strlen(userName)-1]=='\n') userName[strlen(userName)-1]='\0';
	if(password[strlen(password)-1]=='\n') password[strlen(password)-1]='\0';

	//open the account file
	FILE * rfp=NULL;
	if((rfp=fopen("./users.txt","a+"))==NULL){
		perror("register:open users.txt error");
		return -1;
	}

	//format info and print into file
	fprintf(rfp,"%s %s\n",userName,password);

	//close opened file
	fclose(rfp);

	printf("register successfully !\n");
	return 0;
}

int do_login(){
	char userName_input[20]="";
	char password_input[20]="";
	char userName_reg[20]="";
	char password_reg[20]="";

	printf("please enter your username:");
	fgets(userName_input,sizeof(userName_input),stdin);
	if(userName_input[strlen(userName_input)-1]=='\n') userName_input[strlen(userName_input)-1]='\0';

	printf("please enter your password:");
	fgets(password_input,sizeof(password_input),stdin);
	if(password_input[strlen(password_input)-1]=='\n') password_input[strlen(password_input)-1]='\0';
	
	FILE *lfp=NULL;
	if((lfp=fopen("./users.txt","r"))==NULL){
		perror("login:open users.txt error");
		return -1;
	}

	while(fscanf(lfp,"%s %s",userName_reg,password_reg)!=EOF){
		if(strcmp(userName_input,userName_reg)==0 && strcmp(password_input,password_reg)==0){
			printf("login successfully !\n");
			fclose(lfp);
			return 0;
		}
	}
	printf("login failed !\n");
	fclose(lfp);

	return -1;
}

int main(int argc, const char *argv[])
{
	char menu;
	while(1){
		//1.print menu
		printf("*** menu ***\n");
		printf("1.register\n");
		printf("2.login\n");
		printf("0.exit\n");
		printf("please select and enter number:");

		scanf("%c",&menu);
		//2.switch to select corresponding function
		while(getchar()!='\n');
		switch(menu){
			case '1':
				do_regist();
				break;
			case '2':
				do_login();
				break;
			case '0':
				exit(EXIT_SUCCESS);
			default:
				printf("entered wrong number! please enter again.\n");
		}
		printf("please enter any key to clear screen!\n");
		while(getchar()!='\n');
		system("clear");
	}
	return 0;
}
3> 完成图像文件信息的读写操作
#include <stdio.h>

int main(int argc, const char *argv[])
{
	FILE *bmpfp=NULL;
	if((bmpfp=fopen("./b.bmp","r+"))==NULL){
		perror("open bnp file error");
		return -1;
	}
	int buf=0;
	fseek(bmpfp,2,SEEK_SET);
	fread(&buf,sizeof(buf),1,bmpfp);
	printf("buf=%d\n",buf);
	fseek(bmpfp,48,SEEK_CUR);

	unsigned char color[3]={0,255,0};
	for(int i=0;i<1920;i++){
		for(int j=0;j<200;j++){
			fwrite(color,sizeof(color),1,bmpfp);
		}
	}

	fclose(bmpfp);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值