重命名功能在C中不起作用

时间:2014-12-02 15:28:57

标签: c

我创建了一个更新文件值然后重命名的函数。 我的代码:

void UpdateBooks()
{
system("CLS");
char *update[5] = {malloc(30),malloc(30),malloc(30),malloc(30),malloc(30)};
int coop = 0;
struct Books book;
char oldfn[] = "d:/booksdata.txt";
char newfn[] = "d:/booksdata_temp.txt";
int deletecheck = 0;
char *myisbnumber = malloc(4);
char line[256];
char * pch;
int countcheck = 0;
allocatebooks(&book);
fp = fopen(oldfn,"r");
fpa = fopen(newfn,"w+");
if (fp == NULL || fpa == NULL)
{
    printf("Error opening file!\n");
    exit(1);
}
printf("Enter ISBN Number Of The Book You Want To Update:\n");
scanf("%10s",myisbnumber);

while (fgets(line,sizeof line,fp) != NULL)
{
    pch = strtok (line,",");
    while (pch != NULL)
    {
    if(countcheck == 0)
        book.name = pch;
    else if(countcheck == 1)
        book.author = pch;
    else if(countcheck == 2)
        book.bookserialnumber = pch;
    else if(countcheck == 3)
        book.isbnnumber = pch;
    else if(countcheck == 4)
        book.edition = pch;
    else if(countcheck == 5)
        book.is_reserved = pch;
    pch = strtok (NULL, ",");
    countcheck++;
    }
    if(strcmp(myisbnumber,book.isbnnumber) != 0)
    {
        fprintf(fpa,"%s,%s,%s,%s,%s,%s,\n",book.name,book.author,book.bookserialnumber,book.isbnnumber,book.edition,book.is_reserved);
    }
    else
    {
        deletecheck++;
        RedoUpdate:
        system("CLS");
        coop = 0;
        printf("What do you want to Update:\n");
        printf("1.Name\t  %s\n2.Author\t  %s\n3.Serial Number\t  %s\n4.Edition\t  %s\n5.Is_Reserved\t  %s\n6.Nothing to Update\n",book.name,book.author,book.bookserialnumber,book.edition,book.is_reserved);
        scanf("%d",&coop);
        if(coop > 0 && coop <= 5)
        {
            printf("Enter New Value\n");
            scanf("%s",update[coop]);
            if(coop == 1)
                book.name = update[coop];
            else if(coop == 2)
                book.author = update[coop];
            else if(coop == 3)
                book.bookserialnumber = update[coop];
            else if(coop == 4)
                book.edition = update[coop];
            else if(coop == 5)
                book.is_reserved = update[coop];
            goto RedoUpdate;
        }
        fprintf(fpa,"%s,%s,%s,%s,%s,%s,\n",book.name,book.author,book.bookserialnumber,book.isbnnumber,book.edition,book.is_reserved);
        printf("Updated!\n");
    }
    countcheck = 0;
}
if(deletecheck == 0)
    goto END;
if(fclose(fp) != 0)
    perror("File Not Closed FP\n");
if(fclose(fpa) != 0)
    perror("File Not Closed FPA\n");
if(remove(oldfn) == -1)
{
    perror("Remove Error");
    exit(1);
}
if(rename(newfn,oldfn) == -1)
{
    perror("Rename Error");
    exit(1);
}
printf("Completed!\n");
END:
getch();
}

整个代码正常工作。我也用fclose关闭文件,但是当我尝试重命名它时,我得到Permission Denied错误。 当使用重命名(newfn,oldfn)时,我得到:权限被拒绝错误

2 个答案:

答案 0 :(得分:0)

remove - rename代码看起来不错。代码还有一些其他问题,最值得注意的是Andrew Medico指出的myisbnumber缓冲区溢出。你应该解决这个问题!

还有内存泄漏,因为你永远不会释放任何内存malloc - 你应该考虑将它们分配为普通数组。堆栈应该能够处理额外的154个字节(如果您将myisbnumber更改为char myisbnumber[11],则为161个。)

但是,我不认为其中任何一个导致您的问题(尽管您永远无法确定第一个,因为它会导致未定义的行为)。我认为问题是由于文件权限(从目录继承)。

该路径表明您在Windows中工作。 您应该检查新创建的文件的权限。该目录可能具有设置,因此您不能重命名该文件。如果是这种情况,您可以通过更改目录权限或使用您具有完全访问权限的目录来解决问题。

答案 1 :(得分:0)

来自ISO / IEC9899:

  

7.19.4.2重命名功能

     

概要

     

1 #include <stdio.h>

     

int rename(const char *old, const char *new);

正如你可以从原型中看到的那样:你在错误的地方使用旧的和新的论点。