fopen()函数使用C不工作 [英] fopen() not working in C

查看:200
本文介绍了fopen()函数使用C不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:得到它固定的。感谢所有帮助。

Got it fixed. Thanks for all the help.

我现在已经过好几篇文章,论坛帖子和主题看这里;然而,没有实际上已经固定我的问题。问题是,我的的fopen(file.txt的,W); 不创建文件

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;
}

我试过扔在

if(!receipt) { printf("Error!"); } else { fprintf(blabla); }

但无济于事。

它根本无法创建文件:在X code和/运行;在Mac上。没有警告/通告或其他的东西给我的什么是错的任何想法。

It simply does not create the file :/ Running in xCode & on Mac. No warnings/notices or otherwise stuff to give me any idea of what is wrong.

我尝试添加系统(PWD)弄清楚,如果不保存它应该保存它,但我有一个很难真正找到该目录(如果它是说不上来临时,但即便如此,该文件应该在那里?)。很显然,我不是质疑图书馆,咄的实际有效性,含蓄,这是我用不给我我所期待的则fopen?
我不能让PERROR给我任何有用的信息。一切似乎工作,因为它应该,我只是不明白的文件。请避免任何更多的评论自作聪明,如果你不想帮就是不写。

还删除了所有code,但实际fopen()函数和fprintf()

Also removed all code but the actual fopen() and fprintf()

推荐答案

尝试 PERROR 。这是可能的,你没有权限或类似的东西。

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

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

这篇关于fopen()函数使用C不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆