making files, windows aaaargh

Started by
3 comments, last by Programmer16 18 years ago
hey, I'm trying to create a small file called highscore.sav which will contain ten names and ten scores. I used to do this by making an empty .txt file and changing the extension. Now it seems windowsXP doesn't allow that anymore. I can't acces the extensions. This is the code I'm using to initialise the file, but it doesn't do anything. It compiles fine, but the file stays at 0 kb after running.

include <string>
#include <fstream>

int main(){
   int i;
   
   std::fstream save( "topscore.txt" );
    //If the file loaded
    if( save != NULL )
        {
        for (i=0; i<10; i++){
            save << 300;
            save << "/n";
            save << "xyz";
            save << "/n";
            }
        save.close();
        }
    return 0;
}

i've tried entering both topscore.txt and topscore.sav...
Advertisement
You instantiate the variable save onto the runtime stack, and then ask for it being not null. Testing on null is senseful for pointer variables only (i.e. if you allocate the stream by using the new operator). So drop the
if( save != NULL )
condition. If that works then look out whether something like
if( save.is_open() )
fulfills your needs instead.

I would really wonder if XP forbids creation of files ending on .sav or so. The only things that may hit you are
(a) you don't have write permission to the directory to create a new file, or
(b) you don't have write permission to the file to overwrite it if already existing.
So the ofstream command doesn't return a pointer?


found the error now: i misstyped

std::fstream save( "topscore.txt" );
should be

std::ofstream save( "topscore.txt" );
Quote:Original post by twoaterisn
So the ofstream command doesn't return a pointer?

ofstream isn't a command but a class name. If you type
std::ofstream myStream("xyz");

you instantiate an object of class type std::ofstream and name myStream on the runtime stack. Such an object is destroyed as soon as the nearest surrounding pair of {} is left (the braces enclose a so-called "scope"). In your case that means that the object named save is destroyed when the main routine is left. (Okay, all is destroyed if main is left, but you understand the principle of that sentence, I hope.)

If, on the other hand, you use something like
std::ofstream* myStream = new std::ofstream("xyz");

then you allocate a new object on the heap (say the main memory), and assign the address of that object to the pointer variable myStream. Although the pointer itself gets destructed if the nearest scope is left, the object remains onto the heap. Say, you are still able to access it (if you have at least one pointer still pointing to it, otherwise it is lost until the app exits and you have produced a so-called "memory leak").

Quote:Original post by twoaterisn
found the error now: i misstyped

std::fstream save( "topscore.txt" );
should be

std::ofstream save( "topscore.txt" );

Oh yes, I've overseen it, too ;)
Quote:Original post by twoaterisn
I'm trying to create a small file called highscore.sav which will contain ten names and ten scores. I used to do this by making an empty .txt file and changing the extension. Now it seems windowsXP doesn't allow that anymore. I can't acces the extensions.


Sounds like they're not enabled in the system. Go to any folder (My Documents will work.)

Click 'Tools' and then 'Folder Options...'
Click the 'View' tab
Scroll down until you see "Hide extensions for know file types" and uncheck it.

HTH!

[Edited by - Programmer16 on April 20, 2006 2:54:42 AM]

This topic is closed to new replies.

Advertisement