1/2 work

文章展示了C语言中文件操作的实践,包括使用fread和fwrite进行文件内容拷贝,以及注册登录框架和图像文件信息读写的示例。

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

1.思维导图

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Link
	{
		char name[5];
		char gender[5];
		int tail;
	}link;

int main(int argc, const char *argv[])
{
	//定义文件指针可写
	FILE *p=NULL;
	if((p=fopen(argv[1],"w"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	if(argc!=3)
	{
		fputs("input file error\n",stdout);	
		fputs("usage:./a.out srcfile\n",stderr);
		return -1;
	}

	link s[2]={{"zwt","m",182},{"cjj","w",163}};

	fwrite(s,sizeof(link),2,p);

	fclose(p);
	//定义文件指针可写
	if((p=fopen(argv[1],"r"))==NULL)
	{
		perror("fopen error");
		return -1;
	}

	//定义文件指针可读
		FILE *q=NULL;
	if((q=fopen(argv[2],"w"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	while(1)
	{
	link t;
	int rew;
	//读取p文件中的值
	rew=fread(&t,sizeof(t),1,p);
	if(rew<1)
	{
		break;
		fclose(q);
		fclose(p);
	}
	//拷贝到q argv[2]中
	fwrite(&t,sizeof(link),1,q);
	}
	return 0;
}
ubuntu

 

 

3. 将注册登录框架重新实现一遍

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int register_zc();
int login_dl();
int main(int argc, const char *argv[])
{
		char n;
	while(1)
	{
	printf("\t\t=========0、退出======\n");
	printf("\t\t=========1、登录======\n");
	printf("\t\t=========2、注册======\n");
	printf("请输入功能选项:");
	scanf("%c",&n);
	while(getchar()!='\n');
	switch(n)
	{	
	case '0':
		{
			exit(0);
		}
	case '1':
		{
			login_dl();
			break;
		}

	case '2':
		{
			register_zc();
			break;
		}
	default:printf("您输入功能选项有误\n");
	}
	printf("请输入任意键按回车清屏!!!\n");
	while(getchar()!='\n');
	system("clear");
	}
	return 0;
}

int register_zc()
{
	char id[20];
	char password[20];
	printf("请输入要注册的账号:");
	fgets(id,sizeof(id),stdin);
	printf("请输入要注册的密码:");
	fgets(password,sizeof(password),stdin);
	//将账号密码的换行符取消,存放在同一行
	id[strlen(id)-1]='\0';
	password[strlen(password)-1]='\0';
	//定义文件指针
	FILE*p=NULL;
	if((p=fopen("./user.txt","a+"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	fprintf(p,"%s %s\n",id,password);

	fclose(p);

	printf("注册成功\n");
	return 0;
}

int login_dl()
{
	char id[20],password[20];//注册过的账号密码
	char id_log[20],password_log[20];//登录的账号密码

	printf("请输入你要登录的账号:");
	fgets(id_log,sizeof(id_log),stdin);
	printf("请输入你要登录的密码:");
	fgets(password_log,sizeof(password_log),stdin);
	
	FILE*q=NULL;
	if((q=fopen("./user.txt","a+"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	id_log[strlen(id_log)-1]='\0';
	password_log[strlen(password_log)-1]='\0';
while(1)
{
	int rew;
	rew=fscanf(q,"%s %s",id,password);
	if(rew<0)
	{
		printf("登录失败\n");
		fclose(q);
		return -1;
	}
	if(strcmp(id,id_log)==0&&strcmp(password,password_log)==0)
	{
		printf("登录成功!\n");
		fclose(q);
		return 0;
	}
}
}

 

 

4.完成图像文件信息的读写操作

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
	FILE *fp=NULL;
	if((fp=fopen("./ggb.bmp","r+"))==NULL)
	{
		perror("fopen error");
		return -1;
	}

	//向后偏移两个字节得到文件的大小
	fseek(fp,2,SEEK_SET);

	unsigned int size;
	fread(&size,sizeof(size),1,fp);
	printf("size=%d\n",size);

	unsigned char color[3]={255,0,0};

	fseek(fp,54,SEEK_SET);

	for(int i=0;i<250;i++)
	{
		for(int j=0;j<959;j++)
		{
			fwrite(color,sizeof(color),1,fp);
		}
	}

	fclose(fp);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值