2017-05-04 99 views
0

我有一个原始文件students.txt。我想。我尝试使用重命名文件studentsmodified.txt覆盖但似乎改名着覆盖文件:用其他修改覆盖原文件

rename("studentsmodified.txt","students.txt"); 

我该怎么办呢?

+0

检查'errno'看到失败的原因 - 可能是一个权限问题,或打开文件。 –

+0

文件存在说错误 –

+0

它应该适用于任何性能良好的操作系统,例如Linux操作系统。您是否可能试图在非POSIX操作系统上执行此操作,例如[Windows](https://msdn.microsoft.com/en-us/library/zw5t957f.aspx)? –

回答

2

便携版使用removerename:使用MoveFileEx function

#include <stdio.h>

...

remove("students.txt"); 
rename("studentsmodified.txt","students.txt"); 

Windows的具体版本:

#include <Windows.h>

...

MoveFileEx("studentsmodified.txt","students.txt",MOVEFILE_REPLACE_EXISTING);