用C语言更新记录

时间:2015-03-04 20:16:46

标签: c file updates

有人可以帮我解决这个问题。当我尝试这个代码时它根本不起作用。我在使用C语言处理文件的经验非常少。请帮我解决这个问题

这是结构

struct passenger
{
    char airline[20];
    char name[20];
    int age;
    char gender;
    char clas[10];
    int seatNo;

}e;

这是插入记录的功能

oid reserve()
{
    int airline, clas;
    printf("\nSelect your flight:");
    printf("\n1. PIA\n2. Shaheen\n3. Air Blue\n\nEnter Choice:");
    scanf("%d", &airline);
    if (airline == 1)
        strcpy(e.airline, "\nPIA");
    else if (airline == 2)
        strcpy(e.airline, "\nShaheen");
    else if (airline == 3)
        strcpy(e.airline, "\nAirBlue");

    printf("\nEnter Name:");
    scanf("%s", &e.name);

    printf("\nEnter Age:");
    scanf("%d", &e.age);

    printf("\nEnter Your Gender (M/F): ");
    e.gender = getche();

    printf("\n\nPress 1 for ECONOMY CLASS and 2 for BUSSINESS CLASS: ");
    scanf("%d", &clas);
    if (clas == 1)
        strcpy(e.clas, "Economy");
    else
        strcpy(e.clas, "Bussiness");
    e.seatNo = rand() % 500;
    printf("\nYour seat num is: %d\n\n", e.seatNo);

    if (airline==1)
        save = fopen("PIA.DAT", "a");
    else if (airline==2)
        save = fopen("Shaheen.DAT", "a");
    else
        save = fopen("AirBlue.DAT", "a");
    fwrite(&e, sizeof( struct passenger), 1, save);
    fclose(save);

    getch();
}

这是用于更新记录

void update()
    int  i = 1, count = 0, clas, record = 0;
    int seat, airline;
    struct passenger se;
    printf("\n\n\nSelect your flight:");
    printf("\n1. PIA\n2. Shaheen\n3. Air Blue\n\nEnter Choice:");
    scanf("%d", &airline);

    if (airline == 1)
        updateR = fopen("PIA.DAT", "r+");
    else if (airline == 2)
        updateR = fopen("Shaheen.DAT", "r+");
    else
        updateR = fopen("AirBlue.DAT", "r+");

    if (updateR == NULL)
    {
        printf("File not found.");
    }
    else
    {
        printf("\nEnter Your Seat No: ");
        scanf("%d", &seat);
        while (!feof(updateR))
        {
            fread(&se, sizeof(struct passenger), 1, updateR);
            if (se.seatNo == seat)
            {
                printf("\nEnter Name:");
                scanf("%s", &e.name);

                printf("\nEnter Age:");
                scanf("%d", &e.age);

                printf("\nEnter Your Gender (M/F): ");
                e.gender = getche();

                printf("\n\nPress 1 for ECONOMY CLASS and 2 for BUSSINESS CLASS: ");
                scanf("%d", &clas);
                if (clas == 1)
                    strcpy(e.clas, "Economy");
                else
                    strcpy(e.clas, "Bussiness");
                fseek(updateR,sizeof(struct passenger)* record, SEEK_END);
                fwrite(&e, sizeof(&e), 1, updateR);
                break;
            }
        }
        fclose(updateR);
    }
    getch();
}

3 个答案:

答案 0 :(得分:0)

完整分析缺少相当多的信息。但乍一看,你从文件的末尾开始寻求。您正在使用SEEK_END。通常你应该使用SEEK_SET。

答案 1 :(得分:0)

scanf在扫描字符串时不需要address-of运算符。

所以你在哪里:

scanf("%s", &e.name);

你真的应该:

scanf("%s", e.name);

答案 2 :(得分:0)

您正在打开文本文件,并编写结构*的内容,这可能无法按预期方式工作。我不知道你的文件格式是什么,你还没有指定它。 假设一个逗号分隔的变量格式化文件,每行一个记录。它看起来像这样:

Alaska, Jones, 23, 1, First, 12
NW, Smith, 12, 1, No, 34
...  

然后,从该文件中写入和读取 的代码可能如下所示:

typedef struct passenger
{
    char airline[20];
    char name[20];
    int age;
    char gender[2];//note re-definition from your original
    char clas[10];
    int seatNo;

}E;

void UpdateRecord(char *path, char *record);
void ReadRecord(char *path, E *db, int record);
int getLines(char *path);

int main(void)
{
    FILE *fp = {0};
    char line[80] = {"Alaska2, Jones2, 23, M, First, 12\n"};
    E e;

    UpdateRecord(".\\new.dat", line);
    ReadRecord(".\\new.dat", &e, 1); //Get first line
    ReadRecord(".\\new.dat", &e, getLines(".\\new.dat")); //Get last line

    return 0;
}
void UpdateRecord(char *path, char *record)
{
    FILE *fp = fopen(path, "a");
    if(fp)
    {
        fputs(record, fp);
    }
    fclose(fp);
}

void ReadRecord(char *path, E *db, int record)
{
    int i = 0;
    FILE *fp = fopen(path, "r");
    char line[80];
    char *buf = {0};
    while(fgets(line, 80, fp))
    {
        if(++i == record)
        {
            //get e.airline
            buf = strtok(line, " ,\n");
            if(buf) strcpy(db->airline, buf);
            //get e.name
            buf = strtok(NULL, " ,\n");
            if(buf) strcpy(db->name, buf);
            //get e.age
            buf = strtok(NULL, " ,\n");
            if(buf) db->age = atoi(buf);
            //get e.gender
            buf = strtok(NULL, " ,\n");
            if(buf) strcpy(db->gender, buf);
            //get e.clas
            buf = strtok(NULL, " ,\n");
            if(buf) strcpy(db->clas, buf);
            //get e.seatNo
            buf = strtok(NULL, " ,\n");
            if(buf) db->seatNo = atoi(buf); 
        }
    }
    fclose(fp);
}

int getLines(char *path)
{
    int lines = 0;
    char line[80];
    FILE *fp = fopen(path, "r");
    if(fp)
    {
        while(fgets(line, 80, fp))
        {
            lines++;
        }
        fclose(fp);
    }
    return lines;
}