473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

File management?

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.
Jul 23 '05 #1
4 2354
TempEcho wrote:
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.

Jul 23 '05 #2
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?!

Jul 23 '05 #3
ab*********@spambob.com wrote:
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?!


#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::ofstream 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;
}
}

Jul 23 '05 #4
Rolf Magnus wrote on Apr 27, 5:17 pm
abecedar...@spambob.com wrote:
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::ofstream 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

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Scott Abel | last post by:
For immediate release: The Rockley Group Content Management Workshop Series Coming to Atlanta, Seattle, Vancouver, Chicago, Washington, DC, Toronto, and Research Triangle Park Learn more:...
1
by: serge calderara | last post by:
dear all, I have problem accessing section group in my configuration application file. I got an error saying thta I can have only one section ???? here is my application configuration looks...
6
by: Codemonkey | last post by:
Hi, I have a few questions about best practices when it comes to the management of temporary files. Any thoughts anyone can give would be much appreciated. Basically, I'm writing a document...
4
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an...
3
by: kimimaro | last post by:
hi below is my save function that is used to placed data from the C program to a text file for future usage. void save() { FILE *save; int i = 0; save=fopen("employeerecord.txt", "a+");
4
by: Richard | last post by:
Hi all, Is there any class in .NET 1.x that will help me set file & folder permissions for a user? Aka - I would like to do the in code equivalent of right clicking a folder in file explorer...
1
by: PK9 | last post by:
I'm building a windows app using C#. The goal is to merge portions of multiple xml files into one. I currently have an .xsl stylesheet that pulls in the required sections of multiple xml files...
1
by: Anonieko | last post by:
Logging To a File In A S P . N E T 2 . 0 . I also want weekly rolling filename for the log file. Step 1. Create a class library ( 2 classes) ============================== AppWebEvent.cs...
3
by: Freddie | last post by:
hi, i try to get the asp.net 2.0 security controls to work with SQL Server 2005 Dev RTM, this is my connetion string in mashine.config <connectionStrings> <add name="LocalSqlServer"...
0
by: Xaradas | last post by:
Hi, I'm writing here to ask if some one know a free software developed with PHP&MySQL for web files management. The basic requirements are: - User Management - File Permission Management...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.