【问题标题】:Can't delete the filename and can't rename my temporary file to database-aluno.txt无法删除文件名,也无法将我的临时文件重命名为 database-aluno.txt
【发布时间】:2015-02-26 14:06:16
【问题描述】:

我正在尝试为我的算法和数据结构主题开发解决方案。我有一个函数可以让学生比较我数据库中存在的数字读取的数字(对应于行数)。

发生在系统输入餐点的时候,每周的每一天只有 1 餐。到目前为止,没有任何问题。

系统检查学生的余额,如果学生余额为0.0或小于3(即每餐的费用),系统会提示或自动推荐加载余额,并且大部分步骤都正确.

事实证明,可能是由于几个实现的周期,我几乎一切都顺利,但在功能指令的末尾,指令remove(filename); andrename("backup.c",filename);`

代码:

void EncomendaRefeicoes()
{
    FILE *myFile;

    FILE *fin, *fout;//ficheiro de entrada ficheiro de saida 
    const char *fn = "database-aluno.txt";
    myFile = fopen(filename, "r");
    int num; //numero
    rewind(myFile);
    printf("Insrira o numero de aluno");//Ask of number
    scanf("%d", &num);//read number
    printf("\n A verificar se o aluno existe!");//Check
    //procurar estudante
    int fnum = 0;//student number in file
    struct Aluno student;
    char fnom[50];//student file name
    char saldotostring[5];//
    double saldo;//saldo 
    //int i=0;

    while (!(feof(myFile)))//enquanto difrente de fim de ficheiro(feof)
    {
        fscanf(myFile, "%d %s %s %lf %d-%d\n", &Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, &Aluno[acstruct].saldo, &Aluno[acstruct].dia, &Aluno[acstruct].mes);
        //file scan as variaveis num,name,saldo,dia,mes

        if (num == Aluno[acstruct].num) //se num lido == num ficheiro
        {
            printf("\n");
            printf("\n");
            printf("Aluno %d encontrado", num);//Dizer que foi encontrado
            printf("\n");
            printf("Numero: %d", Aluno[acstruct].num);//imprime numero
            printf("\n");
            printf("Nome: %s", Aluno[acstruct].name);//imnprime nome
            printf("\n");
            printf("Subnome: %s", Aluno[acstruct].fname);
            printf("\n");
            //sprintf(saldotostring, "%.2f", saldo);
            printf("Saldo: %.2f", Aluno[acstruct].saldo);//imprime saldo
            printf("\n");
            printf("Custo de aquisicao: 3.00 euros \n");//diz qual o custo de aquisicao
            printf("\n");



            //Menu
            SelectOneFood(6, fp1);
            printf("\n");
            printf("Quer enconendar? \n");

            printf("1-Reservar \n");
            printf("\n");
            printf("2-Mandar-me para o menu principal \n");
            int opcaoescolhida;
            scanf("%d", &opcaoescolhida);
            double price = 3.00;

            if (opcaoescolhida == 1)
            {
                if (Aluno[acstruct].saldo == 0.0)
                {

                    char op = 'N';
                    printf("Nao tem saldo. Deseja carregar o plafound? (Primir N para nao) \n");
                    /*
                    if (scanf("%c", op) == 'N')
                    {
                    _tmain();
                    }
                    */
                    printf("Valor a inserir: ");
                    printf("\n");

                    scanf("%lf", &saldo);
                    if (saldo < 5.00)
                    {
                        printf("Minimo de carregamento obrigatorio e de 5 euros \n");
                        scanf("%lf", &saldo);

                    }
                    fin = fopen(fn, "r");
                    fout = fopen("sbackup.txt", "w");//an temporary file
                    rewind(fin);
                    rewind(fout);
                    while (!(feof(fin))){
                        //Lê 1 a 1
                        fscanf(fin, "%d %s %s %lf %d-%d\n", &Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, &Aluno[acstruct].saldo, &Aluno[acstruct].dia, &Aluno[acstruct].mes);

                        if (num == Aluno[acstruct].num)
                            fprintf(fout, "%d %s %s %lf %d-%d\n", Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, saldo, Aluno[acstruct].dia, Aluno[acstruct].mes);
                        else if (num != Aluno[acstruct].num)

                            fprintf(fout, "%d %s %s %lf %d-%d\n", Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, Aluno[acstruct].saldo, Aluno[acstruct].dia, Aluno[acstruct].mes);

                    }

                    remove(filename);
                    rename("sbackup.txt",filename);
                    printf("\n");
                    printf("Saldo alterado");
                    fclose(fout);
                    fclose(myFile);
                    fclose(fin);

                }

                /*
                if (Aluno[acstruct].saldo < price)
                printf("Voce nao tem saldo suficiente \n");
                */
                else if (Aluno[acstruct].saldo >= price)
                {
                    Aluno[acstruct].saldo = Aluno[acstruct].saldo - price;
                    //substiruir saldo no ficheiro de texto

                    printf("Refeicao adequerida com sucesso \n");
                    printf("Saldo Final %.2f euros", Aluno[acstruct].saldo);


                }
            }
            if (opcaoescolhida == 2)
                _tmain();

        }

    }
}

我该如何解决这个问题? if和while循环会出问题吗?

【问题讨论】:

标签: c file rename feof


【解决方案1】:

为什么不检查remove()的返回值,如果出错,检查errno

IMO,问题是,您正在调用 remove() 的文件指针已经是 fopen()ed 而不是 fclose()d。

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2018-10-11
    • 2011-03-18
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2013-08-31
    相关资源
    最近更新 更多