fopen() 在 C 中不起作用
我把它修好了。感谢您的所有帮助。
我现在已经浏览了很多文章、论坛帖子和主题;然而,没有人真正解决我的问题。问题是我的 fopen("file.txt", "w");
没有创建该文件。
代码:
//
//Includes
#include <stdio.h>
int main ()
{
FILE *receipt = fopen("receipt.txt", "w");
//Create file
fprintf(receipt, "Price: %.2f$", purchase);
fprintf(receipt, "\nDiscount: %.2f$", discount);
fprintf(receipt, "\nTax %%: %.2f%%", tax_pct);
fprintf(receipt, "\nTaxes: %.2f$", tax);
fprintf(receipt, "\nTotal Price: %.2f$", end_price);
fprintf(receipt, "\n\nEnd of Receipt.");
fclose(receipt);
return 0;
}
我尝试过添加
if(!receipt) {
printf("Error!");
}
else {
fprintf(blabla);
}
但无济于事。
它根本不创建文件:/ 在 Xcode 和 Mac 上运行。没有警告/通知或其他东西让我知道出了什么问题。
*我尝试添加 system("pwd")
来确定它是否没有将其保存在应该保存的位置,但我很难真正找到该目录(我不知道是否它是临时的,但即使如此,文件也应该在那里?)。显然我并没有质疑这个库的实际有效性,呃,隐含的是我使用的 fopen
没有给我我所期望的东西?
我无法让 perror 给我任何有用的信息。一切看起来都会按其应有的方式进行;我只是没有得到文件。请避免更多自作聪明的评论,如果您不想帮忙就不要写。*
此外,我删除了所有代码,但实际的 fopen()
和 fprintf()< /代码>。
I got it fixed. Thanks for all the help.
I've now looked through quite a few articles, forum posts and topics here; however, none have actually fixed my issue. The problem is that my fopen("file.txt", "w");
doesn't create the file.
Code:
//
//Includes
#include <stdio.h>
int main ()
{
FILE *receipt = fopen("receipt.txt", "w");
//Create file
fprintf(receipt, "Price: %.2f$", purchase);
fprintf(receipt, "\nDiscount: %.2f$", discount);
fprintf(receipt, "\nTax %%: %.2f%%", tax_pct);
fprintf(receipt, "\nTaxes: %.2f$", tax);
fprintf(receipt, "\nTotal Price: %.2f$", end_price);
fprintf(receipt, "\n\nEnd of Receipt.");
fclose(receipt);
return 0;
}
I've tried throwing in
if(!receipt) {
printf("Error!");
}
else {
fprintf(blabla);
}
But to no avail.
It simply does not create the file :/ Running in Xcode and on Mac. No warnings/notices or otherwise stuff to give me any idea of what is wrong.
*I tried adding system("pwd")
to figure out if it didn't save it where it should save it, but I have a hard time actually finding that directory (I don't know if it's temp, but even so the file should be there?). Obviously I wasn't questioning the actual validity of the library, duh, implicit that it was the fopen
I was using not giving me what I expected?
I can't make perror give me any useful information. Everything would appear to work as it should; I just don't get a file. Please avoid any more smartass comments, if you don't want to help just don't write.*
Also, I removed all code, but the actual fopen()
and fprintf()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
错误
。您可能没有权限或类似的东西。Try
perror
. It's possible you don't have permissions or something like that.唯一有意义的解释是您没有在工作目录中创建文件的权限,或者工作目录不是您查找要创建的文件的位置。
好吧,当然它可以工作。您不应该认为标准库不起作用。
您在调用
fopen()
后没有检查错误。如果您不检查错误,您希望如何将它们交付给您?The only explanations that makes sense are that you don't have permissions to create the file in the working directory, or the working directory is not where you are looking for the file to be created.
Well, of course it works. You shouldn't get in the mindset that the standard library doesn't work.
You did not check for errors after calling
fopen()
. If you don't check for errors, how do you expect them to be delivered to you?文件已创建,但不在您期望的位置。
检查工作目录 (
getcwd
)。The file is created, but not where you expect it to be.
Check the working directory (
getcwd
).我也有同样的问题。我正在使用 VSC 进行编码,并且尝试了与您相同的代码,但它也不起作用。所以,我在其他问题中读到大多数防病毒软件都阻止了 VSC。禁用我的 avast 防病毒软件后,问题已解决。
I had the same problem. I was using VSC to code and I tried the same code as yours and it didn't work also. So, I have read in other problems that the most of the antivirus softwares are blocking VSC. After disabling my avast antivirus the problem has been solved.
您应该使用
strerror
自行格式化错误,或使用perror
打印与 errno 匹配的系统错误。man errno
可能会帮助你You should use
strerror
to format the error yourself orperror
to print the system error matching with the errno.man errno
may help you