1,105,581 Community Members

write from file to file

Member Avatar
skannigan
Newbie Poster
7 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, i want to write from one file to another. i managed to get the file to write but not in the way i expected it too, there is more to the program but for now i just need to know how can i mirror one file...
My input file contains:
peter 3 / + - *
Dahne 8 + - / *

but my outfile saves as:
peter
3
/
+
-
*
...... and so on

how do i fix this? i need it to look exactly like the input file for now

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;
    in_stream.open("precedence.dat");
    if(in_stream.fail( ))
    {
        cout << "input file opening faild.\n";
        exit(1);
    }
    out_stream.open("results.dat");
    if (in_stream.fail( ))
    {
        cout << "output file opening failed.\n";
        exit(1);
    }
    //char line;
    string next;
    //in_stream.get(next);
    while(! in_stream.eof())
    {
    in_stream>>next;
    out_stream<< next<< endl;
    //in_stream.get(next);
    }
    in_stream.close( );
    out_stream.close( );
    return 0;

}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

out_stream<< next<< endl; -- what does endl do?

Member Avatar
skannigan
Newbie Poster
7 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

it ends the line after and moves the curser down to the next line. thats a good point, thank you.
now i removed the endl my out put is all in one line

peter 3 / + - *Dahne 8 + - / *

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

So where do you need the endl? How do you know when that spot is read?

Member Avatar
skannigan
Newbie Poster
7 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i dont know, im very new to this, i would have assumed by my text book that its reads dat files line by line and outputs it to the outfile, but it moves a line down every time it encounters a white space. ive tried using string variables but still nothing, either im getting one word per a line or one character per line.
the program itself has to have an input file that pre-filled. and a output file. from the input file it needs to extract the number and then do a switch for a calculation which ive done, then the outfile needs to save the name the /+*- symbols and the answer, my biggest hurdle is the file output, its not giving me the desired result

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Ahhh, now I see the problem. I didn't look close enough.

... but it moves a line down every time it encounters a white space.

That's the definition of a read using >>. If you want to read a line you need something like getline() .

Member Avatar
skannigan
Newbie Poster
7 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

YOU ARE AWESOME!!!! thank you perfect mirror or original file

while(! in_stream.eof())
    {
        while(getline(in_stream,wrd))
        {
            out_stream<<wrd<<endl;
        }
    }
Question Answered as of 2 Years Ago by WaltP
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: