io注册登录界面和fread\fwrite实现复制

注册登录界面

main.c 

#include "log.h"
int main(int argc, const char *argv[])
{
	int ch;
	while(1)
	{

	printf("\t\t\t\n1.注册");
	printf("\t\t\t\n2.登录");
	printf("\t\t\t\n3.退出");
	printf("\n请输入你的选择:");

	scanf("%d",&ch);
	getchar();
	switch(ch)
	{
	case 1:
		regist();
		break;
	case 2:
		login();
		break;
	case 3:
		exit(EXIT_SUCCESS);
		break;
	}
	}

	return 0;
}

reist.c 

#include "log.h"
int regist()
{
	user reg;
	printf("请输入注册的账户:\n");
	fgets(reg.name,sizeof(reg.name),stdin);
	printf("请输入注册的密码:\n");
	fgets(reg.code,sizeof(reg.code),stdin);

	FILE *fp= fopen("./user.txt","w");
	if(fp==NULL)
	{
		perror("fopen");
		return -1;
	}
	fprintf(fp,"%s %s",reg.name,reg.code);
	printf("注册成功\n");
	fclose(fp);
	return 0;
}

login.c 

#include "log.h"
int login()
{
	user log,reg;
	printf("请输入登录账户:\n");
	fgets(log.name,sizeof(log.name),stdin);
	printf("请输入登录密码:\n");
	fgets(log.code,sizeof(log.code),stdin);

	log.name[strlen(log.name)-1]='\0';
	log.code[strlen(log.code)-1]='\0';
	FILE *fp = fopen("./user.txt","r");
	if(fp==NULL)
	{
		perror("fopen");
		return -1;
	}
	fscanf(fp,"%s%s",reg.name,reg.code);
	if(strcmp(log.name,reg.name)==0&&
			strcmp(log.code,reg.code)==0)
	{
		printf("登陆成功");
	}
	else
	{
		printf("失败");
	}
	return 0;
}

log.h 

#ifndef _LOG_H_
#define _LOG_H_
#include <myhead.h>
typedef struct
{
	char name[20];
	char code[20];
}user;
int login();
int regist();
#endif

fwrite和fread实现复制 

ubuntu@ubuntu:day2$ 
ubuntu@ubuntu:day2$ cat 1.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
	int a=1000;
	int num;
	FILE *fp1=fopen("./1.txt","w+");
	if(fp1==NULL)
	{
		perror("fopen");
		return -1;
	}
	fwrite(&a,sizeof(int),1,fp1);
	fclose(fp1);//写入1000

	fp1=fopen("./1.txt","r");
	fread(&num,1,sizeof(int),fp1);
	printf("num=%d\n",num);
	fclose(fp1);//读取1000

	FILE *fp2=fopen("./2.txt","w+");
	fwrite(&num,1,sizeof(int),fp2);
	printf("num=%d\n",num);
	fclose(fp2);//赋值1000

	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 首先,`fread()`和`fwrite()`是C语言标准库中的函数,用于读取和写入文件数据。它们的基本语法如下: ```c size_t fread(void *ptr, size_t size, size_t count, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); ``` 其中,`ptr`是指向要读取或写入数据的缓冲区指针,`size`是每个数据项的大小(字节数),`count`是要读取或写入的数据项数,`stream`是指向`FILE`类型的文件指针。 要实现`cp`命令的功能,可以使用`fread()`和`fwrite()`函数来读取和写入文件数据。下面是一个简单的示例程序,用于复制一张图片: ```c #include <stdio.h> #include <stdlib.h> int main() { FILE *src, *dst; char buf[1024]; size_t n; // 打开源文件和目标文件 src = fopen("source.jpg", "rb"); dst = fopen("destination.jpg", "wb"); if (src == NULL || dst == NULL) { printf("Error: failed to open file.\n"); exit(1); } // 读取并写入文件数据 while ((n = fread(buf, 1, sizeof(buf), src)) > 0) { fwrite(buf, 1, n, dst); } // 关闭文件 fclose(src); fclose(dst); return 0; } ``` 在上面的程序中,我们首先打开源文件和目标文件,然后循环读取源文件中的数据,并将其写入目标文件中,直到源文件被完全复制。最后,关闭文件并退出程序。 请注意,此代码可能需要根据您的系统和编译器进行调整。此外,我们还需要确保源文件和目标文件在正确的位置,并且目标文件不存在或者可以被覆盖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值