注册功能
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("usr.txt","a+");
char name[20];
char password[20];
char str[20];
scanf("%s %s",name,password);
while(1)
{
if(fscanf(fp,"%s%*s",str)==EOF)
{
fprintf(fp,"%s %s",name,password);
break;
}
if(!strcmp(name,str))
{
printf("%s已存在",name);
break;
}
}
fclose(fp);
return 0;
}
用fgetc实现一个文件有几行,文件大小
#include <stdio.h>
int countLine(const char *name);
int countSize(const char *name);
int main(int argc, const char *argv[])
{
printf("%d %d",countLine(argv[1]),countSize(argv[1]));
return 0;
}
int countLine(const char *name)
{
FILE *fp=fopen(name,"r");
if(!fp)
{
perror("fopen");
return -1;
}
int num=0;
int c=0;
while((c=fgetc(fp))!=EOF)
{
if(c=='\n')
num++;
}
if(fclose(fp)==EOF)
{
perror("fclose:");
}
return num;
}
int countSize(const char *name)
{
FILE *fp=fopen(name,"r");
if(!fp)
{
perror("fopen");
return -1;
}
int num=0;
int c=0;
while((c=fgetc(fp))!=EOF)
{
num++;
}
if(fclose(fp)==EOF)
{
perror("fclose:");
}
return num;
}