Author Topic: fopen(<>, w) unable to write to file in Windows 10  (Read 7074 times)

Offline a_sarkar1024

  • Single posting newcomer
  • *
  • Posts: 3
fopen(<>, w) unable to write to file in Windows 10
« on: November 12, 2019, 01:14:09 pm »
Hello

I am using CodeBlocks to learn C programming and this is my first post in this forum. I am successfully able to read files using fopen, but when I try to write to a text file within the executable folder using fopen function - I get a null pointer in return.

When I execute the code below:
 - a file with the name HELLOWORLD.TXT is created in the same folder as the executable.
 - I have checked "File name extensions" under View in Windows Explorer and the file name is HELLOWORLD.txt and NOT HELLOWORLD.txt.txt.
 - but then the if block in the following code is executed.

    FILE *fp;
    char s[80];
    fp=fopen("HELLOWORLD.TXT", "w");
    if(fp==NULL)
    {
        puts("Cannot Create File");
        exit(1);
    }

Could you kindly assist me to resolve this issue?
« Last Edit: November 12, 2019, 04:17:43 pm by a_sarkar1024 »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: fopen(<>, w) unable to write to file in Windows 10
« Reply #1 on: November 12, 2019, 01:36:02 pm »
This is out of this forum scope. Try using perror() and then ask in a programming forum.

Offline a_sarkar1024

  • Single posting newcomer
  • *
  • Posts: 3
Re: fopen(<>, w) unable to write to file in Windows 10
« Reply #2 on: November 12, 2019, 01:44:22 pm »
Sincere apologies. Which forum should I post this? By the way I perror() gives me 0.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: fopen(<>, w) unable to write to file in Windows 10
« Reply #3 on: November 12, 2019, 04:34:00 pm »
Test another name for the file, just in case a crashed program has it locked.

Quote
Which forum should I post this?

I don't know. First hit in Google: https://cboard.cprogramming.com/

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: fopen(<>, w) unable to write to file in Windows 10
« Reply #4 on: November 12, 2019, 05:14:36 pm »
You need to know that the executable is ran from the "working directory" set in Code::Blocks.
There is place to change the "working directory" in C::B; but, I do not use it much.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline a_sarkar1024

  • Single posting newcomer
  • *
  • Posts: 3
Re: fopen(<>, w) unable to write to file in Windows 10
« Reply #5 on: November 12, 2019, 05:22:15 pm »
Thank you.. This is resolved now.... Not sure what happened, but  the smae code works after a system restart.

Thank you for your help.