file open, and close

can i open a file in both read and write mode at the same time like this:
1
2
3
4
5
6
7
8
9
ofstream outputfile;
    outputfile.open("Dbase.txt");
    ifstream infile;
    infile.open("Dbase.txt");
    if (!infile)
              {  
                 cout << "Open of output file failed" << endl;
                 exit(1);
              }
Last edited on
Merge these two together...or not.:

read:

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char filename[256];
    cout << "Enter the name of file you want to open:";
    cin.getline(filename,256);

    for(int s = 0;s != '\n';s++)
    {
        if(filename[s] == NULL)
        {
            filename[s] = '.';
            filename[s + 1] = 't';
            filename[s + 2] = 'x';
            filename[s + 3] = 't';
            filename[s + 4] = NULL;
            break;
        }
    }

    cout << "Opening and reading contents of '" << filename << "'" << endl;

    ifstream File(filename);

    for(int n = 1;;n++)
    {
        char object[256];
        File.getline(object,256);

        if(File.fail())
        {
            break;
        }

        cout << n << " - " << object << endl;
    }

    system("PAUSE");
    return 0;
}


write:
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;

class Person
{
    public:
    char FirstName[256];
    char LastName[256];
    char phone[256];
    char address[256];
    char email[256];
};

Person getPerson()
{
    Person person;
    ofstream data;

    cout << "\nEnter another Person\n"
    << "First Name: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.FirstName;
    data << "First Name: " << person.FirstName;
    data << "\n";
    data.close();

    cout << "Last Name: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.LastName;
    data << "Last Name: " << person.LastName;
    data << "\n";
    data.close();

    cout << "Phone number: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin >> person.phone;
    data << "Phone: " << person.phone;
    data << "\n";
    data.close();

    cin.getline(person.address,256);
    cout << "Address: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin.getline(person.address,256);
    data << "Address: " << person.address;
    data << "\n";
    data.close();

    cout << "Email: ";
    data.open("Info.txt", fstream::in|fstream::app);
    cin.getline(person.email,256);
    data << "Email: " << person.email;
    data << "\n ---------------------------------------------------------------------------------------------------------------------------\n" << endl;
    data.close();

    return person;
}

int getPeople(Person people[], int nMaxSize)
{
    int index;
    for(index = 0; index < nMaxSize; index++)
    {
        char cAnswer;
        cout << "Enter another person? (Y or N): ";
        cin >> cAnswer;

        if(cAnswer != 'Y' && cAnswer != 'y')
        {
            break;
        }

        people[index] = getPerson();
    }
    return index;
}

void displayPerson(Person person)
{
    cout << "Name: " << person.FirstName << " " << person.LastName << endl;
    cout << "Phone number: " << person.phone << endl;
    cout << "Address: " << person.address<< endl;
    cout << "Email: " << person.email << endl;
    cout << "-----------------------------------------------------------\n" << endl;
}

void displayPeople(Person people[], int nCount)
{
    for(int index = 0;index < nCount; index++)
    {
        displayPerson(people[index]);
    }
}

int main(int nNumberofArgs,char* pszArgs[])
{
    Person people[256];

    cout << "Hello!, this is a program that stores: Name, Phone, Address, and email." << endl;
    cout << "It will store it temporarily in this console while" << endl;
    cout << "storing it in 'info.txt'." << endl;

    int count = getPeople(people, 256);

    cout << endl << "Here is what your entered: " << endl << endl;
    displayPeople(people,count);

    system("PAUSE");
    return 0;
}


as you can see, the writing needs class, be careful. Remember to study classes in your c plus plus class at school too. See what I did there?
InfinityCounter wrote:
can i open a file in both read and write at the same time


there is fstream which you can use to read and write at the same time. You just need to specify ios_base flag.
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream iofile("Dbase.txt", ios::in | ios::out);

    if (!iofile)
    {
        //create an empty file for next run
        cout << "Dbase.txt not found.\n";
        iofile.open("Dbase.txt", ios::out);
        iofile.close();

        cout << "Dbase.txt was created. Please run the program again.\n";
        cin.get();
        return 1;
    }

    /* do stuff */
    int i;
    char temp[20];

    //output to Dbase.txt some content
    iofile << 300 << " " << "John" << endl;
    iofile << 200 << " " << "Adam" << endl;
    iofile << 380 << " " << "Tracy" << endl;

    //rewind the stream
    iofile.clear();
    iofile.seekg(0);

    //read Dbase.txt's content
    while (iofile >> i >> temp)
        cout << i << " " << temp << endl;


    iofile.close();
    return 0;
}


output (1st run):
Dbase.txt not found.
Dbase.txt was created. Please run the program again.


Process returned 1 (0x1)   execution time : 1.919 s


output (2nd run):
300 John
200 Adam
380 Tracy

Process returned 0 (0x0)   execution time : 0.062 s


...output (nth run):
300 John
200 Adam
380 Tracy

Process returned 0 (0x0)   execution time : 0.062 s


if you want to keep the previous data then the flag is ios::in | ios::out | ios::app, so the 3rd time you run this program it will output
300 John
200 Adam
380 Tracy
300 John
200 Adam
380 Tracy

Process returned 0 (0x0)   execution time : 0.062 s
Last edited on
Thanks guys, greenleaf800073 i don't take programming classes i'm learning on my own.
@InfinityCounter:
Just for curiosity, what grade are you?
Topic archived. No new replies allowed.