使用 C fopen 保存文件

发布于 2024-09-29 20:47:00 字数 294 浏览 1 评论 0原文

我用 C 做了一个程序,但它不允许保存在 c:\SomeDirectory\afile.txt

我正在使用这个:

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

为什么呢?是否有可以保存的定义文件夹?

SomeDirectory 是先前创建的。

我使用的是 Windows 7 操作系统。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-10-06 20:47:00

如果fopen遇到错误,它会设置errno变量来指示发生了什么错误。您可以对此进行测试,或者更简单,使用 perror 打印出一条错误消息,告诉您出了什么问题:

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

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");
}
离不开的别离 2024-10-06 20:47:00

听起来也许“SomeDirectory”不存在。您可以使用 C++ 创建文件夹,但您需要检查文件夹是否已经存在。仅调用打开命令不会自动创建文件夹。 :)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文