为什么remove和rename没起作用?求指教

鬼才冷面宝贝 2014-03-12 12:05:56
#include"stdio.h"
#include"string.h"
#include"stdlib.h"

struct student
{
char num[11],name[9];
int normal, exam;


};


int judge_num_exist(char *filename , char *num)
{
struct student x;
FILE *fp;
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("文件%s不存在!",filename);
exit(0);

}
fscanf(fp,"%s%s%d%d",x.num,x.name,&(x.normal),&(x.exam));

while(!feof(fp))
{
if(strcmp(x.num,num)==0) break;
fscanf(fp,"%s%s%d%d",x.num,x.name,&(x.normal),&(x.exam));
}
if(feof(fp))
return 0;
else
return 1;


}

void delete_record(char *filename ,char *num)
{
FILE *fp1, *fp2;
struct student y;
if(!judge_num_exist(filename,num))
printf("学号%s在文件中不存在!",num);
else
{
fp1=fopen(filename,"r");
fp2=fopen("temp.txt","w");
fscanf(fp1,"%s%s%d%d",y.num,y.name,&(y.normal),&(y.exam));
while(!feof(fp1))
{
if(strcmp(y.num, num)!=0)
fprintf(fp2,"%-11s%-9s%-4d%-4d\n",y.num,y.name,y.normal,y.exam);
fscanf(fp1,"%s%s%d%d",y.num,y.name,&(y.normal),&(y.exam));


}
fclose(fp1);
fclose(fp2);
remove("filename");//删除源文件
rename("temp.txt",fielname);//重命名文件

}


}


void test(char *filename)
{
char numstr[20],ans[2];
ans[0]='y';
while(ans[0]=='y'||ans[0]=='Y')
{
printf("请输入要删除记录的学号:");
gets(numstr);
delete_record(filename,numstr);
printf("是否继续删除(y/n);");
gets(ans);


}


}




void main()
{

test("student.txt");

}
...全文
465 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶之香 2014-03-17
  • 打赏
  • 举报
回复
引用 9 楼 u012706494 的回复:
可为什么要两次关闭fp,,在最后直接关闭一次为什么不行
因为两次返回的文件句柄值是不一样的,当然需要关闭两次
鬼才冷面宝贝 2014-03-12
  • 打赏
  • 举报
回复
不行啊,还是没用
  • 打赏
  • 举报
回复
  remove("filename");//删除源文件
    rename("temp.txt",fielname);//重命名文件
改成:
remove(filename);
rename("xxxxxx.txt',filename);
赵4老师 2014-03-12
  • 打赏
  • 举报
回复
remove, _wremove Delete a file. int remove( const char *path ); int _wremove( const wchar_t *path ); Routine Required Header Compatibility remove <stdio.h> or <io.h> ANSI, Win 95, Win NT _wremove <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns 0 if the file is successfully deleted. Otherwise, it returns –1 and sets errno either to EACCES to indicate that the path specifies a read-only file, or to ENOENT to indicate that the filename or path was not found or that the path specifies a directory. This function fails and returns -1 if the file is open. Parameter path Path of file to be removed Remarks The remove function deletes the file specified by path. _wremove is a wide-character version of _remove; the path argument to _wremove is a wide-character string. _wremove and _remove behave identically otherwise. All handles to a file must be closed before it can be deleted. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tremove remove remove _wremove Example /* REMOVE.C: This program uses remove to delete REMOVE.OBJ. */ #include <stdio.h> void main( void ) { if( remove( "remove.obj" ) == -1 ) perror( "Could not delete 'REMOVE.OBJ'" ); else printf( "Deleted 'REMOVE.OBJ'\n" ); } Output Deleted 'REMOVE.OBJ' File Handling Routines See Also _unlink rename, _wrename Rename a file or directory. int rename( const char *oldname, const char *newname ); int _wrename( const wchar_t *oldname, const wchar_t *newname ); Routine Required Header Compatibility rename <io.h> or <stdio.h> ANSI, Win 95, Win NT _wrename <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns 0 if it is successful. On an error, the function returns a nonzero value and sets errno to one of the following values: EACCES File or directory specified by newname already exists or could not be created (invalid path); or oldname is a directory and newname specifies a different path. ENOENT File or path specified by oldname not found. EINVAL Name contains invalid characters. For other possible return values, see _doserrno, _errno, syserrlist, and _sys_nerr. Parameters oldname Pointer to old name newname Pointer to new name Remarks The rename function renames the file or directory specified by oldname to the name given by newname. The old name must be the path of an existing file or directory. The new name must not be the name of an existing file or directory. You can use rename to move a file from one directory or device to another by giving a different path in the newname argument. However, you cannot use rename to move a directory. Directories can be renamed, but not moved. _wrename is a wide-character version of _rename; the arguments to _wrename are wide-character strings. _wrename and _rename behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _trename rename rename _wrename Example /* RENAMER.C: This program attempts to rename a file * named RENAMER.OBJ to RENAMER.JBO. For this operation * to succeed, a file named RENAMER.OBJ must exist and * a file named RENAMER.JBO must not exist. */ #include <stdio.h> void main( void ) { int result; char old[] = "RENAMER.OBJ", new[] = "RENAMER.JBO"; /* Attempt to rename file: */ result = rename( old, new ); if( result != 0 ) printf( "Could not rename '%s'\n", old ); else printf( "File '%s' renamed to '%s'\n", old, new ); } Output File 'RENAMER.OBJ' renamed to 'RENAMER.JBO' File Handling Routines
鬼才冷面宝贝 2014-03-12
  • 打赏
  • 举报
回复
可为什么要两次关闭fp,,在最后直接关闭一次为什么不行
赵4老师 2014-03-12
  • 打赏
  • 举报
回复
反正帖MSDN比回答问题容易。
叶之香 2014-03-12
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
remove, _wremove Delete a file. int remove( const char *path ); int _wremove( const wchar_t *path ); Routine Required Header Compatibility remove <stdio.h> or <io.h> ANSI, Win 95, Win NT _wremove <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns 0 if the file is successfully deleted. Otherwise, it returns –1 and sets errno either to EACCES to indicate that the path specifies a read-only file, or to ENOENT to indicate that the filename or path was not found or that the path specifies a directory. This function fails and returns -1 if the file is open. Parameter path Path of file to be removed Remarks The remove function deletes the file specified by path. _wremove is a wide-character version of _remove; the path argument to _wremove is a wide-character string. _wremove and _remove behave identically otherwise. All handles to a file must be closed before it can be deleted. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tremove remove remove _wremove Example /* REMOVE.C: This program uses remove to delete REMOVE.OBJ. */ #include <stdio.h> void main( void ) { if( remove( "remove.obj" ) == -1 ) perror( "Could not delete 'REMOVE.OBJ'" ); else printf( "Deleted 'REMOVE.OBJ'\n" ); } Output Deleted 'REMOVE.OBJ' File Handling Routines See Also _unlink rename, _wrename Rename a file or directory. int rename( const char *oldname, const char *newname ); int _wrename( const wchar_t *oldname, const wchar_t *newname ); Routine Required Header Compatibility rename <io.h> or <stdio.h> ANSI, Win 95, Win NT _wrename <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns 0 if it is successful. On an error, the function returns a nonzero value and sets errno to one of the following values: EACCES File or directory specified by newname already exists or could not be created (invalid path); or oldname is a directory and newname specifies a different path. ENOENT File or path specified by oldname not found. EINVAL Name contains invalid characters. For other possible return values, see _doserrno, _errno, syserrlist, and _sys_nerr. Parameters oldname Pointer to old name newname Pointer to new name Remarks The rename function renames the file or directory specified by oldname to the name given by newname. The old name must be the path of an existing file or directory. The new name must not be the name of an existing file or directory. You can use rename to move a file from one directory or device to another by giving a different path in the newname argument. However, you cannot use rename to move a directory. Directories can be renamed, but not moved. _wrename is a wide-character version of _rename; the arguments to _wrename are wide-character strings. _wrename and _rename behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _trename rename rename _wrename Example /* RENAMER.C: This program attempts to rename a file * named RENAMER.OBJ to RENAMER.JBO. For this operation * to succeed, a file named RENAMER.OBJ must exist and * a file named RENAMER.JBO must not exist. */ #include <stdio.h> void main( void ) { int result; char old[] = "RENAMER.OBJ", new[] = "RENAMER.JBO"; /* Attempt to rename file: */ result = rename( old, new ); if( result != 0 ) printf( "Could not rename '%s'\n", old ); else printf( "File '%s' renamed to '%s'\n", old, new ); } Output File 'RENAMER.OBJ' renamed to 'RENAMER.JBO' File Handling Routines
亲,别每次都贴MSDN的说明,大家都可以自己去看的,你这样一贴上来,意义真不大
叶之香 2014-03-12
  • 打赏
  • 举报
回复
引用 5 楼 zgangz 的回复:
1. judge_num_exist 这个函数中打开了文件但未关闭 if(feof(fp)){ fclose(fp); return 0; } else{ fclose(fp); return 1; } 2. remove(filename);//删除源文件 rename("temp.txt",fielname);//
正解 楼主在打开文件的时候,最好不要多次调用fopen打开一个文件,比如你程序里面,两次打开,本就可以只打开一次
ZG 2014-03-12
  • 打赏
  • 举报
回复
1. judge_num_exist 这个函数中打开了文件但未关闭 if(feof(fp)){ fclose(fp); return 0; } else{ fclose(fp); return 1; } 2. remove(filename);//删除源文件 rename("temp.txt",fielname);//
赵4老师 2014-03-12
  • 打赏
  • 举报
回复
请判断每次函数调用的返回值,如果返回值代表出错了,调用perror("");输出错误原因。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧