IO day1

1.

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char *argv[])
{
    char c = 0;
    int size = 0, n = 0;
    FILE *fp1 = fopen("1.txt", "r");
    FILE *fp2 = fopen("2.txt", "w");
    if (fp1 == NULL || fp2 == NULL)
    {
        perror("fopen");
        return -1;
    }
    while (c != -1)
    {
        c = fgetc(fp1);
        if (c == '\n')
        {
            n++;
        }
        if (c != -1)
        {
            size++;
            fputc(c, fp2);
        }
    }
    printf("copy success\n");
    printf("size=%d\n", size);
    printf("换行有%d个\n", n);
    return 0;
}

 

2.main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct usr
{
    char usr[20];
    char pwd[20];
};

int main(int argc, const char *argv[])
{
    char u[20];
    char p[20];
    FILE *fp = fopen("usr.txt", "r");
    if (NULL == fp)
    {
        perror("fclose");
        return -1;
    }
    struct usr usr[4];
    for (int i = 0; i < 4; i++)
    {
        fscanf(fp, "%s %s\n", usr[i].usr, usr[i].pwd);
    }
    printf("请输入用户名:\n");
    scanf("%s", u);
    printf("请输入密码:\n");
    scanf("%s", p);
    for (int i = 0; i < 4; i++)
    {

        if (strcmp(u, usr[i].usr) == 0)
        {
            if (strcmp(p, usr[i].pwd) == 0)
            {
                printf("密码正确登录成功\n");
                // 关闭
                fclose(fp);
                return 0;
            }
            printf("密码错误\n");
            // 关闭
            fclose(fp);
            return -1;
        }
    }
    printf("账户不存在\n");
    // 关闭
    fclose(fp);

    return -1;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值