IO进程线程第一天(7.25)文件IO函数fopen,perror,fclose,fprintf,fscanf,fputc,fgetc

1.用fgetc和fputc实现文件的拷贝和计算字节,行数(拷贝的是用fprintf和fscanf实现拷贝的代码)

#include
#include
#include
int main(int argc, const char *argv[])
{
    FILE *p=fopen("fscanf.txt","r");
    if(NULL==p)
    {   
        perror("fopen1");
        return -1; 
    }   
    FILE *q=fopen("fgetc.txt","w");
    if(NULL==q)
    {   
        perror("fopen2");
        return -1; 
    }   
    char a;int count=0,line=0;
    while((a=fgetc(p))!=EOF)
    {   
        fputc(a,q);
        if('\n'==a)
            line++;
        printf("%c",a);
        count++;
    }   
    fclose(p);
    fclose(q);
    printf("文件有%d字节,文件有%d行\n",count,line);
    return 0;
} 

IO进程线程第一天(7.25)文件IO函数fopen,perror,fclose,fprintf,fscanf,fputc,fgetc_第1张图片

 2.

IO进程线程第一天(7.25)文件IO函数fopen,perror,fclose,fprintf,fscanf,fputc,fgetc_第2张图片

 #include
#include
#include
int main(int argc, const char *argv[])
{
    FILE *fp=fopen("usr.txt","a+");
    char zh[20]="";
    char mm[20]="";
    char c;
    char *p;
    char zh1[20]="";
    char mm1[20]="";
    int a;
    printf("请输入账号:\n");
    scanf("%s",zh);
    printf("请输入密码\n");
    scanf("%s",mm);
    while(1)
    {
        a=fscanf(fp,"%s",zh1);
        if(a==EOF)                                                                                                                                                                                                                                                                                             
        {
            printf("账号不存在,是否注册:(y/n)\n");
            scanf("%*c%c",&c);
            if(c!='Y'&&c!='y')
            {
                return -1;
            }
            else
            {
                printf("注册该账号\n");
                fprintf(fp,"%s ",zh);
                printf("请输入密码:\n");
                scanf("%s",mm);
                fprintf(fp,"%s\n",mm);
                break;
            }
        }
        if(strcmp(zh,zh1)==0)
        {
            printf("账户存在\n");
            fscanf(fp,"%s",mm1);
            if(strcmp(mm,mm1)==0)
            {
                printf("登陆成功\n");
                return 0;
            }
            else
            {
                printf("密码错误\n");
                break;
            }
        }
        fscanf(fp,"%s",zh1);
    }
    return 0;
}

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