fopen
type568

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

Second argument.

GullRaDriel

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

gnolam

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

type568

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

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.

type568

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

GullRaDriel

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.

BAF

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

type568

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
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

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

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

James Stanley

or perror("fopen");

Ron Ofir

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

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.

Archon
Quote:

No, it's just r.

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

type568

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.

Thread #591415. Printed from Allegro.cc