0

Beginner here so please be patient. I am having a bit of a trouble using a list of words for a simple hangman program that I am creating as an exercise. I am using this code to read a list of words from a text file:

vector<string> getWords()
{
  vector<string> text_file;
  ifstream ifs( "my_hangman_words.txt" );
  string temp;
  while(getline(ifs, temp))
  {
    text_file.push_back( temp );
  };  
  return text_file;
}

This works fine when I compile and run directly but then it does not when I run the executable individually. From what I understand I need to write the vector to a file and #include the file with my program. Could someone give me a pointer on how to achieve that?

Dionysis
  • 804
  • 8
  • 25
  • 2
    You are probably running the executable from two different directories and it will only look in the current directory for "my_hangman_words.txt". – hmjd Mar 15 '12 at 11:14
  • What do you mean by "compile and run directly" and "run the executable individually" ? @hmjd probably has it right - great argument for always error checking file IO! – John3136 Mar 15 '12 at 11:15
  • It is almost always wrong to use hardcoded paths – user877329 Mar 15 '12 at 11:23
  • @hmjd Nop, I was running the executable from the same directory. – Dionysis Mar 15 '12 at 11:47
  • @John3136 By "compile and run directly" I mean doing it from inside MacVim. "Run the executable individually" I mean going to the finder and double clicking to run it in the terminal. Konrad's answer clarified why that happened. – Dionysis Mar 15 '12 at 11:47

1 Answers1

1

#includes are for bringing in code not resources.

The lack of an absolute path (i.e. c:/workingpath/file.txt) is what it likely preventing your code from reading in the file correctly when run directly.

When running in the debugger you can set a number of extra parameters including command arguments and working path (links for Visual Studio). These aren't applied when running the binary directly.

If you could be more specific with what happens when you run directly we can help more.

Community
  • 1
  • 1
Konrad
  • 39,751
  • 32
  • 78
  • 114
  • Thank you :) That is indeed the case. When I tried the full path it is working fine. But this is surely not the right approach for including the list of words with the game? Ideally the list should be hidden in the executable so that the player can not see the solutions. That is why I though of writing the vector to a file and use #include. At the very least I should be able to point to the text file that is in the same directory as the executable, instead of hard coding the path. Or am I missing something? Note: I am on a mac. – Dionysis Mar 15 '12 at 11:42
  • You could store it as a static array in a header file but I'd suggest using a resource file. Worst case you could hard code the values into your program but they could easily be obtained by looking at your application strings. :-) – Konrad Mar 15 '12 at 11:54
  • But the resource files are visual basic specific no? Isn't there an elegant solution for standard c++ since I am on a mac? – Dionysis Mar 15 '12 at 12:23