File management?

T

TempEcho

Is there any place where I can find some library / commands to use for file
management (e.g. renaming files , copying , deleting?). Thanks in advance!

T.E.
 
R

Rolf Magnus

TempEcho said:
Is there any place where I can find some library / commands to use for
file management (e.g. renaming files , copying , deleting?). Thanks in
advance!

For renaming, you can use std::rename() from <cstdio>, for removing
std::remove() from the same header. I don't think there is a standard
function for copying, but it's easy to roll your own.
 
A

abecedarian

Rolf Magnus wrote on Apr 27, 11:26 am:
I don't think there is a standard
function for copying, but it's easy to roll your own.

I don't think it's that easy. Show an implementation with full error
handling?!
 
R

Rolf Magnus

Rolf Magnus wrote on Apr 27, 11:26 am:

I don't think it's that easy. Show an implementation with full error
handling?!

#include <iostream>
#include <cctype>
#include <fstream>

int main(int argc, char* argv[])
{
if (argc < 3)
{
std::cerr << "Usage: " << argv[0] << " <infile> <outfile>\n";
return EXIT_FAILURE;
}

std::ifstream infile(argv[1], std::ios_base::binary);
std::eek:fstream outfile(argv[2], std::ios_base::binary);
char c;

while (infile.get(c) && outfile.put(c));

if (!outfile)
{
std::cerr << "Error writing output file\n";
return EXIT_FAILURE;
}

if (!infile.eof())
{
std::cerr << "Error reading input file\n";
return EXIT_FAILURE;
}
}
 
A

abecedarian

Rolf Magnus wrote on Apr 27, 5:17 pm
#include <iostream>
#include <cctype>
#include <fstream>

int main(int argc, char* argv[])
{
if (argc < 3)
{
std::cerr << "Usage: " << argv[0]
<< " <infile> <outfile>\n";
return EXIT_FAILURE;
}

std::ifstream infile(argv[1], std::ios_base::binary);
std::eek:fstream outfile(argv[2], std::ios_base::binary);
char c;

while (infile.get(c) && outfile.put(c));

if (!outfile)
{
std::cerr << "Error writing output file\n";
return EXIT_FAILURE;
}

if (!infile.eof())
{
std::cerr << "Error reading input file\n";
return EXIT_FAILURE;
}
}

- you don't check if outfile exists, you just destroy it.
- you don't close outfile, you let the destructor do it. If close()
fails (e.g. because the data cannot be flushed to disk) your program
reports 'success'. Definitely a bug.
- you don't perform any cleanup in case of error.
- 'while (infile.get(c) && outfile.put(c))' is very slow (not an error
but ...).
- ...

In the worst case you destroy an existing file, create a corrupted
(half-written) file with the same name and report success. It's not
that easy to ... . Not entirely your fault though. std::iostream is a
toy library not suitable for professional programming.

Abe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top