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 <myhead.h>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#endif
2. 注册登录功能
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//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 <myhead.h>
// 使用标准IO完成两个文件的拷贝
void cpy(const char *src, const char *dst)
{
//define FILE ptr
FILE *fdsrc = NULL;
FILE *fddst = NULL;
if( (fdsrc=fopen(src, "r")) == NULL ) // open src file as read only
{
perror("src fopen error");
return ;
}
if( (fddst=fopen(dst, "w")) == NULL ) // open dst file as wirte only
{
perror("dst fopen error");
return ;
}
while(!feof(fdsrc)) // copy until reach eof
{
char buf[128] = {0};
int res = fread(buf, sizeof(char), sizeof(buf), fdsrc);
fwrite(buf, sizeof(char), res, fddst); // *** shall only write what read, not buffer
}
fclose(fdsrc);
fclose(fddst);
}
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("input error");
printf("usage: ./aout srcfile, dstfile\n");
return -1;
}
cpy(argv[1], argv[2]);
return 0;
}