How to move a file in C

a 夏天 提交于 2019-12-24 14:28:25

问题


I need to move a file from an input to a certain folder, however, 'rename' keeps crashing the entire application.

char start[50]; 
const char dest = "C:\Windows\System32\oobe\info\backgrounds\supertemp.JPG";

printf("Please enter file to move \n");
fgets(start, sizeof(start), stdin);

if (rename(start, dest) == 0){
    printf("Success \n");
}

else{
    printf("Failed %s ", GetLastError());
}

It always crashes at ' if(rename(start, dest) ==0)'.

I don't ever get as far as retrieving an error value.

Any help would be greatly appreciated! Cheers


回答1:


Two problems. You have not declared dest as an array, and you need to put a double \\ in a literal string, to prevent \ being used as an escape character.

const char dest[] = "C:\\Windows\\System32\\oobe\\info\\backgrounds\\supertemp.JPG";


来源:https://stackoverflow.com/questions/26577984/how-to-move-a-file-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!