2009-12-20 91 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()。否則,文件可能被鎖定。或者它可能還沒有創建,所以在刪除之前添加一個延遲。