#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,const char *argv[])
{
if(argc<2){
printf("请输入文件名\n");
return -1;
}
FILE *fp = fopen(argv[1],"r");
if(NULL == fp){
perror("fopen");
return -1;
}
int count=0;
char s[32]="";
while(1){
char *flag = fgets(s,sizeof(s),fp);
if(NULL == flag)
break;
if(s[strlen(s)-1] == '\n')
count++;
}
printf("文件行数为:%d\n",count);
fclose(fp);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#include <unistd.h>
int main(int argc,const char *argv[])
{
FILE *fp = fopen("./time.txt","a+");
if(NULL == fp){
perror("fopen");
return -1;
}
time_t ti;
struct tm * t;
int count=0;
char s[32] = "";
while(fgets(s,sizeof(s),fp) != NULL){
if(s[strlen(s)-1] == '\n'){
count++;
}
}
while(1){
count++;
ti = time(&ti);
t = localtime(&ti);
fprintf(stderr," %d-%02d-%02d %02d:%02d:%02d\r",\
t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
fprintf(fp,"[%d] %d-%02d-%02d %02d:%02d:%02d\n",\
count,t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
fflush(fp);
sleep(1);
}
fclose(fp);
return 0;
}
第三个作业:实现注册功能,同一用户名不能重复注册
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,const char *argv[])
{
char in_name[20] = "";
char in_password[20] = "";
printf("请输入用户名:");
scanf("%s",in_name);
printf("请输入密码:");
scanf("%s",in_password);
//打开user.txt
FILE *fp = fopen("./user.txt","a+");
if(NULL == fp){
perror("fopen");
return -1;
}
fseek(fp,0,SEEK_SET);
char sys_name[20];
char sys_password[20];
while(1){
//从系统录入用户名
if(fscanf(fp,"%s %s",sys_name,sys_password) == EOF){
fprintf(fp,"%s %s\n",in_name,in_password);
// fprintf(fp,"\n");
printf("注册成功\n");
break;
}
//进行用户名比较,存在则退出
if(strcmp(in_name,sys_name) == 0){
printf("用户名存在\n");
break;
}
}
return 0;
}