IO进程线程day2

#include <stdio.h>
#include <string.h>
#include <stdlib.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 t;
    //printf ("%ld\n", t);
    struct tm *info = NULL;
    int i=1;
    int j=1;
    char buf;
    while (1)
    {
        if(fscanf (fp, "%c", &buf) != EOF)
        {
            if ('\r' == buf)
            {
                j++;i++;
            }
        }else
        {
            break;
        }
    }
    printf ("%d %d\n", j,i);
    while(1)
    {
        time (&t);
        info = localtime(&t);
        //fprintf (stderr, "[%d] %d-%02d-%02d %02d:%02d:%02d\r", i++, info -> tm_year+1900, info ->tm_mon+1, info ->tm_mday, \
        info ->tm_hour, info -> tm_min, info -> tm_sec);
        sleep(1);
        fprintf (fp, "[%d] %d-%02d-%02d %02d:%02d:%02d\r", j++, info -> tm_year+1900, info ->tm_mon+1, info ->tm_mday, \
        info ->tm_hour, info -> tm_min, info -> tm_sec);
        fflush(fp);
    }
    fclose(fp);
    return 0;
}

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

int main(int argc,const char * argv[])
{
    FILE *fp1 = fopen("./read.c", "w");
    FILE *fp2 = fopen("./fputs_fgets.c", "r");
    if (NULL == fp1 || NULL == fp2)
    {
        perror("fopen");
        return -1;
    }
    char s[5] = "";
    int line = 0;
    while(fgets(s, sizeof(s), fp2))
    {
        fputs(s, fp1);
        while(10 == s[strlen(s)-1])
        {
                line++;
                break;
        }
    }
    fseek(fp2, 0, SEEK_END);
    int n=ftell(fp2);
    printf ("文件大小 = %d\n文件行数 = %d\n", n, line);
    return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct 
{
    char name [20];
    char cipher [20];
}* Node,node;
void logon (FILE *fp, Node p);
int mume(FILE *fp, Node p);
void Sign_in (FILE *fp, Node p);
void Exit();
void Reenter(FILE *fp, Node p,char name1[20]);
int main(int argc,const char * argv[])
{
    FILE *fp=fopen("./usr.txt", "a+");
    if (NULL==fp)
    {
        perror("fopen");
        return -1;
    }
    Node p = (Node)malloc(sizeof (node));
    mume(fp, p);
    fclose (fp);
    return 0;
}
int mume(FILE *fp, Node p)
{
    puts("1.注册账号");
    puts("2.登录账号");
    puts("0.退出程序");
    while(1)
    {
        int sum;
        printf ("请输入要执行的功能:");
        scanf ("%d", &sum);
        switch (sum)
        {
            case 1:
            {
                logon(fp, p);
            }break;
            case 2:
            {
                Sign_in(fp, p);
            }break;
            case 0:
            {
                Exit();
            }break;
            default :printf ("此功能不存在\n");break;
        }
    }
}
//注册
void logon (FILE *fp, Node p)
{
    fp=fopen("./usr.txt", "a+");
    printf ("请输入账号:");
    scanf ("%s", p->name);
    printf ("请输入密码:");
    scanf ("%s", p->cipher);
    char name1[20]; char cipher1[20];
    while(fscanf (fp, "%s %s", name1, cipher1) != EOF)
    {
        if (strcmp(p->name, name1) == 0)
        {
            printf ("该账号已存在,请重新输入\n");
            fclose(fp);
            return ;
        }else
        {
            break;
        }
    }
    fprintf (fp, "%s %s\n", p->name, p->cipher);
    printf ("该账号注册成功\n");
    fclose (fp);
    return ;
}
//登录
void Sign_in (FILE *fp, Node p)
{
    fp=fopen("./usr.txt", "a+");
    printf ("请输入账号:");
    scanf ("%s", p->name);
    printf ("请输入密码:");
    scanf ("%s", p->cipher);
    char name1[20]; char cipher1[20];
    while(fscanf (fp, "%s %s", name1, cipher1) != EOF)
    {
        if (strcmp(p->name, name1) == 0)
        {
            if (strcmp(p->cipher, cipher1) == 0)
            {
                printf ("登录成功\n");
                fclose(fp);
                return;
            }else
            {
                printf ("密码错误,请重新输入\n");
                Reenter(fp, p, name1);
            }
        }
    }
    printf ("该账号不存在,请注册账号\n");
    fclose(fp);
    return ;
}
//退出程序
void Exit()
{
    exit(0);
}
//重新输入密码
void Reenter(FILE *fp, Node p, char name1[20])
{
    char cipher1[20];
    fp=fopen("./usr.txt", "a+");
    printf ("请输入密码:");
    scanf ("%s", p->cipher);
    while(fscanf (fp, "%s %s", name1, cipher1) != EOF)
    {
        if (strcmp (p->name, name1) == 0)
        {
         if (strcmp(p->cipher, cipher1) == 0)
            {
                printf ("登录成功\n");
                fclose(fp);
                return ;
            }else
            {
                printf ("密码错误,请重新输入\n");  
                Reenter(fp, p, name1);
            }
        }
     }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值