gpt4 book ai didi

无法删除文件名,也无法将我的临时文件重命名为database-aluno.txt

转载 作者:行者123 更新时间:2023-11-30 17:23:07 25 4
gpt4 key购买 nike

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

就在系统输入膳食时发生,每周每天只有 1 顿。到目前为止,没有任何问题。

系统检查学生的余额,如果学生有0.0余额或少于3余额(即每顿饭的费用),系统会提示或自动建议加载余额,并且大多数情况下都正确执行所有步骤.

事实证明,也许由于几个实现的周期,我几乎一切顺利,但在函数指令的末尾,指令 remove(filename);和重命名(“backup.c”,文件名);`

代码:

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循环中会出现问题吗?

最佳答案

为什么不检查 remove() 的返回值,并在出现错误时检查 errno

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

关于无法删除文件名,也无法将我的临时文件重命名为database-aluno.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com