端午节快乐

#include

int main()
{
	time_t newtiame,otime;
	struct tm *stime;
	FILE *fp;
	char num[1024];
	int br=0;
	if ((fp=fopen("./time.txt","a+"))==NULL)
	{
		perror("error");
		return -1;

	}
	char buf[1024];
	while(fgets(buf,sizeof(buf),fp)!=NULL)
	{
		if(buf[strlen(buf)-1]=='\n')
		{
			br++;
		}
	}
	while(1)
	{
		newtiame=time(NULL);
		stime=localtime(&newtiame);
		if(newtiame!=otime)
		{
			otime=newtiame;
			sprintf(buf,"%3d.%4d-%2d-%02d %02d:%02d:%4d\n",stime->tm_year+1900,
                    stime->tm_mon+1,
                    stime->tm_mday,
                    stime->tm_hour,
                    stime->tm_min,
                    stime->tm_sec);
			fputs(buf,fp);
			fflush(fp);
		}
	}
	fclose(fp);
	return 0;
}

端午节快乐_第1张图片

 复制

#include
int main()
{
	FILE *fp1,*fp2;
	char buf[1024];
	if ((fp1=fopen("./02_text.txt","r"))==NULL)
	{
		perror("error");
		return -1;
	}
	if((fp2=fopen("./01_text.txt","w"))==NULL)
	{
		perror("error");
		return -1;
	}
	while (!feof(fp1))
	{
		int ret=fread(buf,1,sizeof(buf),fp1);
		fwrite(buf,1,ret,fp2);
	}
	printf("%s",buf);
	fclose(fp1);
	fclose(fp2);
	return 0;

}

 端午节快乐_第2张图片

端午节快乐_第3张图片

 

 端午节快乐_第4张图片

 端午节快乐_第5张图片

 

端午节快乐_第6张图片

 

 端午节快乐_第7张图片

 

 

 fsanf   fnprintf

#include
#include
#pragma warning(disable:4477)
int main()
{
	char name[10];
	FILE* fp;
	char username[100];
	errno_t err;
	err = (fopen_s(&fp, "./01text.txt", "a"));
	if (err != 0)
	{
		perror("error");
		return -1;
	}
	printf("输入账户:");
	scanf_s("%s", username,sizeof(username));
	printf("输入密码:");
	scanf_s("%s", name,sizeof(name));
	fprintf(fp, "%s %s\n", username, name);
	fclose(fp);
	puts("YSE");
	err = (fopen_s(&fp, "./01text.txt", "r"));
	if (err != 0)
	{
		perror("error");
		return -1;
	}
	char nwename[100];
	char man[10];
	printf("输入账户:");
	scanf_s("%s", nwename,sizeof(nwename));
	printf("输入密码:");
	scanf_s("%s", man,sizeof(man));
	while (fscanf_s(fp, "%s %s", username, name) != EOF)
	{
		if ((strcmp(username, nwename) == 0) && (strcmp(name, man)) == 0)
		{
			puts("成功登录");
			fclose(fp);
			return 2;
		}
	}
	
	puts("失败");
	fclose(fp);
	return 0;
}

feof ferror

#include
#include
#pragma warning(disable:4477)
int main()
{

	FILE* fp;

	errno_t err;
	err = (fopen_s(&fp, "./01text.txt", "r"));
	if (err != 0)
	{
		perror("error");
		return -1;
	}
	while (true)
	{
		char name;
		name = fgetc(fp);
		printf("%c", name);
		if (feof(fp))
		{
			puts("末尾");
			break;
		}
		else if (ferror(fp))
		{
			puts("读取失败");
			break;
		}
	}

	fclose(fp);
	return 0;
}

你可能感兴趣的:(算法,c语言,c++)