Code::Blocks Forums
Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started 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?
-
This is out of this forum scope. Try using perror() and then ask in a programming forum.
-
Sincere apologies. Which forum should I post this? By the way I perror() gives me 0.
-
Test another name for the file, just in case a crashed program has it locked.
Which forum should I post this?
I don't know. First hit in Google: https://cboard.cprogramming.com/
-
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.
-
Thank you.. This is resolved now.... Not sure what happened, but the smae code works after a system restart.
Thank you for your help.