Storing data in a file

I am trying to store the Title, Artist, and date published of a list of CD's. I can't seem to be able to get it to print the list or not sure if it is actually storing it. This is what i have so far... any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    char names[5][100];
    
    for (int i = 0; i < 5; i++)
    {
        cout << "Title of DVD, Date created, Producer:\n";
        cin.getline(names[i], 99);;
    }

    
    ifstream inFile;
    ofstream outFile;
    char response;
    inFile.open("CD.txt");
    
    if (!inFile.fail()) {
        cout << "A file by the name CD.txt exists.\n"
        << "Do you want to continue and overwrite it\n"
        << " with the new data (y or n): ";
        cin >> response;
        if (tolower(response) == 'n') {
            cout << "The existing file will not be overwritten." << endl;
            exit(1);
        }
    }
    outFile.open("CD.txt");
    if (inFile.fail()) {
        cout << "\nThe file was not successfully opened" << endl;
        exit(1);
    }
    
    cout << "The file has been succesfully opened for writing." << endl;
    
    
    ofstream myfile ("CD.txt");
    if (myfile.is_open())
    {
        myfile << char names [5][100] << endl;
        myfile.close();
    }
    else cout << "Unable to open file";
    
    
    
    return 0;
}
Last edited on
When posting code, please use code tags to make it easier to read.
or not sure if it is actually storing it


you could open your text file and check :)
Topic archived. No new replies allowed.