Read() and Write() Functions

I'm writing a program where I have to use read() and write() to duplicate the content of an input file, such as NotesBetweenTwoSisters1.txt, to an output file named outputFile.dat. The content and format of the output file has to be exactly the same with those of the input file.

I'm still new to C++ and I'm having trouble on how to use these functions. if anyone can help me out on this or give me a hint, I would appreciate it.

NotesBetweenTwoSisters1.txt
1
2
3
4
5
6
Madam, 

Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon. 
But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip. 

Sis  


Code In Progress...
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Class to define the properties
class Contestant
{
private:
    // Instance variables
    string p1;
    string p2;
    string p3;

public:
    // Function declaration of input() to input info
    int input();

    // Function declaration of output() to
    // extract info from file Data Base
    int output();
};

// Function definition of input() to input info
int Contestant::input()
{
    // Object to write in file
    ofstream file_obj;

    // Opening file in append mode
    file_obj.open("NotesBetweenTwoSisters1.txt", ios::app);

    // Object of class contestant to input data in file
    Contestant obj;

    // Feeding appropriate data in variables
    string str1 = "Madam,";

    // Assigning data into object
    obj.p1 = str1;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    // Feeding appropriate data in variables
    string str2 = "Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon. But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip";

     // Assigning data into object
    obj.p2 = str2;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    // Feeding appropriate data in variables
    string str3 = "Sis";

     // Assigning data into object
    obj.p3 = str3;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    return 0;
}

int Contestant::output()
{
    // Object to read from file
    ifstream file_obj;

    // Opening file in input mode
    file_obj.open("outputFile.dat", ios::in);

    // Object of class contestant to input data in file
    Contestant obj;

    // Reading from file into object "obj"
    file_obj.read((char*)&obj, sizeof(obj));

    string output;

    cout << output;
    return 0;
}

// Driver code
int main()
{
    // Creating object of the class
    Contestant object;

    // Inputting the data
    object.input();

    // Outputting the data
    object.output();

    return 0;
}


Expected Output
1
2
3
4
5
6
Madam, 

Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon. 
But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip. 

Sis  
Last edited on
So why do you have a function called input(), which
- opens files for output
- contains the strings you're meant to be reading from the file.

I would expect your main to be something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    // Creating object of the class
    Contestant object;

    // Inputting the data
    object.input("NotesBetweenTwoSisters1.txt");

    // Outputting the data
    object.output("outputFile.dat");

    return 0;
}

read and write are usually for binary files (text files are a subset of binary files, so that is not critical).

what you need to do:
get file size
allocate a buffer big enough for file size (huge files you can do in chunks)
then
file.read(buffer, size of file)
and file2.write(buffer, size of file)
not sizeof() but the actual file's size which you can get various ways.
@salem c, @jonnin; I updated my code and this is the output I got.

My Updated Code:
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream testFile("NotesBetweenTwoSisters1.txt", ios::binary);
    testFile.seekg(0, ios::end);
    int SIZE = testFile.tellg();

    cout << "File Size: " << SIZE << endl;
    char data[SIZE];
    fstream file;

    file.open("outputFile.dat", ios::out | ios::binary);

    cout << "Writing the characters to the file.\n";
    file.write(data, sizeof(data));

    file.close();

    file.open("NotesBetweenTwoSisters1.txt", ios::in | ios::binary);

    cout << "Now reading the data back into memory.\n\n";
    file.read(data, sizeof(data));

    for(int count = 0; count < SIZE; count++)
    {
        cout << data[count] << "";
    }

    cout << endl;

    file.close();

    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
File Size: 272
Writing the content to the file.
Now reading the data back into memory.

Madam,

Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon.
But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip.

Sis 
Last edited on
char data[SIZE]; this is a language extension and not technically legal. arrays have to have size known at compile - time, not run-time, in standard c++.

you didn't read the file, you just immediately start writing it.
sizeof(data) isn't the size of the array. use SIZE !!!

those 2 bugs mean outputfile.dat is a mess.

you then open a different file, read it and so on.

Go through the steps of the program one by one carefully and explain each one to an invisible child beside you who asked what it was all about. Look for the things like what I am saying where the flow/logic makes no sense.
Topic archived. No new replies allowed.