2/18作业

 1、使用fgets统计给定文件的行数


#include
#include
#include
#define MAXSIZE 5
int main(int argc, const char *argv[])
{
	//判断是否进行外部传参
	if(argc != 2)
	{
		printf("input file error");
		printf("usage:./a.out filename\n");
		return -1;
	}
	//定义文件指针,以只读的形式打开传入的文件
	FILE *fp = NULL;
	if((fp =fopen(argv[1],"r"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	//统计行号
	int line=0;
	char buf[MAXSIZE];
	while(1)
	{
		fgets(buf,sizeof(buf),fp);
		for(int i=0;i

2、使用fputs和fgets完成两个文件的拷贝

#include
#include
#include
#define MAXSIZE 10
 
int main(int argc, const char *argv[])
{
	//判断是否进行了外部传参
	if(argc !=3)
	{
		printf("input file error\n");
		printf("usage:./a.out srcfile destfile\n");
		return -1;
	}
	//定义文件指针,以只读的形式打开源文件
	FILE *srcfp = NULL;
	if((srcfp = fopen(argv[1],"r"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	//定义文件指针,以只写的形式打开源文件
	FILE *destfp = NULL;
	if((destfp = fopen(argv[2],"w"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	//遍历文件字符
	char buf[MAXSIZE];
	//循环从文件中读取字符串
	while(1)
	{
		//将读取的数据放入目标文件中
		fgets(buf,sizeof(buf),srcfp);
		fputs(buf,destfp);
		if(strlen(buf)==0)
			break;
		memset(buf,0,sizeof(buf));
	}
 
	printf("copy success!\n");
 
	//关闭文件
	fclose(srcfp);
	fclose(destfp);
 
	return 0;
}

3.


#include
#include
#include
#define MAXSIZE 100
 
void menu();
void Register();
void Login();
 
int main(int argc, const char *argv[])
{
	while(1)
	{
		menu();
	}
	return 0;
}
void menu()
{
	int menu = 0;
	while(1)
	{
		printf("\t\t==========QQ登录界面=========\n");
		printf("\t\t==========1、注册 =========\n");
		printf("\t\t==========2、登录 =========\n");
		printf("\t\t==========0、退出 =========\n");
		printf("please enter selection:");
		scanf("%d",&menu);
		getchar();
 
		//菜单多分支选择
		switch(menu)
		{
		case 1:Register();
					break;
		case 2:Login();
					break;
		case 0:goto END;
 
		default:printf("Error Input!Please input again\n");
				break;
		}
	}
END:
	return;
}
void Register()	
{
	char bufRe[MAXSIZE]="";
	printf("please enter accout and password:\n");
	FILE *fp=NULL;
	if((fp=fopen("user.txt","a+"))==NULL)
	{
		perror("fopen error");
		return;
	}
	fgets(bufRe,sizeof(bufRe),stdin);
	fputs(bufRe,fp);
	memset(bufRe,0,sizeof(bufRe));
	fclose(fp);
	printf("Register Success!");
	printf("\n\n");
}
void Login()	
{
	char bufLg1[MAXSIZE]="";
	printf("please enter accout and password:\n");
    fgets(bufLg1,sizeof(bufLg1),stdin);
	FILE *fp=NULL;
	if((fp=fopen("user.txt","r"))==NULL)
	{
		perror("fopen error");
		return;
	}
	char bufLg2[MAXSIZE]="";
	while(1)
	{
		if(!fgets(bufLg2,sizeof(bufLg2),fp))
			break;
		if(strcmp(bufLg2,bufLg1)==0)
		{
			printf("Login Success!");
			printf("\n\n");
			return;
		}
		memset(bufLg2,0,sizeof(bufLg2));
	}
	printf("Login Fail!");
	printf("\n\n");
}

你可能感兴趣的:(java,前端,服务器)