首页 > 解决方案 > 用结构和txt文件在C中读取函数

问题描述

有人可以帮我编写一个从 .txt 文件中读取的函数。我做了所有这些,我写了很多不同的函数来从文件中读取,但它们都不起作用。每次我按下选项 2 时,它都没有显示我写入文件的内容。代码可以正常工作,我只是不知道如何编写阅读功能。

头文件.h

#ifndef HEADER_H
#define HEADER_H


typedef struct tenant {
    char fname[30];
    char lname[30];
    int floor;
    int phone;
}TENANT;



void write(FILE*, int, TENANT*);


#endif

来源.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"
#include <iostream>
#include <conio.h>


int main() {

    int n = 0, helper = 0;
    char nameFile[100];
    TENANT *tenant = NULL;
    tenant = (TENANT*)calloc(200, sizeof(TENANT));
    FILE *file = NULL;


    printf("Name of building: ");
    scanf("%s", nameFile);
    printf("\n");

    while (n != 4) {
        printf("Press 1 for creating file! \n");
        printf("Press 2 for reading from file! \n");
        printf("Press 3 for adding new tenants! \n");
        printf("Press 4 to close the file! \n\n");
        printf("Number: ");
        scanf("%d", &n);
        printf("\n");

        switch (n)
        {
        case 1:
            file = fopen(nameFile, "w");
            printf("File created.\n");
            printf("\n");
            fclose(file);
            break;

        case 2:
            if ((file = fopen(nameFile, "r")) == NULL)
            {
                printf("File is not yet created!\n\n");
                break;
            }
            else
            {
                file = fopen(nameFile, "r");
                read(file, helper, tenant);
                printf("\n");
                fclose(file);
            }
            break;

        case 3:
            helper++;
            printf("Insert details of tenant: \n", helper);
            file = fopen(nameFile, "a");
            write(file, helper, tenant);
            printf("\n");
            fclose(file);
            break;

        case 4:
            printf("Program closed!\n");
            break;

        default:
            break;

        }
    }
    free(tenant);
    system("PAUSE");
    return 0;
}

函数.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "Header.h"

void write(FILE* file, int helper, TENANT* tenant) {

    int i = helper - 1;
    printf("Name of tenant: ");
    scanf("%s", (tenant + i)->fname);

    printf("Laste name of tenant: ");
    scanf("%s", (tenant + i)->lname);

    printf("Floor: ");
    scanf("%d", &(tenant + i)->floor);

    printf("Phone: ");
    scanf("%d", &(tenant + i)->phone);

    fprintf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
    fprintf(file, "\n//////////////////////////////////////////////////////////////////////////\n");

}

void read(FILE* file, int helper, TENANT* tenant)
{

    fread(tenant, sizeof(*tenant), 1, file);
    for (int i = 0; i < helper; i++)
    {
        fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
    }
}

标签: cfile

解决方案


我认为您不应该将函数命名为 write(),因为存在名为 write() 的系统调用,请参见 man 2 write。阅读也一样。

您应该注意一些错误,也许使用 -Wall 编译您的代码以便您查看。您的 printf() 函数格式中没有 %d ,但您将其传递给变量助手。

printf("Insert details of tenant: \n", helper);

在您的 read() 函数中,您不需要这行代码

fread(tenant, sizeof(*tenant), 1, file);

这有很多问题。您应该阅读 man 3 fread 以了解它是如何工作的。

所以,只要让你的阅读功能像这样

void read(FILE* file, int helper, TENANT* tenant)
{
    for (int i = 0; i < helper; i++)
    {
        fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n",     (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
    }
}

在你的主程序中,我认为你会遇到变量助手的问题。你像这样在主函数中初始化它

int helper=0;

因此,第一次调用 write 函数时,您将传递值为 0 的变量助手,而在函数内部,您的索引器我将从 -1 开始。这将导致索引超出范围和未定义的程序行为。也许你应该从一开始就将 helper 初始化为 1。每次调用 read 或 write 函数增量助手后,您就不会覆盖租户 [0]。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


typedef struct tenant {
char fname[30];
char lname[30];
int floor;
int phone;
}TENANT;


void write(FILE* file, int helper, TENANT* tenant) {

int i = helper - 1;
printf("Name of tenant: ");
scanf("%s", (tenant + i)->fname);

printf("Laste name of tenant: ");
scanf("%s", (tenant + i)->lname);

printf("Floor: ");
scanf("%d", &(tenant + i)->floor);

printf("Phone: ");
scanf("%d", &(tenant + i)->phone);

fprintf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
fprintf(file, "\n//////////////////////////////////////////////////////////////////////////\n");

}

void read(FILE* file, int helper, TENANT* tenant)
{

for (int i = 0; i < helper; i++)
{
    fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, &((tenant + i)->floor), &((tenant + i)->phone));
}
}


int main() {

int n = 0, helper = 1;
char nameFile[100];
TENANT *tenant = NULL;
tenant = (TENANT*)calloc(200, sizeof(TENANT));

FILE *file = NULL;


printf("Name of building: ");
scanf("%s", nameFile);
printf("\n");

while (n != 4) {
    printf("Press 1 for creating file! \n");
    printf("Press 2 for reading from file! \n");
    printf("Press 3 for adding new tenants! \n");
    printf("Press 4 to close the file! \n\n");
    printf("Number: ");
    scanf("%d", &n);
    printf("\n");

    switch (n)
    {
    case 1:
        file = fopen(nameFile, "w");
        printf("File created.\n");
        printf("\n");
        fclose(file);
        break;

    case 2:
        if ((file = fopen(nameFile, "r")) == NULL)
        {
            printf("File is not yet created!\n\n");
            break;
        }
        else
        {
            printf("read");
            read(file, helper++, tenant);

            //after you test your program for reading
            //just delete fprintf :)
            fprintf(stdout, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", tenant[0].fname, tenant[0].lname, tenant[0].floor, tenant[0].phone);
            printf("\n");
            fclose(file);
        }
        break;

    case 3:
        helper++;
        printf("Insert details of tenant: \n");
        file = fopen(nameFile, "a");
        write(file, helper, tenant);
        printf("\n");
        fclose(file);
        break;

    case 4:
        printf("Program closed!\n");
        break;

    default:
        break;

    }
}
free(tenant);
system("PAUSE");
return 0;
}

请注意,您正在使用变量助手进行读取和写入。也许为此使用不同的变量,因为如果在写入时增加变量助手,您将不会从文件的开头读取:)


推荐阅读