Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 7

Character I/O for C++?

So, I have a project that is baffling me. The code I've written works right for the most part. Input is taken from a designated file, lines are numbered, all of that run correctly. However, I cannot seem to understand how to have what is displayed on screen for the user sent to a designated file to be saved. Any input or any help to understand this would be greatly appreciated, as I have about 8 other projects to work on.

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

ifstream in_stream;

ofstream out_stream;

int main ()

{

char next;

int line = 1;

char in_file_name[16], out_file_name[16];

cout << "Please enter filename to read from: ";

cin >> in_file_name;

in_stream.open (in_file_name);

if (in_stream.fail ())

{

cout << "Input file opening failed.\n";

system ("pause");

exit (1);

}

cout << "Please enter file name to save to: ";

cin >> out_file_name;

if (out_stream.fail())

{

cout << "Output file opening failed.\n";

system ("pause");

exit(1);

}

cout << "It worked!\n";

output not saving to blank file

cout << line << setw(3);

in_stream.get(next);

out_stream.put(next);

while (! in_stream.eof())

{

cout << next;

if (next == '\n')

{

line++;

cout << line << ":" << setw(3);

}

in_stream.get(next);

out_stream.put(next);

}

cout << endl;

in_stream.close ();

out_stream.close ();

return 0;

}

Update:

And so I haven't! Thank you for pinpointing my retardation!

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    Er... you didn't open the output file.

    out_stream.open (out_file_name);

Still have questions? Get your answers by asking now.