Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ File I/O Program

46 views
Skip to first unread message

arnuld

unread,
Jan 21, 2018, 11:44:01 PM1/21/18
to
AIM: To copy a file
PROBLEM: None

This runs fine, can this be improved. e.g getting rid of while() ?


#include <iostream>
#include <fstream>

int main()
{
char ch;
std::ifstream ifile("tree.c");
std::ofstream ofile("sample.c");

while(ifile.get(ch))
ofile.put(ch);

if(!ifile.eof() || !ofile)
std::cerr << "Something Happened\n";

return 0;
}



--
-- arnuld
http://lispmachine.wordpress.com/

Ian Collins

unread,
Jan 21, 2018, 11:52:18 PM1/21/18
to
On 01/22/2018 05:43 PM, arnuld wrote:
> AIM: To copy a file
> PROBLEM: None
>
> This runs fine, can this be improved. e.g getting rid of while() ?
>
>
> #include <iostream>
> #include <fstream>
>
> int main()
> {
> char ch;
> std::ifstream ifile("tree.c");
> std::ofstream ofile("sample.c");

ofile << ifile.rdbuf();

return 0;
}
> while(ifile.get(ch))
> ofile.put(ch);
>
> if(!ifile.eof() || !ofile)
> std::cerr << "Something Happened\n";
>
> return 0;
> }

--
Ian.

Real Troll

unread,
Jan 22, 2018, 12:07:14 AM1/22/18
to
On 22/01/2018 04:43, arnuld wrote:
> AIM: To copy a file
> PROBLEM: None
>
> This runs fine, can this be improved. e.g getting rid of while() ?
>
>
> #include <iostream>
> #include <fstream>
>
> int main()
> {
> char ch;
> std::ifstream ifile("tree.c");
> std::ofstream ofile("sample.c");
>
> while(ifile.get(ch))
> ofile.put(ch);
>
> if(!ifile.eof() || !ofile)
> std::cerr << "Something Happened\n";
>
> return 0;
> }
>
>
>

You can use binary mode to handle all kinds of file. See this standard
Microsoft example Program:

<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
#include <iostream>
#include <fstream>

const static int BUF_SIZE = 4096;

using std::ios_base;

int main(int argc, char** argv) {

std::ifstream in(argv[1],
ios_base::in | ios_base::binary); // Use binary mode so we can
std::ofstream out(argv[2], // handle all kinds of file
ios_base::out | ios_base::binary); // content.

// Make sure the streams opened okay...

char buf[BUF_SIZE];

do {
in.read(&buf[0], BUF_SIZE); // Read at most n bytes into
out.write(&buf[0], in.gcount()); // buf, then write the buf to
} while (in.gcount() > 0); // the output.

// Check streams for problems...

in.close();
out.close();
}
<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>


You need to close the stream to flush out the buffer otherwise you lose
data.

After you've compiled the file, you simply run at the command prompt
like this:

mycopy tree.c sample.c

mycopy is the name of the executable,
tree.c is your source file;
sample.c is your destination file;





arnuld

unread,
Jan 22, 2018, 8:20:07 AM1/22/18
to
> On Mon, 22 Jan 2018 17:52:02 +1300, Ian Collins wrote:

> ofile << ifile.rdbuf();

Wow, so simple :) . Will use this.

End of section 15.14.3 of C++ Standard Library 2/e by Josuttis says this:

"Note that a stream buffer has no error state of its own. It also has no
knowledge of the input or the output stream that might connect to it. So,
calling:

out << in.rdbuf();

can't change the error state of \in\ due to a failure or end-of-file."


Is this a cause of concern ?

Juha Nieminen

unread,
Jan 23, 2018, 2:11:16 AM1/23/18
to
arnuld <sun...@invalid.address> wrote:
> AIM: To copy a file

You could use filesystem::copy_file() in the new TS.
Chances are that it will be more efficient than anything
you could implement (at least with standard C++).

Juha Nieminen

unread,
Jan 23, 2018, 2:15:56 AM1/23/18
to
Juha Nieminen <nos...@thanks.invalid> wrote:
> arnuld <sun...@invalid.address> wrote:
>> AIM: To copy a file
>
> You could use filesystem::copy_file() in the new TS.

I meant to say C++17. I don't know why I wrote "TS". It's not a TS anymore.
0 new messages