Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » fopen

This thread is locked; no one can reply to it. rss feed Print
fopen
type568
Member #8,381
March 2007
avatar

I have gotten a problem.. I want to save my texture in a text file. This way I can simple write a texture in notepad :)

FILE* cfg=fopen("poly.cfg.txt","r");
  
  if(cfg==NULL)
    return 1;

And, the function returns 1. I have no idea why. I have tentheled (tripled you can say :p) check the name. It was originally poly.cfg.. I thought maybe it needs a *.txt file. The file is a text file, created and edited in notepad. I am using VS6.

The *.exe file and my text file are in the same directory.

Can anybody please tell me, why would "fopen("poly.cfg.txt","r");" return NULL? Thanks..

Inphernic
Member #1,111
March 2001

Second argument.

GullRaDriel
Member #3,861
September 2003
avatar

I second Inphernic , but I will give you the tip he mention:
fopen("myfile.txt","rw");

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

gnolam
Member #2,030
March 2002
avatar

... and now he'll never learn to look things up for himself.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

type568
Member #8,381
March 2007
avatar

No.. This didn't help. cfg==NULL is true. And, by the way: in the borland 3.1 1992, which help I am using, uses "r".

Edit: Can there be any other reason.. ?

Hard Rock
Member #1,547
September 2001
avatar

Are you running from the IDE? Visual Studio runs exe's from a different path, so the text file shouldn't be in the path with the exe unless you are running it directly (eg double clicking on the exe as opposed to running from the IDE). I think it's one level up, but different projects can have different settings.

Test running the exe directly and see if it helps.

_________________________________________________
Hard Rock
[ Stars Dev Company ][ Twitter ][Global Warming: ARA My TINS 07 Entry][Pong Ultra Website][GifAllegS Ver 1.07]
"Well there's also coolwebsearch but we'll let that be an IE exclusive feature" - arielb on the New Browser Plugins "What's better, HTML or Variables?"

type568
Member #8,381
March 2007
avatar

Thanks Hard Rick, I had a similar issue with a bitmap, so I tested this before asking for help here..

GullRaDriel
Member #3,861
September 2003
avatar

Type568, you imo have two problem. The second was solved (path issue), but the first ...

If you manage to write something in a file you fopen in readable only mode, well, have good time.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

BAF
Member #2,981
December 2002
avatar

You shouldn't be using "rw" unless you want to read AND write. "w" is what you likely want.

type568
Member #8,381
March 2007
avatar

I wanted to read only.. But generally, ANYTHING. I just want something except for NULL from my fopen()..

Edit: Maybe somebody has some working code with fopen()? Just something working, with it's source.

Here's mine:

int init_kyb()
  {
  FILE* cfg=fopen("lol.txt","r");
  
  if(cfg==NULL)
    return 1;
        }

All the code, is attached...

Thanks again.

Archon
Member #4,195
January 2004
avatar

Quote:

I second Inphernic , but I will give you the tip he mention:
fopen("myfile.txt","rw");

Quote:

You shouldn't be using "rw" unless you want to read AND write. "w" is what you likely want.

Is that a MSVC standard?

I thought that it was r+ .

James Stanley
Member #7,275
May 2006
avatar

No, it's just r.
If it doesn't accept just r, burn your hard disk and install Linux and gcc.

EDIT:
Don't try installing to your burnt hard disk, that would really ruin your experience of Linux - get another hard disk first. Or don't burn the other in the first place. Sorry if this is too late now.

clovekx
Member #3,479
April 2003
avatar

Check the errno value after fopen to see where is the problem.

James Stanley
Member #7,275
May 2006
avatar

or perror("fopen");

Ron Ofir
Member #2,357
May 2002
avatar

How about creating a file and searching for it? That way you can know your path. And here's a better way to make sure you will load the file:

char name[200];

get_executable_name(name, sizeof(name));
replace_filename(name, name, "poly.cfg", sizeof(name));
FILE * cfg = fopen(name,"r");

if(cfg == NULL)
    return 1;
}

There's also a way to change the execution path, but I don't remember how.

EDIT: It is called the working directory can be changed using chdir. Please note that this is not ANSI C.

Sirocco
Member #88
April 2000
avatar

It probably doesn't matter given the compiler you're using, but a long time ago I got in the habit of specifying text or binary input and output, so I'd use "RB" or "RT" as the final argument, assuming you are reading and not writing. If writing open the file with "WB" or "WT".

Oddly, with an older version of Borland C++ I've had issues with files not properly opening using read/write mode, so I never use that.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Archon
Member #4,195
January 2004
avatar

Quote:

No, it's just r.

I know that that works, but it looks like "rw" is supposed to mean read and write.

type568
Member #8,381
March 2007
avatar

Thanks a lot, to everybody.. I managed to read the file, and do some nice crap to what I've read. Here it is, VS6 workspace, Also the file with an *.exe included :p

Edit: comments are greatly appreciated :p

Edit(1): Gnolam, you are totally not right.

Go to: