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;

}

Recommended Answers

All 6 Replies

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

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 + - / *

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

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

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() .

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

while(! in_stream.eof())
    {
        while(getline(in_stream,wrd))
        {
            out_stream<<wrd<<endl;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.