c - C语言读取函数结构体和txt文件

标签 c file

有人可以帮我编写一个从 .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);
    }
}

最佳答案

我认为你不应该将你的函数命名为 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。 每次调用读取或写入函数后都会增加助手,这样您就不会覆盖租户[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;
}

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

关于c - C语言读取函数结构体和txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50295541/

相关文章:

c - 将外部文件的输出存储在变量中

c - 如何确定结构数组的大小并分配内存 C 编程

c - 如何在特定目录中创建文件

c - 只读模式下的 fopen() 及其缓冲区

在 C 中使用 strtol() 将 char 数组转换为 int

javascript - 使用Prototype动态加载js文件?

c - 即使在程序中提供了正确的路径后,我也无法在 Visual C++ 中打开该文件

c - 读取一个日志文件并以指定的格式写入其他文件