Save file with C fopen

2019-08-02 18:07发布

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.

2条回答
闹够了就滚
2楼-- · 2019-08-02 18:42

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. :)

查看更多
仙女界的扛把子
3楼-- · 2019-08-02 18:57

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");
}
查看更多
登录 后发表回答