日期:2014-05-16  浏览次数:20580 次

read()函数问题 求助 !!
本人写了一个模拟注册登录的程序,但是无论如何登录都登录失败,几经调试个人感觉是read()函数没读到东西,希望大家能帮我分析下问题所在,代码如下
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

typedef struct Log
{
    char name[20];
    char passwd[20];
    char email[20];
}Log;

int signup(Log log,int fd)
{
    scanf("%*c");
    printf("请输入用户名:");
    scanf("%s",log.name);
    char temp[20];
    int flag = 0;
    while(!flag)
    {
        printf("请输入密码:");
        scanf("%s",log.passwd);
        printf("请再次输入密码:");
        scanf("%s",temp);
        if(strcmp(log.passwd,temp))
        {
            flag = 0;
        }
        else break;
    }
    printf("请输入邮箱:");
    scanf("%s",log.email);
    int res = write(fd,&log,sizeof(log));
    if(res == -1) perror("write"),exit(-1);
    return 1;
}

int Loggin(Log log,int fd)
{
    printf("请输入用户名:");
    scanf("%s",log.name);
    printf("请输入密码:");
    scanf("%s",log.passwd);
    int len = lseek(fd,0,SEEK_END)/sizeof(log);
    struct Log temp[len]; 
    int i = 0;
    int res = read(fd,&temp[i],sizeof(Log));
    if(res == -1) perror("read"),exit(-1);
    printf("%s,%s\n",temp[i].name,temp[i].passwd);
    while(res)
    {
        i++;
        res = read(fd,&temp[i],sizeof(Log));
        if(res == -1) perror("read"),exit(-1);
    }
    i = 0;
    for(i=0;i<len-1;i++)
    {
        if(strcmp(log.name,temp[i].name) == 0 && strcmp(log.passwd,temp[i].passwd) == 0)
        {
            return 1;
            break;
        }
    }
    return 0;
}

int main()
{
    int fd = open("log",O_CREAT|O_RDWR|O_APPEND,0666);
    if(fd == -1) perror("open"),exit(-1);
    int select;
    struct Log&n