Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: a_sarkar1024 on November 12, 2019, 01:14:09 pm

Title: fopen(<>, w) unable to write to file in Windows 10
Post by: a_sarkar1024 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?
Title: Re: fopen(<>, w) unable to write to file in Windows 10
Post by: Miguel Gimenez 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.
Title: Re: fopen(<>, w) unable to write to file in Windows 10
Post by: a_sarkar1024 on November 12, 2019, 01:44:22 pm
Sincere apologies. Which forum should I post this? By the way I perror() gives me 0.
Title: Re: fopen(<>, w) unable to write to file in Windows 10
Post by: Miguel Gimenez 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/
Title: Re: fopen(<>, w) unable to write to file in Windows 10
Post by: stahta01 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.
Title: Re: fopen(<>, w) unable to write to file in Windows 10
Post by: a_sarkar1024 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.