在C控制台程序中读取(或写入)文件时出错

时间:2015-12-01 21:09:09

标签: c file

我的目的是使用c创建程序来管理文件中的记录。程序应该能够从控制台获取信息,写入文件然后从中读取。结构本身工作正常,但我没有得到我写的所有值(见输出)

enter image description here

和源代码

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

struct dob
{
    int date;
    int month;
    int year;
};

struct person
{
    int id;
    char firstName[20];
    char lastName[20];
    struct dob date;
    char email[20];
    int phoneNo;
}new;

void readRecordsFromFile();
void readRecordsFromKeyboard();

int main(int argc, const char * argv[]) {
    puts("Hello");
    while (1) {
        puts("Select option. \n 1. Read records from file. \n 2. Read records from keyboard \n Type any number to exit\n");
        int i;
        scanf("%d", &i);
        switch (i) {
            case 1:
                readRecordsFromFile();
                break;
            case 2:
                readRecordsFromKeyboard();
                break;
            default:
                return 0;
                break;
        }
    }


    return 0;
}

void readRecordsFromFile(){
    //struct person new;
    char filename[100];
    puts("Scpecify the file name to read data");
    scanf("%s", filename);
    struct person *new=malloc(sizeof(struct person));
    FILE * file= fopen(filename, "rb");
    if (file != NULL) {
        fread(new, sizeof(struct person), 1, file);
        fclose(file);
    }
    printf("\nID: %d\nName: %s\nSurname: %s\nDay of birth:%d\nMonth of birth:%d\nYear of birth:%d\nE-mail: %s\nPhone Number: %d\n",new->id,new->firstName,new->lastName,new->date.date,new->date.month,new->date.year,new->email,new->phoneNo);
}
void readRecordsFromKeyboard(){
    struct person *new=malloc(sizeof(struct person));
    puts("Enter the info about person");
    puts("ID number");
    scanf("%d", &new->id);
    puts("First Name");
    scanf("%19s", new->firstName);
    puts("Last name");
    scanf("%19s", new->lastName);
    puts("Day, month and year of birth.(by numbers, every is new line)");
    scanf("%d", &new->date.date);
    scanf("%d", &new->date.month);
    scanf("%d", &new->date.year);
    puts("Email");
    scanf("%19s", new->email);
    puts("Phone number");
    scanf("%d", &new->phoneNo);

    puts("Specify the file you want to write yor data");
    char filename[100];
    scanf("%99s",filename);
    FILE *inputf;
    inputf = fopen(filename,"wb");
    if (inputf == NULL){

        printf("Can not open the file.\n");
        exit(0);

    }else{
        if (fwrite(new, sizeof(new), 1, inputf) != 1)
        {
            fprintf(stderr, "Failed to write to %s\n", filename);
            return;
        }else{
            puts("Data saved\n");
            printf("\nID: %d\nName: %s\nSurname: %s\nDay of birth:%d\nMonth of birth:%d\nYear of birth:%d\nE-mail: %s\nPhone Number: %d\n",new->id,new->firstName,new->lastName,new->date.date,new->date.month,new->date.year,new->email,new->phoneNo);

        }

    }


    fclose(inputf);
}

1 个答案:

答案 0 :(得分:1)

这是你的问题

inputf = fopen(filename,"wb");

此命令清除文件,因为它使用“wb”打开文件。

如果要在多个运行中在该文件中写入多个记录,请使用“wb +”打开它。然后使用fseek()转到文件末尾。之后用fwrite()写下你的记录。

除了fwrite()之外,你需要使用sizeof strusture,而不是指针。你需要这样的东西:

if (fwrite(new, sizeof(struct person), 1, inputf) != 1)
{
}