gpt4 book ai didi

c - C中删除一个文件

转载 作者:太空狗 更新时间:2023-10-29 17:02:48 25 4
gpt4 key购买 nike

我编写了一个操作文件的程序,有时我会删除一个文件。于是在C语言的stdio头文件中查找并找到了remove()函数。

问题是有时有效有时无效。有时文件被删除,其他 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;
}

下面是使用 remove 的函数:

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。

最佳答案

您尚未指定所使用的平台,但在 Windows 上,当您尝试删除该文件时,该文件不应打开。 (在 Unix 风格的系统上,事情的工作方式有点不同,即使文件是打开的,也几乎总是可以删除。)

关于c - C中删除一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com