2009-12-20 88 views
7

我已经编写了一个程序来处理文件,我有时候会删除一个文件。因此,我搜索并在C中的stdio头文件中找到remove()函数。删除C中的文件

问题是,有时可以使用,有时不可以。有时文件被删除,其他perror()会显示一条消息,表示权限被拒绝,但我没有在文件上指定任何特殊权限。事实上,这个文件是由另一个函数创建的。我必须考虑哪些特殊条件?

下面是创建该文件的加载功能:

... 
int loadF (const char *filename, plist_t pl, int size, int createFlag) { 
    FILE *pFile = NULL; 
    pnode_t pn = NULL; 
    int fsize; 
    int total; 
    int i; 

    // Get file stream. 
    pFile = fopen (filename, "rb"); 
    if (!pFile) { // File does not exist. 
     if (createFlag) { 
      if (!createF (filename)) { 
       return 0; // fail 
      } 
     } else { // abort 
      perror ("loadF:fopen"); 
      return 0; 
     } 
    } 

    // Confirm that we have opened the file stream. 
    if (!pFile) { 
     pFile = fopen (filename, "rb"); 
     if (!pFile) { 
      perror ("loadF:fopen:"); 
      return 0; 
     } 
    } 

    // Check if list has not been initialized. 
    if (pl == NULL) { 
     fclose (pFile); 
     pFile = NULL; 
     return 0; // abort 
    } 

    // Get the size of the file. 
    fseek (pFile, 0, SEEK_END); 
    fsize = ftell (pFile); 
    rewind (pFile); 

    // Check if the file is empty. 
    if (!fsize) { 
     fclose (pFile); 
     pFile = NULL; 
     return 1; // No data to load, continue. 
    } 

    // Get the total number of structures in the file. 
    total = fsize/size; 

    // Allocate memory for a node to transfer data. 
    pn = (pnode_t) malloc (sizeof (node_t) * sizeof (char)); 
    if (!pn) { 
     fclose (pFile); 
     pFile = NULL; 
     perror ("loadF:malloc"); 
     return 0; 
    } 

    // Copy from file to list every structure. 
    for (i = 1; i <= total; i++) { 
     if (feof (pFile)) { 
      printf ("OUT!"); 
      break; 
     } 
     printf ("g"); 
     fread (pn->key, size, 1, pFile); 
     printf ("f\n"); 
     if (ferror (pFile)) { 
      fclose (pFile); 
      pFile = NULL; 
      perror ("loadF:fread"); 
      return 0; 
     } 
     addfirst (pl, pn->key); // Maybe we have to allocate memory with malloc every time? 
     // Debug with a for loop in the nodes of the list to see if data are OK. 
     printf ("cid = %d\n", pl->head->key->card.cid); 
     printf ("limit = %5.2f\n", pl->head->key->card.limit); 
     printf ("balance = %5.2f\n", pl->head->key->card.balance); 
    } 

    // Close the stream. 
    if (pFile) { 
     fclose (pFile); 
     pFile = NULL; 
    } 

    // Deallocate transfer memory. 
    if (pn) { 
     free (pn); 
    } 

    // Exit 
    return 1; 
} 

这里是使用删除功能:

int saveF (const char *filename, plist_t pl, int size) { 
    FILE *pFile = NULL;  // Pointer to the file structure. 
    pnode_t pn = NULL;  // Pointer to a node of a list. 


    // Delete the specified file - on success it returns 0. 
    if (remove (filename) == -1) { 
     perror ("saveF:remove"); 
     return 0; 
    } 

    // Re-create the file (but now is empty). 
    if (!createF (filename)) { 
     return 0; 
    } 

    // Get the file stream. 
    pFile = fopen (filename, "ab"); 
    if (!pFile) { 
     perror ("saveF:fopen"); 
     return 0; 
    } 

    // Check if list is not empty. 
    if (isEmpty (pl)) { 
     fclose (pFile); 
     pFile = NULL; 
     return 0;   // Abort 
    } 

    // Traverse the list nodes and save the entity that the key points to. 
    for (pn = pl->head; pn != NULL; pn = pn->next) { 
     fwrite ((pccms_t)(pn->key), size, 1, pFile); 
     if (ferror (pFile)) { 
      fclose (pFile); 
      pFile = NULL; 
      perror ("saveF:fwrite"); 
      return 0; 
     } 
    } 

    // Close the stream. 
    if (pFile) { 
     fclose (pFile); 
     pFile = NULL; 
    } 

    // Exit 
    return 1; 
} 

我使用Windows XP。

+0

修正了这个问题,一个函数将pfile指针留给有时打开的文件。感谢您的回答。 – Ponty 2009-12-20 11:08:19

+0

欢迎来到Stack Overflow!如果您的问题得到了很好的回答,请点击答案左侧的复选框大纲以“接受”最有帮助的答案。这表明你已经收到了适合你的答案。这样做有助于提高您在网站上的声誉。 – 2009-12-20 23:00:49

回答

6

您尚未指定您正在使用的平台,但在Windows上,当您尝试删除该文件时,该文件不应该打开。 (守得云开有一点不同的Unix类系统和删除几乎总是可能的,即使文件被打开。)

1

有3个原因,在我看来:

  1. 您没有权限删除文件。
  2. 该文件正在被另一个功能使用。
  3. 您必须在该文件所在的目录中写入保留。
2

无法删除文件的情况也取决于该文件被另一个进程锁定。在你的情况下,它可能是你的函数创建文件。这也取决于你使用的平台。如果是Windows,请尝试使用Unlocker,然后重试删除文件。

-1

我理解你: 你在第5行创建一个文件,然后在第7行删除它。

如果是: 尝试在这些行之间设置一个小暂停,100 ms应该没问题。 或者:该文件仍处于打开状态。关闭它,然后尝试删除它。

+0

如何在c中延迟?有没有delay()函数? – Ponty 2009-12-20 10:51:16

0

先做一个fclose()。否则,文件可能被锁定。或者它可能还没有创建,所以在删除之前添加一个延迟。