[英]Why does Remove and Rename once working once not?
I want to delete a line from a file I use as a "database".我想从用作“数据库”的文件中删除一行。 I do this with copying everything expect that line to a new file, then deleting the old one and renaming the new one.
为此,我将期望该行的所有内容复制到一个新文件中,然后删除旧文件并重命名新文件。 I know this is not the best solution probably, but that's not the point now.
我知道这可能不是最好的解决方案,但这不是重点。 The problem is, that I do this with 2 files, and it doesn't work with the first one, but works perfectly with the second one.
问题是,我用 2 个文件执行此操作,它不适用于第一个文件,但可以完美地与第二个文件一起使用。 I have no idea what casusing the problem.
我不知道是什么导致了这个问题。 (The problem is that the first remove and rename does nothing, and returs with -1 which is the error code.)
(问题是第一次删除和重命名什么都不做,并且返回 -1 是错误代码。)
Here is the code:这是代码:
#define defaultBuilds "build.txt"
#define defaultAnswers "answers.txt"
FILE* fop = fopen(defaultAnswers, "r"); //forrasfile
FILE* fdp = fopen("temp_a.txt", "w"); //ideiglenes cel file
//bejegyzesek masolasa
char buff[buffSize];
int ind;
for(int i = 0; i < lines; i++)
{
fgets(buff, buffSize, fop);
if(i != sor) //kiveve a torlendo bejegyzest
fprintf(fdp, "%s", buff);
else if(i == sor)
{
//tolendo bejegyzeshez tartozo index meghatarozasa
int end, start;
for(end = 0; buff[end] != 0; end++);
for(start = end; buff[start] != ' '; start--);
char temp[50];
for(int j = ++start, k = 0; j < end; j++, k++)
temp[k] = buff[j];
sscanf(temp, "%d", &ind);
}
}
fclose(fop);
fclose(fdp);
remove(defaultAnswers); //regi torlese
rename("temp_a.txt", defaultAnswers); //uj atnevezese
fop = fopen(defaultBuilds, "r");
fdp = fopen("temp_b.txt", "w");
//szamitogepek masolasa
int temp;
for(int i = 0; i < lines; i++)
{
fscanf(fop, "%d\n", &temp);
if(temp != ind) //torlendo kihagyasa
fprintf(fdp, "%d\n", temp);
for(int j = 0; j < defaultAmountOfParts; j++)
{
fgets(buff, buffSize, fop);
if(temp != ind)
{
if(j == 0)
fprintf(fdp, "\t");
fprintf(fdp, "%s", buff);
}
}
}
fclose(fop);
fclose(fdp);
remove(defaultBuilds); //torles
rename("temp_b.txt", defaultBuilds); //atnevezes
Oh and here is the folder structure:哦,这是文件夹结构:
I can probably solve the problem in an ugly way but i would be really happy if i could use this method, it is much more simple.我可能会以一种丑陋的方式解决问题,但如果我能使用这种方法,我会非常高兴,它更简单。
EDIT: I found that remove() has the error of: Permission denied , which i don't know why is happening.编辑:我发现 remove() 有错误: Permission denied ,我不知道为什么会发生。 I checked, fclose closes the file succesfully.
我检查了, fclose 成功关闭了文件。
I want to delete a line from a file I use as a "database".我想从用作“数据库”的文件中删除一行。 I do this with copying everything expect that line to a new file, then deleting the old one and renaming the new one.
为此,我将期望该行的所有内容复制到一个新文件中,然后删除旧文件并重命名新文件。 I know this is not the best solution probably, but that's not the point now.
我知道这可能不是最好的解决方案,但这不是重点。 The problem is, that I do this with 2 files, and it doesn't work with the first one, but works perfectly with the second one.
问题是,我用 2 个文件执行此操作,它不适用于第一个文件,但可以完美地与第二个文件一起使用。 I have no idea what casusing the problem.
我不知道是什么导致了这个问题。 (The problem is that the first remove and rename does nothing, and returs with -1 which is the error code.)
(问题是第一次删除和重命名什么都不做,并且返回 -1 是错误代码。)
Here is the code:这是代码:
#define defaultBuilds "build.txt"
#define defaultAnswers "answers.txt"
FILE* fop = fopen(defaultAnswers, "r"); //forrasfile
FILE* fdp = fopen("temp_a.txt", "w"); //ideiglenes cel file
//bejegyzesek masolasa
char buff[buffSize];
int ind;
for(int i = 0; i < lines; i++)
{
fgets(buff, buffSize, fop);
if(i != sor) //kiveve a torlendo bejegyzest
fprintf(fdp, "%s", buff);
else if(i == sor)
{
//tolendo bejegyzeshez tartozo index meghatarozasa
int end, start;
for(end = 0; buff[end] != 0; end++);
for(start = end; buff[start] != ' '; start--);
char temp[50];
for(int j = ++start, k = 0; j < end; j++, k++)
temp[k] = buff[j];
sscanf(temp, "%d", &ind);
}
}
fclose(fop);
fclose(fdp);
remove(defaultAnswers); //regi torlese
rename("temp_a.txt", defaultAnswers); //uj atnevezese
fop = fopen(defaultBuilds, "r");
fdp = fopen("temp_b.txt", "w");
//szamitogepek masolasa
int temp;
for(int i = 0; i < lines; i++)
{
fscanf(fop, "%d\n", &temp);
if(temp != ind) //torlendo kihagyasa
fprintf(fdp, "%d\n", temp);
for(int j = 0; j < defaultAmountOfParts; j++)
{
fgets(buff, buffSize, fop);
if(temp != ind)
{
if(j == 0)
fprintf(fdp, "\t");
fprintf(fdp, "%s", buff);
}
}
}
fclose(fop);
fclose(fdp);
remove(defaultBuilds); //torles
rename("temp_b.txt", defaultBuilds); //atnevezes
Oh and here is the folder structure:哦,这是文件夹结构:
I can probably solve the problem in an ugly way but i would be really happy if i could use this method, it is much more simple.我可能会以一种丑陋的方式解决问题,但如果我能使用这种方法,我会非常高兴,它更简单。
EDIT: I found that remove() has the error of: Permission denied , which i don't know why is happening.编辑:我发现 remove() 有错误: Permission denied ,我不知道为什么会发生。 I checked, fclose closes the file succesfully.
我检查了, fclose 成功关闭了文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.