Why!!?!??!??

Why is this not opening my file so I can view it, and it dose not show up anywhere in my computer?

1
2
3
ofstream SaveFile;
            SaveFile.open("SavedGame.txt", ios::out);
            SaveFile << "This is your save file, editing any of this could severly damage your game./n";



I have the functions and evrything the whole thing works fine with zero errors, this is just the only chunk about files I have.
How are you starting your program?

The "SavedGame.txt" file will be written to whatever directory is active when you start your program. (And if the program doesn't have write permission on that directory, no file will be created.)

This is where things get a little hairy. What OS are you using? Windows?
I am compiling it to start it, and I am using Windows 7
I am compiling it to start it


Makes no sense, btw. Compiling != running. As Duoas said, this file will be created in whichever directory your program runs from. In other words, it will be in the same directory as the exe file.
I think you have made a basic mistake.

ofstream is for writes to a file, not reading a file, for that you want to use ifstream.

O for Output
I for Input

I tried switching it to ofstream, fstream, and ifstream, a file still did not open, and I start my code with all of my headers, aand my window proc, and winmain(etc)
You should show your code at least the lines that are relevant to the file. In between, have you tried to support the full path for the file?
ofstream is used to read data from a file into your program. To open the file just means that we are ready to read from the file. It will not in any way display the content of the file on the screen, unless you write code for that to happen.
Oh! thanks peter!!
ResidentBiscuit wrote:
In other words, it will be in the same directory as the exe file.

No, it might not be.

Since he is running the exe from his IDE, it is more than probable that the file is being created (or trying to be created) in a different directory. And Windows7 is pretty strict about file permissions.


On Windows, the proper place to save files is the user's Application Data folder.

The simplest way to get that folder is to use the APPDATA environment variable.

But you'll have to use a Windows API function to create your saved game data directory.


Here's a function to create everything for you, and an example of how to use it to open a file for writing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <cstdlib>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

std::string GameFileName( const std::string& filename )
  {
  const char* p_appdata = std::getenv( "APPDATA" );
  if (!p_appdata) throw 1;
  
  std::string appdata( p_appdata );
  appdata.append( "/johnnys-game" );
  CreateDirectoryA( appdata.c_str(), NULL );
  
  appdata.append( "/" );
  appdata.append( filename );
  return appdata;
  }
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>

int main()
  {
  try {
    std::ofstream f( GameFileName( "SavedGame.txt" ).c_str() );
    f << "Game progress for Johnny's game.\n";
    } 
  catch (int)
    {
    std::cerr << "Fooey! Could not save game data!\n";
    }
  
  return 0;
  }

On Windows7, this should create a file named:

  C:\Users\johnny\AppData\Roaming\johnnys-game\SavedGame.txt

(Where johnny is your username -- or the username of whoever is logged on.)

Use it the same way to open a file for reading, using a std::ifstream instead. Just remember that you cannot open an ifstream on a file that has not already been created.


Hope this helps.


PS. Windows purists, I know about SHGetKnownFolderPath(). And I know about Boost Filesystem. Let's not overload poor OP just yet with a hobbyist game for his friends. The code I posted is fine and safe.
Topic archived. No new replies allowed.