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

标签 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);
}

最佳答案

这是你的问题

inputf = fopen(filename,"wb");

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

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

除了 fwrite() 你需要使用 sizeof 结构,而不是指针。意味着你需要这样的东西:

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

关于c - 在 C 控制台程序中读取(或写入)文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34030181/

相关文章:

java - 如何在Java中获取文本文件的随机行?

c - AT&T 程序集 : how to mult a double by 2 without local variable. Shift Double?

c - 如何检查 BDS/北斗卫星系统的 BCH(15,11,1) 代码/校验和

c - 如何从 CUDA 内核函数返回单个变量?

c - 只获取c/Ubuntu中某个目录下的文件

Delphi:检查文件是否正在使用

php - 服务器中的安全文件存储

file - 如何使用 SVN 提交除一个文件之外的所有文件

c - ';' 标记前的语法错误

c - 如何使用 LoadLibrary() 和 GetProcAddress() 函数将 ntdll.dll 添加到项目库中?