Archived

This topic is now archived and is closed to further replies.

capn_midnight

Problem with File IO

Recommended Posts

capn_midnight    1707
I had this bit of code, and it worked fine
#include 
#include 
void readfile()
{
	FILE *in;
	if((in=fopen(loc[world],"r"))==NULL)
	{
		perror("Error while loading a data file");
		exit(1);
	}
	fclose(in);
}
 
okay, and here is my current code
#include 
#include 
void readfile()
{
	FILE *in;
	FILE *wlist;
	if((wlist=fopen("world.txt","r"))==NULL)
	{
		perror("Error while loading world list");
		exit(1);
	}
	fclose(wlist);
	if((in=fopen(loc[world],"r"))==NULL)
	{
		perror("Error while loading a data file");
		exit(1);
	}
	fclose(in);
}
 
all of a sudden, my new code doesn''t work. It says the world.txt file doesn''t exist. I''M LOOKING AT IT RIGHT NOW! IT''S IN THE SAME DIRECTORY! The file contains a list of all the rooms. Without it, the next file can''t be opened. What am I doing wrong? I did something stupid while trying to figure this out. I took exit(1) out of the first attempt to open a file. DOS threw some kind of error, but I didn''t have time to read it because my computer shut down ALL ON IT''S OWN! That frickin'' pissed me off right there. shut up CAN I GET A WOO WOO!

Share this post


Link to post
Share on other sites
Falden    122
i''ve had trouble with stuff like that before, too. try putting in the *whole* file name + directory (ex: c:\stuff\world.txt). sometimes this helps.

Falden

Share this post


Link to post
Share on other sites
outRider    852
This usually happens when you''re running a program through the compiler''s run option. when you do fopen("world.txt", "r"); it will only work if the file is actually in the same directory, which you said was the case, but if you run an app through the compiler (or even through PIF''s for DOS apps) the program isn''t being detected as if it''s being run from whatever directory its in, its being run as if the ''Working Directory'' is the local directory. The Working Dir can be anything, the compiler''s directory, the c:\ directory, even the Win9x Desktop. It used to drive me crazy too, til I figured it out.

------------
- outRider -

Share this post


Link to post
Share on other sites
WhatEver    125
Go to the project settings page if you have MSVC. Set the current working directory to your exe.

There are some commands in one of the libs to set your current working directory, but I forget what it is. I''ll make another post later stating what the name is, I''m in a hurry right now.

Share this post


Link to post
Share on other sites
WhatEver    125


Just giving you info that you may find usefull in the future. It''s just that I''ve had the same snag as you, and it frustrated the hell out of me .

And I still haven''t left yet. I hate it when people say lets go before they''re ready...grrrrr.

Share this post


Link to post
Share on other sites