Public Group

# Puzzling C++ File I/O Error

This topic is 4898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, I'm a month into learning C++ (Still adjusting to {} and ; after programming in the ; and {}-less paradise of VB.NET). I am starting on a little console text game. I'm working on the world class. The first thing I've gotten around to is WorldName. The name loads from a file and is assigned to WorldName. I'm testing reading it from the file. I'm using Dev-C++. Here is Main.cpp and World.cpp. If anybody needs World.h, say so and I'll post it. The file is called "World.txt", contains one line, "The Whole World" and is located in the directory that has all the project files in it. It compiles with zero errors. However, when run from the IDE, Dataread fails and nothing is read. I ran it from the .exe seperately though, and Dataread found it and read the file. Why doesn't it succeed in the IDE??????????????? You may also rip apart the rest of my code to shreds if I'm doing anything bad. Main.cpp
#include <iostream>
#include "World.h"

int main(int argc, char *argv[])
{
World GameWorld;
cout<<GameWorld.Get_WorldName();
cin.get();
return 0;
}


World.cpp
void World::LoadWorld(string FileName)
{
{
}
else
{
cout<<"Error";
}
}

//Gets the Name of the world
string World::Get_WorldName()
{
{
return WorldName;
}
else
{
return "";
}
}



##### Share on other sites
It sounds like some sort of "current working directory" issue. For instance, when run by itself, the current working directory will be the same directory the program is in. But from the IDE's point of view, the current working directory could be somewhere else and so the program is trying to find the file in the wrong directory. I can't think of any way to fix it because I don't use Dev-cpp though. Just experiment around with putting World.txt in different nearby directories and it should work eventually. Hope that helps.

##### Share on other sites
Quote:
 Original post by Texas BrigadeYou may also rip apart the rest of my code to shreds if I'm doing anything bad.

I'll just draw your attention to the fact that World::LoadWorld() ignores the passed-in file name parameter currently, using "world.txt" regardless. :) (It's also not the best possible name because of the needless redundancy, and you should probably do this work in a constructor anyway.)

##### Share on other sites
As for ripping apart the rest of the code...
Quote:
 if (World_Loaded = true)
You meant == there, not just =. Turn up your compiler's warning level; this can be a real problem if you're coming from languages such as VB. (Alternatively, just if (World_Loaded) will work.)

But don't bother changing it: just return WorldName. Move the file-loading code to the class constructor. A "World" object shouldn't exist in memory if you haven't loaded a world into memory.

(Also, I'd recommend renaming the function to Name(). "World" is redundant and I don't personally like functions to begin with "get" or "set". :))

##### Share on other sites
Thanks for all the replies.

To nobodynews: Sounds like it might work. I'll try that.

To Zahlman: Yes, I wondered if somebody would realize that. I did that on purpose for now, just to remove a possible source of error. I'll change it back when I get the rest of the function working.

To BeerHunter: All sound like good suggestions. I'm always looking for better ways to code. Especially since I'm just learning and don't want to form bad habits.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015330
×