fopen() 在 C 中不起作用

发布于 2024-12-04 01:53:53 字数 1134 浏览 1 评论 0原文

我把它修好了。感谢您的所有帮助。

我现在已经浏览了很多文章、论坛帖子和主题;然而,没有人真正解决我的问题。问题是我的 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 技术交流群。

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

发布评论

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

评论(5

优雅的叶子 2024-12-11 01:53:53

尝试错误。您可能没有权限或类似的东西。

FILE *receipt = fopen("receipt.txt", "w");
if (!receipt)
    perror("fopen");

Try perror. It's possible you don't have permissions or something like that.

FILE *receipt = fopen("receipt.txt", "w");
if (!receipt)
    perror("fopen");
缱倦旧时光 2024-12-11 01:53:53

唯一有意义的解释是您没有在工作目录中创建文件的权限,或者工作目录不是您查找要创建的文件的位置。

fopen() 不工作

好吧,当然它可以工作。您不应该认为标准库不起作用。

没有警告/通知或其他内容让我知道出了什么问题。

您在调用 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.

fopen() not working

Well, of course it works. You shouldn't get in the mindset that the standard library doesn't work.

No warnings/notices or otherwise stuff to give me any idea of what is wrong.

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?

蓝眸 2024-12-11 01:53:53

文件已创建,但不在您期望的位置。

检查工作目录 (getcwd)。

The file is created, but not where you expect it to be.

Check the working directory (getcwd).

孤凫 2024-12-11 01:53:53

我也有同样的问题。我正在使用 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.

时光清浅 2024-12-11 01:53:53

您应该使用 strerror 自行格式化错误,或使用 perror 打印与 errno 匹配的系统错误。

man errno 可能会帮助你

You should use strerror to format the error yourself or perror to print the system error matching with the errno.

man errno may help you

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