C中的remove()返回;没有这样的文件或目录;
我已经面临这个问题大约两周了,通过我所做的所有搜索(这对你来说是非常广泛的),我还没有找到解决方案。以下是我的功能:C中的remove()返回;没有这样的文件或目录;,c,stat,C,Stat,我已经面临这个问题大约两周了,通过我所做的所有搜索(这对你来说是非常广泛的),我还没有找到解决方案。以下是我的功能: void delete( int sock, char *fileName ) { unsigned int tid = (unsigned int)pthread_self(); if ( fileExists( fileName ) != 0 ) { char msg[20];
void delete( int sock, char *fileName ) {
unsigned int tid = (unsigned int)pthread_self();
if ( fileExists( fileName ) != 0 ) {
char msg[20];
strcpy( msg, "ERROR NO SUCH FILE\n" );
send( sock, msg, strlen( msg ), 0 );
printf( "[thread %u] Sent: %s", tid, msg );
}
if ( remove( fileName) != 0 ) {
send( sock, FAILURE, strlen( FAILURE ), 0 );
printf( "[thread %u] Sent: %s", tid, FAILURE );
} else {
send( sock, SUCCESS, strlen( SUCCESS ), 0 );
printf( "[thread %u] Sent: %s", tid, SUCCESS );
}
}
此函数用于我正在用C编写的FTP服务器的一部分,其中客户端可以输入的命令之一是从服务器存储器(在我的硬盘上标记为“.storage/”的文件夹)中删除特定文件。fileExists()函数的代码如下所示:
int fileExists( char *fileName ) {
struct stat buf;
return ( stat( fileName, &buf ) == 0 );
}
当我运行代码并告诉服务器删除一个文件(我知道事实上存在该文件)时,remove()
函数返回错误“没有这样的文件或目录”(在我的旧版本代码中,我使用peror()
来找出错误是什么)。但是,fileExists()
函数返回0,表示文件确实存在
有没有人遇到过这样的问题:一个stat()
报告一个文件存在,但remove()
没有?我真的在竭尽全力想弄明白这一点,但我还没有找到解决办法。任何帮助都将不胜感激
编辑:感谢您的回复。原来remove()
似乎不喜欢我发送的字符串上的空终止符。但是,我同意我的fileExists()
函数有点误导。如果文件不存在(或者有另一个错误),我将更改它以使其更简单(如果只为我自己)。stat()
返回-1
。在这种情况下,stat(fileName,&buf)=0
是0
。因此,你应该更换
if ( fileExists( fileName ) != 0 ) {
与
检查是否存在错误条件
如果您“知道文件存在”,那么问题可能是fileName
是一个相对路径,它是相对于当前工作目录解决的
这一过程的关键
请注意,实际上根本不需要单独的fileExists()
函数(和
如果在fileExists()
以及remove()
调用)。最好尝试删除该文件,然后选中errno
如果失败:
void delete( int sock, char *fileName ) {
unsigned int tid = (unsigned int)pthread_self();
if ( remove( fileName) != 0 ) {
if (errno == ENOENT) {
// file does not exist
char msg[20];
strcpy( msg, "ERROR NO SUCH FILE\n" );
send( sock, msg, strlen( msg ), 0 );
printf( "[thread %u] Sent: %s", tid, msg );
} else {
// some other error ...
send( sock, FAILURE, strlen( FAILURE ), 0 );
printf( "[thread %u] Sent: %s", tid, FAILURE );
}
} else {
send( sock, SUCCESS, strlen( SUCCESS ), 0 );
printf( "[thread %u] Sent: %s", tid, SUCCESS );
}
}
stat()
如果文件不存在(或存在其他错误),则返回-1
。在这种情况下,stat(fileName,&buf)=0
是0
。因此,你应该更换
if ( fileExists( fileName ) != 0 ) {
与
检查是否存在错误条件
如果您“知道文件存在”,那么问题可能是fileName
是一个相对路径,它是相对于当前工作目录解决的
这一过程的关键
请注意,实际上根本不需要单独的fileExists()
函数(和
如果在fileExists()
以及remove()
调用)。最好尝试删除该文件,然后选中errno
如果失败:
void delete( int sock, char *fileName ) {
unsigned int tid = (unsigned int)pthread_self();
if ( remove( fileName) != 0 ) {
if (errno == ENOENT) {
// file does not exist
char msg[20];
strcpy( msg, "ERROR NO SUCH FILE\n" );
send( sock, msg, strlen( msg ), 0 );
printf( "[thread %u] Sent: %s", tid, msg );
} else {
// some other error ...
send( sock, FAILURE, strlen( FAILURE ), 0 );
printf( "[thread %u] Sent: %s", tid, FAILURE );
}
} else {
send( sock, SUCCESS, strlen( SUCCESS ), 0 );
printf( "[thread %u] Sent: %s", tid, SUCCESS );
}
}
stat()
如果文件不存在(或存在其他错误),则返回-1
。在这种情况下,stat(fileName,&buf)=0
是0
。因此,你应该更换
if ( fileExists( fileName ) != 0 ) {
与
检查是否存在错误条件
如果您“知道文件存在”,那么问题可能是fileName
是一个相对路径,它是相对于当前工作目录解决的
这一过程的关键
请注意,实际上根本不需要单独的fileExists()
函数(和
如果在fileExists()
以及remove()
调用)。最好尝试删除该文件,然后选中errno
如果失败:
void delete( int sock, char *fileName ) {
unsigned int tid = (unsigned int)pthread_self();
if ( remove( fileName) != 0 ) {
if (errno == ENOENT) {
// file does not exist
char msg[20];
strcpy( msg, "ERROR NO SUCH FILE\n" );
send( sock, msg, strlen( msg ), 0 );
printf( "[thread %u] Sent: %s", tid, msg );
} else {
// some other error ...
send( sock, FAILURE, strlen( FAILURE ), 0 );
printf( "[thread %u] Sent: %s", tid, FAILURE );
}
} else {
send( sock, SUCCESS, strlen( SUCCESS ), 0 );
printf( "[thread %u] Sent: %s", tid, SUCCESS );
}
}
stat()
如果文件不存在(或存在其他错误),则返回-1
。在这种情况下,stat(fileName,&buf)=0
是0
。因此,你应该更换
if ( fileExists( fileName ) != 0 ) {
与
检查是否存在错误条件
如果您“知道文件存在”,那么问题可能是fileName
是一个相对路径,它是相对于当前工作目录解决的
这一过程的关键
请注意,实际上根本不需要单独的fileExists()
函数(和
如果在fileExists()
以及remove()
调用)。最好尝试删除该文件,然后选中errno
如果失败:
void delete( int sock, char *fileName ) {
unsigned int tid = (unsigned int)pthread_self();
if ( remove( fileName) != 0 ) {
if (errno == ENOENT) {
// file does not exist
char msg[20];
strcpy( msg, "ERROR NO SUCH FILE\n" );
send( sock, msg, strlen( msg ), 0 );
printf( "[thread %u] Sent: %s", tid, msg );
} else {
// some other error ...
send( sock, FAILURE, strlen( FAILURE ), 0 );
printf( "[thread %u] Sent: %s", tid, FAILURE );
}
} else {
send( sock, SUCCESS, strlen( SUCCESS ), 0 );
printf( "[thread %u] Sent: %s", tid, SUCCESS );
}
}
这是
stat
的手册
成功时,返回零。在出现错误时,返回-1,并正确设置errno
您正在检查函数中是否返回零
int fileExists( char *fileName ) {
struct stat buf;
return ( stat( fileName, &buf ) == 0 );
}
假设存在一个文件。那么您的返回条件将返回true
(非空值),因为stat
将返回0
,并且满足该条件。。
但这张支票显然是真的
如果(文件存在(文件名)!=0){
而且你的女朋友打印的错误文件不存在`
把你的情况改为
如果(文件存在(文件名)=0){
这是
stat
的手册
成功时,返回零。出现错误时,返回-1,并正确设置errno
您正在检查函数中是否返回零
int fileExists( char *fileName ) {
struct stat buf;
return ( stat( fileName, &buf ) == 0 );
}
假设存在一个文件。那么您的返回条件将返回true
(非空值),因为stat
将返回0
,并且满足该条件。。
但这张支票显然是真的
如果(文件存在(文件名)!=0){
而且你的女朋友打印的错误文件不存在`
把你的情况改为
如果(文件存在(文件名)=0){
这是
stat
的手册
成功时,返回零。出现错误时,返回-1,并正确设置errno
<