fstream won't write to file

Hello there, I'm trying to write a program that reads user name, ID and puts them in a file then reads them. Mine doesn't seem to be doing that though. I want it to write the user data from [0] and put it in [1] by putting it into the file first then reading it. What am I doing wrong? Compiles fine but file is 0 bytes.


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  
#include <iostream>
#include <fstream>


struct User
{
    int ID;
    char Name[20];
};

int vMemoryAlloc(User *pUsers[])
{
    int iNumUsers = 0;

    pUsers[iNumUsers] = new User{-1,"temp"};





    std::cout << "Enter name or -1 to quit" << std::endl;
    std::cin.getline(pUsers[iNumUsers]->Name, 20);

    while (pUsers[iNumUsers]->Name[0] != '-')
    {
        std::cout << "Enter ID" << std::endl;
        std::cin >> (*pUsers[iNumUsers]).ID; 
        iNumUsers++;
        std::cout << "Enter name or -1 to quit" << std::endl;
        pUsers[iNumUsers] = new User;
        std::cin.ignore();
        std::cin.getline(pUsers[iNumUsers]->Name, 20);

    }



    return iNumUsers;
}


int main()
{
    User *pUsers[50];
    int iNum = vMemoryAlloc(pUsers);


    std::fstream file("C:/Users/Privacy/Desktop/Info.data", std::ios::in |std::ios::out | std::ios::trunc);

    file.seekg(0);
    file.seekp(0); 

    file.write((char *)pUsers[0], sizeof(pUsers[0]));
    file.read((char *)pUsers[1], sizeof(pUsers[1]));



    std::cout << (*pUsers[1]).Name;






    for (int i = 0; i <= iNum; i++)
        delete pUsers[i];


    std::cin.ignore();
    getchar();
    return 0;
}

The data might be written only after an explicit flush or when the file is closed. Thus you will see the data only when you program ends completely since you do not an explicit flush or close.
Topic archived. No new replies allowed.