2

I've been working on my game and I can easily save my text document, everything works perfectly. My question is, how can I save the text document into a file called "Saves." Here is my code.

Also! I get the input for the char* name from

Save(gets(new char [50]));

Why won't this code work right here?

char* newArray = new char[strlen("PaintAPicture/")+strlen("Saves/")+strlen(name)+strlen(".asciip")+1];
        strcpy(newArray,"PaintAPicture/");
        strcpy(newArray,"Saves/");
        strcpy(newArray,name);
        strcat(newArray,".asciip");

I took what you said about using a string, but it's not creating the file and I get the Saving Failed, Main Problem error.

if(saveFile)
    {
        system("cls");
        string prename;
        cout << "Enter level's number: ";
        cin >> prename;
        string name = "Files/" + "Saves/" + prename + ".asciip";

        ofstream out(name, ios::binary);
        if (!out.is_open()){ MessageBox( 0, "Saving failed! Main problem.", 0, MB_ICONERROR); system("cls"); RedrawMap(); return 0; }

        system("cls"); cout << "Saving...";


        system("cls");
        ShowConsoleCursor(false);
        cout << "Saving...";
        Sleep(1000);

        for(int i = 9; i < SizeY; i++)
        {
            for(int j = -1; j < SizeX; j++)
            {
                out << Map[i][j].ch << endl;
                out << (int)Map[i][j].color << endl;
            }
        }
    }

    cout << '\a';
    out.close();
}
Ryan12345
  • 35
  • 1
  • 8
  • 1
    Check out `std::string`. There is rarely a need to use normal `new` in C++ and almost never one to use the vector form `new[]`. If you need a raw storage for something, use a `std::vector`. In case you wonder why, consider the path with `return 0`: In that path, the allocated memory is not released, so you have a memory leak. – Ulrich Eckhardt Apr 18 '15 at 08:34
  • So should I just change char* newArray to string newArray? But how can I save it still into two folders, then save the files? – Ryan12345 Apr 18 '15 at 15:46
  • There's one thing that makes it hard to guess what's wrong and that is that there seem to be two `out` objects, one inside the `if(saveFile)` part, one outside (see `out.close()`). Also, what happens? Does opening the file fail? In any case, output `name` after assembling it to check whether it has the right value. Actually, replace it with a fixed value to be sure! Then, are you sure that the folder exists? Since you provide a relative path, are you sure that you have the right working directory? – Ulrich Eckhardt Apr 18 '15 at 16:16
  • Yes, opening the file fails. So basically it's not creating the file and I have no clue why! – Ryan12345 Apr 18 '15 at 16:38
  • @UlrichEckhardt Hey man, I have a coding issue on a different topic about my game, and I can't make a post right now, but is their anyway I can contact you the code VIA email? – Ryan12345 Apr 24 '15 at 21:17

1 Answers1

1

Prepend the filename with "Saves/"

char* newArray = new char[strlen("Saves/")+strlen(name)+strlen(".asciip")+1];
strcpy(newArray,"Saves/");
strcat(newArray,name);
strcat(newArray,".asciip");

Also std::string class is designed for storing and manipulating strings. Much easier to use and a lot less error prone than C-strings. Info on string.

EDIT: Your first piece of code

char* newArray = new char[strlen("PaintAPicture/")+strlen("Saves/")+strlen(name)+strlen(".asciip")+1];
    strcpy(newArray,"PaintAPicture/");
    strcpy(newArray,"Saves/");
    strcpy(newArray,name);
    strcat(newArray,".asciip");

doesn't work becouse you're using strcpy where you should use strcat.

strcpy(newArray,"Saves/");
strcpy(newArray,name);

should be

strcat(newArray,"Saves/");
strcat(newArray,name);

As for the problem with creating the file, do those folders exist already? ofstream can create new files to a specified folder, but it cannot create new folders. See https://stackoverflow.com/a/9089919/4761271

Community
  • 1
  • 1
Juhani
  • 26
  • 4
  • It still doesn't create a new file called Saves, then would save the .asciip file into that folder, this was where I had no idea how to figure it out myself. And what do you mean prepend? – Ryan12345 Apr 18 '15 at 04:40
  • Did you remember to write it to the newArray also? I only showed the line that increases the size of newArray to fit the name @Ryan12345 – Juhani Apr 18 '15 at 04:44
  • @juhanni, wait no. what do you mean by writing it to the newArray, I thought we just did? – Ryan12345 Apr 18 '15 at 04:45
  • @Ryan12345 I added to the answer the lines that I mean – Juhani Apr 18 '15 at 04:47
  • Yes! Thank you it worked. I'll mark your answer:) and why did you say to use vectors instead? Would it make a difference? – Ryan12345 Apr 18 '15 at 04:49
  • @Ryan12345 See http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap and http://www.cplusplus.com/reference/vector/vector/ for info on vector itself – Juhani Apr 18 '15 at 04:58
  • I'm sorry, but I have one more question. How can I save it into two files, so one in another, for example. Save it into a file called Test1, then Saves, then the acsii file? I tried my way, but no success! – Ryan12345 Apr 18 '15 at 05:09
  • Why doesn't this code work right here? *edited in my question – Ryan12345 Apr 18 '15 at 05:34
  • Do you really suggest using `std::vector` to store strings, @Juhani? – Ulrich Eckhardt Apr 18 '15 at 08:35
  • @UlrichEckhardt (-ლ) (that's supposed to be a facepalm) I didn't even think that we were storing a string, I just saw a dynamic array... :D I'll fix my post. – Juhani Apr 18 '15 at 09:58