Problem with File IO

Started by
7 comments, last by capn_midnight 23 years, 6 months ago
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!

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Advertisement
Try "/world.txt"

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
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
Actually, I figured it out. I don''t know how, but I did. It works great now. oh well.

shut up
CAN I GET A WOO WOO!

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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 -
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.
I found the time...


use the funcion "chdir(const char* Path)" before you try to open any files in a win32 application.
actually, WhatEver, I''m using BC++4.52 and doing a console app, not win32. Actaully, it''s okay, I got it to work fine. Don''t worry about it.

shut up
CAN I GET A WOO WOO!

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]



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.

This topic is closed to new replies.

Advertisement