IO-DAY2-(fwrit、fread.......)

作业一、

要求计算一个文件有几行。文件的最后一行也有一个'n'

#include 
#include 
#include 
#include 

int main(int argc, const char *argv[])
{
    FILE *fp1 = fopen("./1.txt", "r");
    FILE *fp2 = fopen("./2.txt", "w");
    if (NULL == fp1 || NULL == fp2)
    {
        perror("fp1");
        perror("fp2");
        return -1;
    }

    char c[50] = "";
    char a[50] = "";
    int count = 0,count1=1;
    int byt=0;
    while (fgets(c, sizeof(c), fp1) != NULL)
    {

        fputs(c, fp2);
        byt=strlen(c)-1;
        if(c[byt]=='\n')
            count1++;
        count++;
        bzero(c, sizeof(c));

    }

    long size = 0;
    size = ftell(fp2);
    printf("size=%ld\n", size);
    printf("2.txt文件的行数是:%d\n",count1);

    fclose(fp1);
    fclose(fp2);
    return 0;
}

作业二、

IO-DAY2-(fwrit、fread.......)_第1张图片

#include 
#include 
#include 
#include 
#include 

int main(int argc, const char *argv[])
{

    FILE *fp = fopen("./1.txt", "a+");
    if (NULL == fp)
    {
        perror("fp");
        return -1;
    }

    time_t t;

    struct tm *info = NULL;

    char c;
    int n = 0, count = 1;

    while (fscanf(fp, "%c", &c) != EOF)
    {

        if ('\n' == c)
        {
            n = count++;
        }
    }

    while (1)
    {
        int i = 0;
        time(&t);
        info = localtime(&t);

        sleep(1);

        fprintf(fp, "[%d]%d-%02d-%02d %02d:%02d:%02d\n",
                n++, info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
                info->tm_hour, info->tm_min, info->tm_sec);
        fflush(fp);
        i++;
    }

    fclose(fp);
    return 0;
}

作业三、

实现注册和登录功能

#include 
#include 
#include 

typedef struct my_DATE //查重注册时账户重名
{
    char name[100];
    char pw[100];
} Date;

typedef struct DATE //登录功能验证
{
    char name[100];
    char pw[100];
} Date1;

int main(int argc, const char *argv[])
{


    FILE *fp = fopen("./usr.txt", "r+");
    if (NULL == fp)
    {
        perror("fp");
        return -1;
    }

    int count1=0;
    char c;
    while (fscanf(fp, "%c", &c) != EOF)
    {

        if ('\n' == c)
        {
           count1++;
        }
    }
    printf("%d\n",count1);
    Date date1[count1];

    fseek(fp, 0, SEEK_END);

    int n = 0;
    printf("1.注册账户\n");
    printf("2.登录账户\n请输入和功能对应的编号:");
    scanf("%d", &n);

    int count = 0;

    switch (n)
    {
    case 1:
    {
        //=====================================================================
        // 注册功能
        fseek(fp, 0, SEEK_SET);
        char reg_name[20]; // 注册用户名称
        char reg_pw[20];   // 注册用户密码
        int count = 0;
        int i = 1;
        while (i == 1)
        {
            printf("请输入要申请的用户名:");
            scanf("%s", reg_name);
            for (int i = 0; fscanf(fp, "%s %s", date1[i].name, date1[i].pw) != EOF; i++)
            {
                
            }
            
   
            while (strcmp(reg_name, date1[i].name) == 0)
            {
                printf("用户名重复\n");
                return -1;
            }

            printf("请输入一个新密码:");
            scanf(" %s", reg_pw);

            fprintf(fp, "%s", reg_name);
            fprintf(fp, " %s\n", reg_pw);

            count++;
            printf("如果要继续注册请输入“1”,退出请输入“0”");
            scanf(" %d", &i);
        }
    }

    case 2:
    {
        //======================================================================
        // 登录功能
        fseek(fp, 0, SEEK_SET);
        Date1 date[count1];
        
        for (int i = 0; i < count1; i++)
        {
            fscanf(fp, "%s %s", date[i].name, date[i].pw);
        }
        
        
        char my_name[20] = "", my_pw[20] = "";
        printf("请输入用户名:");
        scanf("%s", my_name);
        printf("请输入用户密码:");
        scanf("%s", my_pw);

        for (int i = 0; i < count1; i++)
        {
            if (strcmp(my_name, date[i].name) == 0)
            {
                if (strcmp(my_pw, date[i].pw) == 0)
                {
                    printf("登录成功\n");
                    fclose(fp);
                    return 0;
                }

                printf("密码错误\n");
                fclose(fp);
                return 0;
            }
        }

        printf("账号不存在\n");
    }

    default:
        printf("输入有误\n");
    }

    fclose(fp);

    return 0;
}

你可能感兴趣的:(c语言,算法,数据结构)