gpt4 book ai didi

无法删除C中的文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:16 28 4
gpt4 key购买 nike

我正在使用代码块,我想使用 C 删除一个文件。该文件由两个函数使用,但不是同时使用。

这是使用该文件的第一个函数:

double FileRead()
{
double n,cl,cd,result;
FILE *fd;
char filename[] = "save.txt";
char buff[5024];


if ((fd = fopen(filename, "r")) != NULL)
{
fseek(fd, 0, SEEK_SET);
while(!feof(fd))
{
memset(buff, 0x00, 5024);
fscanf(fd, "%[^\n]\n", buff);
}
sscanf(buff, "%lf %lf %lf",&n,&cl,&cd);
printf("cl: %1.5f cd: %1.5f\n",cl,cd);


result = (cl/cd);
printf("The CL/CD ratio is : %1.5f\n",result);
}
else
result = 0;

fclose(fd);

return result;
}

这是第二个函数:

void evaluate(void)  /*evaluate the population */
{
int mem;
int i;
double x[NVARS+1];
char buffer[101] = "save.txt";

FILE *controlpoints;

double y[NVARS] = {1.00000,0.92544,0.82351,0.78301,0.74004,0.50199,0.40422,0.31056, /*fixed values on x axis */
0.18549,0.14954,0.11702,0.06331,0.02581,0.01334,0.00509,0.00000,0.00052,0.00555,0.03324,
0.11345,0.33088,0.43678,0.60146,0.70751,0.8043,0.92047,0.98713,1.00000};



for(mem = 0; mem < POPSIZE; mem++)
{
controlpoints = fopen("controlpoints2.txt","w");

for(i = 0; i < NVARS; i++)
{
x[i+1] = population[mem].gene[i];

fprintf(controlpoints,"%1.5f\n%1.5f\n",y[i],x[i+1]);

printf("The value of population[%d].gene[%d] is %f\n",mem,i,population[mem].gene[i]);

}

fclose(controlpoints);
rbspline();

XfoilCall();

population[mem].fitness = FileRead();

}

remove(buffer);
if(remove(buffer) == 0)
printf("File %s deleted.\n", buffer);
else
fprintf(stderr, "Error deleting the file %s.\n", buffer);
}

我一直收到消息“删除文件 save.txt 时出错”。你能检查一下并告诉我我做错了什么吗?

最佳答案

第二个函数中的代码包含:

remove(buffer);
if (remove(buffer) == 0)
printf("File %s deleted.\n", buffer);
else
fprintf(stderr, "Error deleting the file %s.\n", buffer);

您要删除该文件两次,第二次它不存在,因此您报告失败。

修复:删除未选中的 remove(buffer) 行。

关于无法删除C中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370883/

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