Need help with I/O String File Formatting

Hi everyone, I was hoping you could all help me. My assignment is to take this text file "Lincoln.txt"

" The Gettysburg Address
Fourscore and seven years ago
our fathers brought forth on
this continent a new nation,
conceived in liberty, and dedicated to
the proposition that all men are created equal.
Now we are engaged in a great civil war, testing
whether that
nation, or any nation so conceived and so dedicated,

can long endure.
We are met on a great battlefield of that war.
We have come to dedicate a portion of that field as a final resting-place for those
who here gave their lives that the nation might live. It is altogether


fitting and proper that we should do this. But, in a larger sense, we
cannot dedicate, we cannot
consecrate, we cannot hallow, this ground.
The brave men, living and dead, who struggled here have consecrated
it, far above our poor power to add or detract. The world will little
note, nor long remember, what we say here, but it can never forget
what they did here. It is for us

the living, rather, to be dedicated
here to the unfinished work which they who fought here have thus far
so nobly advanced.
It is rather for us to be here dedicated to the
great task remaining before us - that from these honored dead we take
increased devotion to that cause for which they gave the last full
measure

of devotion - that we here highly resolve that these dead
shall not have died

in vain - that this nation, under God, shall have
a new birth of freedom and that government of the people, by the
people, for the people, shall not perish from the earth."

Formatted like that, and remove all the extra white spaces and that lines are set to a given length set by the user.

The file should eventually look like this

"The Gettysburg Address Fourscore and
seven years ago our fathers brought
forth on this continent a new nation,
conceived in liberty, and dedicated to
the proposition that all men are created
equal. Now we are engaged in a great
civil war, testing whether that nation,
or any nation so conceived and so
dedicated, can long endure. We are met
on a great battlefield of that war. We
have come to dedicate a portion of that
field as a final resting-place for those
who here gave their lives that the
nation might live. It is altogether
fitting and proper that we should do
this. But, in a larger sense, we cannot
dedicate, we cannot consecrate, we
cannot hallow, this ground. The brave
men, living and dead, who struggled here
have consecrated it, far above our poor
power to add or detract. The world will
little note, nor long remember, what we
say here, but it can never forget what
they did here. It is for us the living,
rather, to be dedicated here to the
unfinished work which they who fought
here have thus far so nobly advanced. It
is rather for us to be here dedicated to
the great task remaining before us -
that from these honored dead we take
increased devotion to that cause for
which they gave the last full measure of
devotion - that we here highly resolve
that these dead shall not have died in
vain - that this nation, under God,
shall have a new birth of freedom and
that government of the people, by the
people, for the people, shall not perish
from the earth. "

Here's what I have so far"

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>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
	string unformatted;
	string formatted;
	string line;
	cout << "Input the name of the unformatted file: " << endl;
	cin >> unformatted;
	cout << "Where would you like the formatted file to be written?" << endl;
	cin >> formatted;
	ifstream myfile (unformatted);
	if (myfile.is_open())
	{
		cout << "File was successfully read" << endl;
	}
	else
	{
		cerr << "File could not be read" << endl;
	}
	ofstream writeFile (formatted);
	while (getline (myfile, unformatted) )
	{
		writeFile << std::skipws << unformatted << endl;
	} 
	myfile.close();

	return 0;
}


Any help is appreciated. Thank you, and apologies if this is an easy assignment...I'm new.
closed account (Dy7SLyTq)
just to make you feel better: i have this card trick where i have the person pick a card then i split the deck in two and have them put it between the two. i look at the bottom card of the upper half of the deck so i have a reference when looking for the card. ie its a very simple trick only when you learn it. this program is like that. im not going to write your code for you, but i will give you a general idea. read in the file and store it in a container of strings (vector, array, etc). then what you want to do is write a function to strip all extra white space include new lines. then put it all in a stringstream and output it based on a line size that is set by you
All that is required is a single pass through the the white-space delimited tokens in the file. We don't need a container to store them in; once we have printed out a line, we don't need the tokens that formed the line any more.

Read white-space delimited tokens from the file one by one, add each token, separated by a space to a line of text. When the line gets long enough, print it out, reset the line to an empty line, and continue in this manner till end of file.

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
#include <iostream>
#include <sstream>

void pretty_print( std::istream& stm, std::size_t col_width )
{
    if( col_width < 20 ) return ;

    std::string line ;
    std::string token ;

    while( stm >> token ) // for each ws delimited token
    {
        auto required = token.size() + 1 ;
        if( ( line.size() + required ) > col_width ) // the next token won't fit
        {
            std::cout << line << '\n' ; // print the line
            line.clear() ; // make it an empty line
        }
        if( !line.empty() ) line += ' ' ; // add a space to seperate the tokens
        line += token ;
    }
    if( !line.empty() ) std::cout << line << '\n' ; // print the last line
}

int main()
{
    std::istringstream stm { R"(
        The Gettysburg Address
        Fourscore and seven years ago
        our fathers brought forth on
        this continent a new nation,
        conceived in liberty, and dedicated to
        the proposition that all men are created equal.
        Now we are engaged in a great civil war, testing
        whether that
        nation, or any nation so conceived and so dedicated,

        can long endure.
        We are met on a great battlefield of that war.
        We have come to dedicate a portion of that field as a final resting-place for those
        who here gave their lives that the nation might live.
    )" } ;

    pretty_print( stm, 40 ) ;
}

http://coliru.stacked-crooked.com/a/d4fc261c9acb3fa4
You might also look at this post
http://www.cplusplus.com/forum/beginner/113065/
Topic archived. No new replies allowed.