How to input a 2d vector into a file

Hi I have this code that has this input and I need to put it in the file and I do not know how, this are the instructions:"If the user has generated a puzzle, they have the option to save it. If they opt to save the puzzle, your program should write the puzzle to a file. The program should then return to the main menu".
Last edited on
Here's one way to do it, there are faster ways to write to disk, but this should be sufficient for files 1MB or smaller. For larger files check out the ofstream.write() function or memory mapped files.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>
int main()
{
        const int size = 30;
        char wordSearch[size][size] = {{"fill this in as you"},{"have done, this"},{"is just some test data"}};
        // above we have a test array, now to put it into a file: 
        std::ofstream outFile("out.txt"); // just make up a file extension, or use .txt 
        for(int i = 0; i < size; i ++)
        {
                outFile << wordSearch[i] << std::endl;
        }
        outFile.close();
}

https://cplusplus.com/reference/fstream/ofstream/
Last edited on
Topic archived. No new replies allowed.