Write to binary file

Hey all,
my created function is supposed to get a user's input and write it to a binary file. But my binary file remains empty after this function is called. Any help?
Much 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
54
55
56
57
58
59
void createTask(fstream& datfile, char fileName[])
{
    createBinaryFile (datfile, fileName);

    datfile.open (fileName, ios::in | ios::binary);

     if (!datfile)
    {
        cout << "Binary file open for task creation failed!" << endl;
        return;
    }
    else
    {
      cout << "Binary file open for task creation ok!" << endl << endl;
        int enterCount; //variable to store number of tasks user wishes to enter

        task t;
        //create array first then transfer to binary file

        cout << "Creation of tasks" << endl << endl;

        cout << "How many tasks?: ";
        cin >> enterCount; //variable for user to enter how many tasks to create
        cout << endl << endl;
        //capture user entered data
        for (int i = 1; i <= enterCount; i++)
        {
            t.taskNo = i;
            cout << "Information for task " << t.taskNo << endl;
            cout << "Title for assessment: ";
            cin >> t.taskTitle;
            cout << "Weight (%): ";
            cin >> t.taskWeight;
            cout << "Full mark upon: ";
            cin >> t.maxMarks;
            cout << "Description: ";
            cin >> t.description;
            cout << endl;
            cout << setw(60) << setfill('.') << '.' << endl;
            cout << setfill(' ')<< endl;

            //write to binary file
            datfile.write (reinterpret_cast <const char *> (&t), sizeof (t));

        }
            cout << endl << endl;

            cout << "The creation of task file completed" << endl << endl;

            //close binary file
            datfile.close ();

            cout << "The summary of creation" << endl << endl;

            //function call for display all tasks
            displayAllTasks (datfile, fileName);
    }

}
Because on line 5 you open the file with 'ios::in'. Change that to 'ios::out'

See http://www.cplusplus.com/reference/iostream/fstream/fstream/ for more flags
Topic archived. No new replies allowed.