rename()在代码块中编译的C程序中不起作用 #包括 #包括 #包括 #包括 #包括 文件*fp,*fp1; char p_id[7],buf[7],族[15],cl[15],nm[20]; int flag3=0; int main() {//无符号字符t='y'; clrsc(); fp=fopen(“Product.txt”、“r+”); fp1=fopen(“产品id.txt”、“w+”); 放置(“\t\t\t删除产品详细信息”); printf(“\n用户:”); 如果(flag3==0) { printf(“\n\n\n\t\t\t输入产品ID:”); scanf(“%s”,p_id); } 其他的 { printf(“\n\n\n\t\t\t重新输入产品ID:”); scanf(“%s”,p_id); } fclose(fp); fp=fopen(“Product.txt”、“r”); fscanf(fp、%s%s%s)、buf、系列、cl、nm); 而(!feof(fp)) { if(strcmp(p_id,buf)==0) { printf(“\n\n\n\t\t\t产品已删除”); } 其他的 { fprintf(fp1,“%s%s%s\n”,buf,系列,cl,nm); } fscanf(fp、%s%s%s)、buf、系列、cl、nm); } //printf(“\n\n是否要删除更多产品?(Y/n)”; //scanf(“%s”、&t); fclose(fp1); fclose(fp); 删除(“Product.txt”); int status=重命名(“product_id.txt”、“product.txt”); 如果(状态==0) { printf(“工作”); } 其他的 { printf(“不工作”); } getch(); 返回0; }
在这段代码中,我试图通过将更新的数据存储到另一个文件中,然后删除原始文件并重命名第二个文件来更新文件中的特定行。更新工作正常,但rename()不工作 “不工作”是指rename()在代码块中编译的C程序中不起作用 #包括 #包括 #包括 #包括 #包括 文件*fp,*fp1; char p_id[7],buf[7],族[15],cl[15],nm[20]; int flag3=0; int main() {//无符号字符t='y'; clrsc(); fp=fopen(“Product.txt”、“r+”); fp1=fopen(“产品id.txt”、“w+”); 放置(“\t\t\t删除产品详细信息”); printf(“\n用户:”); 如果(flag3==0) { printf(“\n\n\n\t\t\t输入产品ID:”); scanf(“%s”,p_id); } 其他的 { printf(“\n\n\n\t\t\t重新输入产品ID:”); scanf(“%s”,p_id); } fclose(fp); fp=fopen(“Product.txt”、“r”); fscanf(fp、%s%s%s)、buf、系列、cl、nm); 而(!feof(fp)) { if(strcmp(p_id,buf)==0) { printf(“\n\n\n\t\t\t产品已删除”); } 其他的 { fprintf(fp1,“%s%s%s\n”,buf,系列,cl,nm); } fscanf(fp、%s%s%s)、buf、系列、cl、nm); } //printf(“\n\n是否要删除更多产品?(Y/n)”; //scanf(“%s”、&t); fclose(fp1); fclose(fp); 删除(“Product.txt”); int status=重命名(“product_id.txt”、“product.txt”); 如果(状态==0) { printf(“工作”); } 其他的 { printf(“不工作”); } getch(); 返回0; },c,C,在这段代码中,我试图通过将更新的数据存储到另一个文件中,然后删除原始文件并重命名第二个文件来更新文件中的特定行。更新工作正常,但rename()不工作 “不工作”是指rename()根据程序打印“不工作”判断返回非零值吗?在这种情况下,检查和/或打印实际返回值可能会说明问题,或者通过调用peror()@ameyCU打印相应的诊断可能会更好,因为它无法正常工作。它似乎工作正常。您最后的fscanf无法成功。然后,在上一次迭代中,与buf@ameyCU相比,您正在调用一个未定义的行为,这意味着rem
rename()
根据程序打印“不工作”判断返回非零值吗?在这种情况下,检查和/或打印实际返回值可能会说明问题,或者通过调用peror()
@ameyCU打印相应的诊断可能会更好,因为它无法正常工作。它似乎工作正常。您最后的fscanf
无法成功。然后,在上一次迭代中,与buf
@ameyCU相比,您正在调用一个未定义的行为,这意味着remove()
失败。在remove()失败时执行perror。@user3386109这是明智的建议,最好使用,而(4!=fscanf(…)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <string.h>
FILE *fp,*fp1;
char p_id[7],buf[7],family[15],cl[15],nm[20];
int flag3=0;
int main()
{ // unsigned char t='y';
clrscr();
fp=fopen("Product.txt","r+");
fp1=fopen("product_id.txt","w+");
puts("\t\t\t Remove Product Details");
printf("\n User :");
if(flag3==0)
{
printf("\n\n\n \t\t\t Enter Product ID :");
scanf("%s",p_id);
}
else
{
printf("\n\n\n \t\t\t Re-Enter Product ID :");
scanf("%s",p_id);
}
fclose(fp);
fp=fopen("Product.txt","r");
fscanf(fp,"%s %s %s %s",buf,family,cl,nm);
while(!feof(fp))
{
if(strcmp(p_id,buf)==0)
{
printf("\n\n\n \t\t\t product removed");
}
else
{
fprintf(fp1," %s %s %s %s\n",buf,family,cl,nm);
}
fscanf(fp,"%s %s %s %s",buf,family,cl,nm);
}
//printf("\n \n Do you want to remove more product ?(Y/N)");
//scanf("%s",&t);
fclose(fp1);
fclose(fp);
remove("Product.txt");
int status= rename("product_id.txt","Product.txt");
if(status==0)
{
printf("working");
}
else
{
printf("not working");
}
getch();
return 0;
}