IO / day02 作业

1.在文件中打印信息如下

1:23-12-04 11:43:15
2:23-12-04 11:43:16 
3:23-12-04 11:43:17 
。。。 
ctrl + c :关闭程序 
./a.out :运行成 
4:23-12-04 13:30:50 
5:23-12-04 13:30:51 
6:23-12-04 13:30:52

 

代码:

#include 

int get_lines(char *filename)
{
	FILE *fp;

	if( (fp=fopen(filename, "r"))==NULL ) //打开文件
	{
		perror("fopen");
		return -1;
	}


	int count=0;
	char buf[128] = ""; //定义fgets一次获取字符串


	while(1)
	{

		if(fgets(buf, sizeof(buf), fp)==NULL) //判断是否到文件尾部了
		{
			break;
		}
		//printf("buf=%s", buf);

		for(int i=0; buf[i]!='\0'; i++) //搜索\n
		{

			if(buf[i]=='\n')
			{
				count++;
				//printf("count=%d\n", count);
				break;
			}
		}
	}

	fclose(fp); //关闭文件

	return count;

}



int main(int argc, const char *argv[])
{

		char filename[32]="./log.txt";



	//lines
	int lines = get_lines(filename);
	//printf("total lines=%d\n", lines);

	time_t sys_time, old_time;
	struct tm* pt;
	int n=128;
	char str_time[n];

	FILE *fp;
	if( (fp=fopen(filename, "a+"))==NULL ) //打开文件
	{
		perror("fopen");
		return -1;
	}


	while(1)
	{	
		sys_time = time(NULL);

		if(old_time != sys_time)
		{
			old_time = sys_time;
			//1. 获取系统时间的秒数
			//time_t sys_time = time(NULL);
			//time(&sys_time);


			//2.将秒数分解成时间的结构体
			pt = localtime(&sys_time);

			//此时结构体指针中的数据就是当前的系统时间
			snprintf(str_time, sizeof(str_time), "%d: %4d-%02d-%02d %02d:%02d:%02d\n", \
					lines,\
					pt->tm_year+1900,\
					pt->tm_mon+1,\
					pt->tm_mday,\
					pt->tm_hour,\
					pt->tm_min,\
					pt->tm_sec);
			printf("%s", str_time);
			fputs(str_time, fp);
			fflush(fp);
			lines++;
		}
	}

	fclose(fp);




	//create str

	//write to file





	return 0;
}
#ifndef __HEAD_H__
#define __HEAD_H__

#include 
#include 
#include 
#include 
#include 


#endif

2. 注册登录功能

#include 
#include 
#include 

//login

int rgst()
{

	//info
	char name[20];
	char psw[20];


	printf("please input user name:");
	fgets(name, sizeof(name), stdin); //回车也会被获取存入字符数组
	name[strlen(name)-1] = '\0'; // 将结尾的'\n'改为'\0'

	printf("please input password:");
	fgets(psw, sizeof(psw), stdin); //回车也会被获取存入字符数组
	psw[strlen(psw)-1] = '\0'; // 将结尾的'\n'改为'\0'
	
	FILE *pf = NULL;
	if((pf = fopen("./user.txt", "a"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	
	fprintf(pf, "%s %s\n", name, psw );

	fclose(pf);

}

int login()
{
	//1.
	char name[20];
	char psw[20];

	char usr_name[20];
	char usr_psw[20];
	
	//2.

	printf("please input login user name:");
	fgets(name, sizeof(name), stdin); //回车也会被获取存入字符数组
	name[strlen(name)-1] = '\0'; // 将结尾的'\n'改为'\0'

	printf("please input login password:");
	fgets(name, sizeof(psw), stdin); //回车也会被获取存入字符数组
	psw[strlen(psw)-1] = '\0'; // 将结尾的'\n'改为'\0'

	
	//3.
	FILE *pf = NULL; 
	if(	(pf=fopen("./user.txt", "r"))==NULL )
	{
		perror("fopen error");
		return -1;
	}

	while(1)
	{
		// read from file
		fscanf(pf, "%s %s", usr_name, usr_psw );
		printf("usr_name=%s, usr_psw=%s\n", usr_name, usr_psw);

		//跟登录信息进行比对
		if(0==strcmp(name, usr_name) && 0==strcmp(psw, usr_psw))
				{
			printf("login succeed\n");
			break;
		}
		if(feof(pf))
		{
			printf("login failed\n");
			break;
			
		}
	}
	

	//4.
	fclose(pf);

}


int main(int argc, const char *argv[])
{
	int menu = -1;

	while(1)
	{
		system("clear");
		printf("\t-----1.register-----\n");
		printf("\t-----2.login   -----\n");
		printf("\t-----0.exit    -----\n");

		printf("please select:");
		scanf("%d", &menu);
		getchar();

		switch(menu)
		{
		case 1:
			{
				rgst();
				break;
			}
		case 2:
			{
				login();					

				break;
			}
		case 0: goto END;
			

						
		default: printf("input error, please select again:\n");

		}


		printf("input any to exit!\n");
		while(getchar()!='\n');

	}

END:
	

	return 0;
}

3. 使用fread\fwrite完成两个文件的拷贝

#include 
#include 
#include 


int cpy(const char *src, const char *dest)
{	
	

	FILE *pfr, *pfw;
	if( (pfr=fopen(src, "r"))== NULL || (pfw=fopen(dest, "w"))==NULL)
	{
		perror("fopen error");
		return -1;
	}


	char buf[128];
	int size=sizeof(buf);


	while(0==feof(pfr))
	{	
		printf("-----\n")
{
	if(argc<3)
	{
		printf("input error!\n");
		printf("usage: ./a.out dest src\n");
	}

	char dest[128]=""; 
	snprintf(dest, sizeof(dest),"./%s",argv[1]);
	printf("dest=%s\n", dest);

	char src[128]="";
	snprintf(src, sizeof(src),"./%s",argv[2]);
	printf("src=%s\n", src);

	//cpy(dest, src);
	
	cpy(dest, src);
	
	
	return 0;
}

你可能感兴趣的:(数据结构,c语言,linux)