Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

fopen(<>, w) unable to write to file in Windows 10

(1/2) > >>

a_sarkar1024:
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?

Miguel Gimenez:
This is out of this forum scope. Try using perror() and then ask in a programming forum.

a_sarkar1024:
Sincere apologies. Which forum should I post this? By the way I perror() gives me 0.

Miguel Gimenez:
Test another name for the file, just in case a crashed program has it locked.


--- Quote ---Which forum should I post this?
--- End quote ---

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

stahta01:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version