Jump to content
  • Advertisement
Sign in to follow this  
Texas Brigade

Puzzling C++ File I/O Error

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

If you intended to correct an error in the post then please contact us.

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;
  GameWorld.LoadWorld("World.txt");
  cout<<GameWorld.Get_WorldName();
  cin.get();
  return 0;
}

World.cpp
void World::LoadWorld(string FileName)
{
ifstream Dataread("World.txt");
if (! Dataread.fail())
{
getline(Dataread, WorldName);
Dataread.close();
World_Loaded = true;
}
else
{
cout<<"Error";
}
}

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

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Texas Brigade
You 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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!