Save file with C fopen

删除回忆录丶 提交于 2019-12-11 08:25:10

问题


I did a program in C but it does not allow to save on c:\SomeDirectory\afile.txt

I'm using this:

FILE* m_hFile = fopen("c:\\SomeDirectory\\afile.txt", "a+t");
fprintf(m_hFile, "testing");
fclose(m_hFile);

Why that? Is there a defined folder I can save in?

SomeDirectory is previously created.

I'm using Windows 7 OS.


回答1:


If fopen encounters an error, it sets the errno variable indicating what error occurred. You can test this, or even simpler, use perror to print out an error message that will tell you what went wrong:

FILE* m_hFile = fopen("c:\\SomeDirectory\\afile.txt", "a+t");
if (m_hFile == NULL) {
  perror("fopen");
}



回答2:


It sounds like perhaps "SomeDirectory" doesn't exist. You can create folders with C++ but you'll want to check if one's already there. Just calling the open command doesn't automagically create the folder. :)



来源:https://stackoverflow.com/questions/4048949/save-file-with-c-fopen

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