C对文件操作的问题(刚学C)?

半点闲 2003-08-25 05:31:47
我原来是用VB的,现在开始学习C~~

请问如何用C给文件改名?

VB中语句如下:
Name "C:\VB\FF.TXT" As "C:\VB\FT.TXT"
...全文
32 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdspower 2003-08-28
  • 打赏
  • 举报
回复
应该是
#include <stdio.h>
void main()
{
rename("asd.txt","ad.txt");
}
Wolf0403 2003-08-26
  • 打赏
  • 举报
回复
呵呵。。。
C 中能定义“类”吗?!
新自由呼吸 2003-08-26
  • 打赏
  • 举报
回复
最好是自己定义个类,交互输入想改的文件名,目标文件名,呵呵~~~~
liubingqian 2003-08-26
  • 打赏
  • 举报
回复
#include <stdlib.h>

rename("旧名字","新名字");
pcyy 2003-08-26
  • 打赏
  • 举报
回复
在vc6.0下面第一种方法不行。
出错:'rename' : undeclared identifier
程序如下:
#include <stdlib.h>
void main()
{
rename("asd.txt","ad.txt");
}
请gaosu指点。
xdspower 2003-08-26
  • 打赏
  • 举报
回复
#include <stdlib.h>

rename("旧名字","新名字");
或者
#include <stdlib.h>
system("ren 旧名字 新名字");//调用dos命令行工具来完成
半点闲 2003-08-25
  • 打赏
  • 举报
回复
对不起,是我没有说明白!

我用的编辑器是TC~~~不知上述语句是否适用?
xdyanzg 2003-08-25
  • 打赏
  • 举报
回复
MSDN

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'

69,382

社区成员

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

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