I need help please! This lab is due at midnight

I was absent when our instructor taught us how to do the second part of this assignment, and I am completely lost. It is due tonight at midnight, if someone could please just show me how to do it, I would appreciate it SO much! I will go back and learn all the specifics of it later... I just need to get this finished before tonight. The first part of the lab is

Write a program that opens two text files (input1.txt and input2.txt) for
input .The program should write the followings in the two files. input1.txt:

This is the first line in input1.txt.
This is the second line in input1.txt.
This is the third line in input1.txt.
This is the fourth line in input1.txt.

input2.txt:

This is the first line in input2.txt.
This is the second line in input2.txt.
This is the third line in input2.txt.

The second part of the assignment is Read the contents from the two files created in lab 6 and write into a single file. First you should write the contents of first file and then the second file. This is the part that I really need help with. Here is my code for the first part.

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	using namespace std;
	string input1;
	
	ofstream fout("input1.txt");
	fout<<"This is the first line in input1.txt.\n";
	cout<<"The first line was added to input1"<<endl;
	fout<<"This is the second line in input1.txt.\n";
	cout<<"The second line was added to input1"<<endl;
	fout<<"This is the third line in input1.txt.\n";
	cout<<"The third line was added to input1"<<endl;
	fout<<"This is the fourth line in input1.txt.\n";
	cout<<"The fourth line was added to input1"<<endl;
fout.close();

fout.open("input2.txt");
fout<<"This is the first line in input2.txt.\n";
cout<<"The first line was added to input2"<<endl;
fout<<"This is the second line in input2.txt."<<endl;
cout<<"The second line was added to input2"<<endl;
fout<<"This is the third line in input2.txt."<<endl;
cout<<"The third line was added to input2"<<endl;
fout.close();	

cin.get();
cin.get();
	return 0;
}
Consider a file like a jar of jellybeans.

You have a jar named "input1.txt" (which is odd, since you are writing to that file, not reading). You drop a whole bunch of jellybeans into that file and close it (lines 10-19).

You have another jar named "input2.txt" (same caveat as above). You drop a whole bunch of other jellybeans into that file and close it (lines 21-28).

Now you have two jars of jellybeans.

Suppose I give you a third jar (which I'll name "output3.txt" -- I have no idea what your assignment want's you to name it).

Assuming I don't eat any of the jellybeans you give me, how are we going to get the jellybeans from your jars into mine?

Start with the jar named "input1.txt". Open it, and take out a jellybean. Put that jellybean in the "output3.txt" jar. Take another jellybean from the "input1.txt" jar and put it in the "output3.txt" jar. Repeat until there are no more jellybeans in the "input1.txt" jar.

Now open the jar named "input2.txt". Take a jellybean and put it in the "output3.txt" jar. Keep doing that until the "input2.txt" jar is empty.

Now all the jellybeans are in the "output3.txt" jar. Close it up and you're done.

Hope this helps.

Now I'm off to eat my jar of jellybeans. Thanks!
Last edited on
This post here is mostly giving you the answer, so I recommend trying to figure it out yourself through Duoas's reply first. If you can't figure it out, this can be used as a hint.

First of all, you'll need to use a std::ifstream object.
You start of by creating one of those objects, which would be done like this:

ifstream fin("input1.txt");

The parameter of the constructor is obviously which file you want to read information from. You can also add more parameters later on, although these aren't necessary for reading such simple files.

Now that we've got our std::ifstream object, we have multiple ways we can tackle this problem. The solution I am showing you is really simple, although it could be something your class hasn't learned yet.

Instead of manually parsing every line, we could just create a std::stringstream object which handles all of our reading.

stringstream buffer;

This will also bring up another dependency. Include the "sstream" file.

#include <sstream>

Now, we should put all of the contents of the std::ifstream object into our std::stringstream object. You can treat std::stringstream just like you treat any other stream.

You can use the rdbuf() function in order to get the contents of the file. This would be typed like this:

buffer << fin.rdbuf();

Now close the std::ifstream and open it for the other file.

1
2
fin.close();
fin.open("input2.txt");


Repeat the process.

1
2
buffer << fin.rdbuf();
fin.close();


Now you need to open your std::ofstream object again. I am guessing the desired name of your file is "output.txt", and therefore I opened the std::ofstream for that file.

fout.open("output.txt");

Now we'll want to put all of the contents of our std::stringstream into the std::ofstream object. This can, once again, be done with rdbuf().

1
2
fout << buffer.rdbuf();
fout.close();


And now it should be working. I tested it myself, and it seems to work.
Sirolu wrote:
And now it should be working. I tested it myself, and it seems to work.
I'm afraid it doesn't. It ignores the line ordering requirement of the assignment.
Last edited on
cire wrote:
I'm afraid it doesn't. It ignores the line ordering requirement of the assignment.

It doesn't? I read this discussion as "read the two files and concatenate them into one", and it gave me a concatenated version of it, exactly as I thought.

EDIT: This is the result it gave me:
This is the first line in input1.txt.
This is the second line in input1.txt.
This is the third line in input1.txt.
This is the fourth line in input1.txt.
This is the first line in input2.txt.
This is the second line in input2.txt.
This is the third line in input2.txt.



EDIT 2: Anyway, to the creator of this discussion; if this is not the result you wanted for your file, I guess you should just discard my entire solution.
Last edited on
It doesn't? I read this discussion as "read the two files and concatenate them into two", and it gave me a concatenated version of it, exactly as I thought.


Apparently I didn't read the original post right. You are correct. ;)
Topic archived. No new replies allowed.