2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dennis
  • 909
  • 4
  • 13
  • 30
  • 2
    Please reduce the code you gave us to only the relevant code, not the entire program. –  Sep 10 '11 at 09:42
  • 1
    You should check what fopen returns. Maybe the file isn't writable. –  Sep 10 '11 at 09:43
  • Are you sure the program is running in the same directory where you're looking for the created file? You say you tried adding `if (!receipt)...`; what was the result? I can't even tell from what you've written whether `fopen()` reports an error or not. – Keith Thompson Sep 10 '11 at 09:55
  • Try a full path on the `fopen` call: eg `fopen("/some/existing/accessible/path/receipt.txt", "w")` – pmg Sep 10 '11 at 11:53
  • Downloaded an App to get me the fullpath to the folder and smacked that in, new in this mac stuff so that was the issue. Thanks! – Dennis Sep 10 '11 at 12:08

5 Answers5

8

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?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 3
    +1 for "*You shouldn't get in the mindset that the standard library doesn't work*" –  Sep 10 '11 at 09:58
7

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

FILE *receipt = fopen("receipt.txt", "w");
if (!receipt)
    perror("fopen");
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • if (!receipt) { perror("fopen"); } else { printf("Nothing Wrong"); } returns nothing wrong. :/ – Dennis Sep 10 '11 at 10:28
3

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

Check the working directory (getcwd).

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • I'll vote this as #1 with permissions as #2. I really don't like using relative directories (foo.txt) unless I have control over the current directory. For debugging use getcwd() to see where you really are. – Gilbert Sep 10 '11 at 12:33
2

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.

1

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

mathieug
  • 901
  • 1
  • 11
  • 24