如何从文件中获取数据并将结果放入C中的新文件中?

问题描述 投票:0回答:1

我正在开发一个程序,该程序从文件中获取有关书籍的数据(在结构中)。然后它会搜索书籍销售最便宜的城市。一般来说,这个程序会获取一个文件(input_data.txt),例如:


2
Book Title 1
10
200
Author 1
2021
Publisher 1
City 1
Book Title 2
15
150
Author 2
2022
Publisher 2
City 2

结果,它输出 output_data.txt 文件,其中包含拥有最便宜书籍的城市:


City 1

C: 上有我的代码

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

struct izdatel {
    int god;
    char naz[100];
    char gor[100];
};

struct Book {
    char title[100];
    int price;
    int numPages;
    char author[100];
    struct izdatel publisherInfo;
};

void check_number(const char name[], int *s, FILE *input_data) {
    int ret_code;
    do {
        //printf("Input %s:\n", name);
        ret_code = fscanf(input_data, "%d", s);
        while (getchar() != '\n');  // clear the buffer (as not using fgets)
    } while (ret_code != 1 || *s <= 0);
}

void check_year(const char name[], int *s, FILE *input_data) {
    int ret_code;
    do {
        //printf("Input %s (between 1458 and 2024):\n", name);
        ret_code = fscanf(input_data, "%d", s);
        while (getchar() != '\n');  
    } while (ret_code != 1 || *s < 1458 || *s > 2024);
}

void check_string(const char name[], char s[], FILE *input_data) {
    int invalid_input;
    do {
        invalid_input = 0;
        //printf("Input %s:\n", name);
        fscanf(input_data, "%s", s);
        while (getchar() != '\n'); 

        for (int i = 0; i < strlen(s); i++) {
            if (!isalpha(s[i]) && !isspace(s[i])) {
                invalid_input = 1;
                printf("Invalid input. Please enter a string without numbers or special characters.\n");
                break;
            }
        }

    } while (invalid_input || strlen(s) == 0);
}

void input(struct Book *books, const int numStructs, FILE *input_data) {
    fscanf(input_data, "%d", &numStructs);
    while (getchar() != '\n');
    for (int i = 0; i < numStructs; i++) {
        //printf("Book #%d\n", i + 1);
        check_string("Title", books[i].title, input_data);
        check_number("Price", &books[i].price, input_data);
        check_number("Number of pages", &books[i].numPages, input_data);
        check_string("Author", books[i].author, input_data);

        //printf("Publishing company:\n");
        check_year("Year of publication", &books[i].publisherInfo.god, input_data);
        check_string("Publisher name", books[i].publisherInfo.naz, input_data);
        check_string("Publishing city", books[i].publisherInfo.gor, input_data);
    }
}


void solution(struct Book *books, const int numStructs, FILE *output_data){
    float minPrice = books[0].price;
    for (int i = 0; i < numStructs; i++) {
        if (books[i].price < minPrice) {
            minPrice = books[i].price;
        }
    }

    fprintf(output_data, "\nCities where the cheapest books are published:\n");
    for (int i = 0; i < numStructs; i++) {
        if (books[i].price == minPrice) {
            fprintf(output_data, "%s\n", books[i].publisherInfo.gor);
        }
    }
}

int main() {
    int numStructs;
    FILE *inputFile;

    struct Book books[numStructs];
    // input file
    char *inputFilename = "input_data.txt";
    char *outputFilename = "output_data.txt";

    inputFile = fopen(inputFilename, "r");
    FILE *outputFile = fopen(outputFilename, "w");
    
    if (inputFile == NULL || outputFile == NULL) {
        printf("Error opening files.\n");
        return 1;
    }

    if (inputFile) {
        input(books, numStructs, inputFile);
        solution(books, numStructs, outputFile);
    } else {
        printf("Failed to open the input file.\n");
    }
    
    fclose(inputFile);
    fclose(outputFile);

    return 0;
}

我绞尽脑汁解决这个问题。请帮我。我将非常感激!

c file input output structure
1个回答
0
投票

简而言之,回顾一切。

要做长,首先:

int         numStructs;        // is const on all fonction call why not here to?
struct Book books[numStructs]; // numStructs not define and use

解析是这样的程序中唯一的问题。 您需要在文件中查找模式才能了解您拥有哪些信息。 你也想避免这样的事情:

char title[100]; 

您永远不知道输入文件中的字符串的大小。 拿一张纸思考一下。 这里没有人会给您现成的代码。

© www.soinside.com 2019 - 2024. All rights reserved.