为什么我不能用fopen()打开文件?

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

我正在尝试从“ txt”文件读取结构数组,该文件是您自己输入的。我在使用fopen()打开文件时遇到问题。该文件位于项目目录中,但仍然失败。QTCreator是主要的IDE。这是我的代码:首先检查void filename_check(char *name, FILE *fl)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define lmax 50
using namespace std;

struct model
{
    char series[lmax];
    int number;
};
struct realease_date
{
    int year;
    char month[15];
};

struct car
{
    char firm[lmax], color[lmax];
    float price, engine_volume;
    model standart;
    realease_date date;
};

int valid_value(FILE *fl)
{
    int num;
    if (fscanf(fl,"%d",&num)!=1)
    {
        printf("Error in reading number of cars.");
        exit(1);
    }
    else
    {
        if (num<1 || num>lmax)
        {
            printf("Incorrect number.\n");
            exit(1);
        }

    }
    return num;
}

void input_array(int n, car *cars, FILE *fl)
{
    for (int i=0;i<n;i++, cars++)
    {
        if (fgets(cars->firm,sizeof(cars->firm), fl)==NULL)
        {
            printf("Error in reading company.");
            exit(1);
        }
        if (fgets(cars->color,sizeof(cars->color), fl)==NULL)
        {
            printf("Error in reading color.");
            exit(1);
        }
        if (fscanf(fl,"%d",&(cars->price))!=1)
        {
            printf("Error in reading price.");
            exit(1);
        }
        if (fscanf(fl,"%f",&(cars->engine_volume))!=1)
        {
            printf("Error in reading engine_volume.");
            exit(1);
        }
        if (fscanf(fl,"%d",&(cars->date.year))!=1)
        {
            printf("Error in reading year of release date.");
            exit(1);
        }
        if (fscanf(fl,"%d",&(cars->date.month))!=1)
        {
            printf("Error in reading month of release date.");
            exit(1);
        }
        if (fscanf(fl,"%d",&(cars->standart.number))!=1)
        {
            printf("Error in reading number of model.");
            exit(1);
        }
        if (fgets(cars->standart.series,sizeof(cars->standart.series), fl)==NULL)
        {
            printf("Error in reading series of model.");
            exit(1);
        }
        fscanf(fl,"\n");
    }
}
void input_limit(float *k,FILE *fl)
{
    if (fscanf(fl,"%f",k)!=1)
    {
        printf("Error in reading limit.");
        exit(1);
    }
}
int search(car *cars, char found_firms[][lmax], int n,int k)
{
    int nf=0, j;
    car *first=cars;
    for (int i=0;i<n;i++, cars++)
    {
        if (cars->engine_volume>=k)
        {
            for (j=0;j<nf && (strcmp(cars->firm,found_firms[j]));j++);
            if (j==nf)
            {
                bool flag=true;
                car *machines=first;
                for (int s=0;s<n && flag;s++, machines++)
                {
                    if (!strcmp(machines->firm,cars->firm) && machines->engine_volume<k) flag=false;
                }
                if (flag) strcpy(found_firms[nf++],cars->firm);
            }
        }
    }
    return nf;
}

void output(char found_firms[][lmax], int nf, FILE *fl)
{
    for (int i=0;i<nf;i++)
    {
        fputs(found_firms[i], fl);
        fputc('\n',fl);
    }
}
// open file function
void filename_check(char *name, FILE *fl)
{
    do
    {
        fscanf(stdin,"%s",name);
        fl=fopen(name,"r+");
        if (!fl)
        {
            puts("File not found. Try again:");

        }
    } while (!fl);
}

int main()
{
    setlocale(LC_ALL,".1251");
    car cars[lmax];
    FILE *finput, *foutput;
    char found_firms[lmax][lmax], file_input_name[lmax], file_output_name[lmax];
    int n, nf;
    float k;
    puts("Enter name of input file:");
    filename_check(file_input_name,finput);
    n = valid_value(finput);
    input_array(n,cars,finput);
    input_limit(&k,finput);
    fclose(finput);
    puts("Enter name of output file:");
    filename_check(file_output_name,foutput);
    nf = search(cars, found_firms,n,k);
    if (nf>0) output(found_firms,nf,finput);
    else printf("Firms, producing only cars with volume %.1f and higher are not found", k);
    fclose(foutput);
    return 0;
}

谢谢

c fopen
1个回答
0
投票

由于C使用传递值,所以无法通过在finput中更改main来更改fl中的filename_check

f1在函数中是finput以外的另一个变量。无论您在功能中更改f1多少次,finput中的main仍保持相同的值。

您将需要以下内容:

void filename_check(char *name, FILE **fl)
                                     ^^
                                    Notice: pointer-to-pointer

在您分配的功能内,例如:

*f1 = ...

然后从main中您致电类似:

filename_check(..., &finput)
© www.soinside.com 2019 - 2024. All rights reserved.