[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1003
  • Last Modified:

system() return values

I have a function that copies various files using SYSTEM(copyThisFile). When the functions attempts to copy, the cmd Prompt will either say "1 file(s) copied." or give an error. What I would like to do, if possible, is something like this:

if "1 file(s) copied" then suppress system message, successCounter++
else display "FileName failed to copy: " + "system failure message", failureCounter++

So if text1.txt copies successfully, the screen shows nothing. If text2.txt fails, the screen displays "text2.txt failed to copy: The source file and destination file are the same."
  • 2
1 Solution
evilrixSenior Software Engineer (Avast)Commented:
#include <io.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
void copy_file(std::string const & sOldPath, std::string const & sNewPath, bool bOverWrite = false)
	if(!bOverWrite && (0 == access(sNewPath.c_str(), 0x00))) { throw std::runtime_error("Output file already exists"); }
	std::ifstream ifs(sOldPath.c_str(), std::ios::binary);
	if(!ifs.is_open()) { throw std::runtime_error("Unable to open input file"); }
	std::ofstream ofs(sNewPath.c_str(), std::ios::trunc | std::ios::binary);
	if(!ofs.is_open()) { throw std::runtime_error("Unable to open output file"); }
	ifs.seekg(0, std::ios::end);
	std::ifstream::pos_type size = ifs.tellg();
	std::string sData(size, std::string::value_type());
	ifs.read(&sData[0], size);
	if(!ifs.good()) { throw std::runtime_error("Unable to read input file"); }
	ofs.write(sData.data(), size);
	if(!ifs.good()) { throw std::runtime_error("Unable to write output file"); }
int main()
	int nRetval = 0;
		std::string sOldPath = "c:\\temp\\test.txt";
		std::string sNewPath = "c:\\temp\\newtest.txt";
		copy_file(sOldPath, sNewPath);
		std::cout << "Copied " << sOldPath << " to " << sNewPath << " successfully." << std::endl;
	catch(std::exception const & ex)
		std::cerr <<"Error. " << ex.what() << std::endl;
		nRetval = -1;
#ifdef WIN32
	system("pause"); // Only works on Windows
	return nRetval;

Open in new window

evilrixSenior Software Engineer (Avast)Commented:
The code above was posted to your previous thread but then I saw you'd opened this one to discuss exactly the issue I raised in the previous thread. As you can see the code above will do the file copy for you programmatically. It has no reliance on system commands and as such (a) gives you better control and (b) is not platform specific.

NB. I've tested the code using my own test sample and it worked file; however, if you do decide to use this code you should test it thoroughly before putting it into a production environment.

>> if "1 file(s) copied" then suppress system message, successCounter++
Change line 43 to be "++ successCounter"

>> else display "FileName failed to copy: " + "system failure message", failureCounter++
Change line 47 - 48 to be "++ failureCounter"

Note, you should prefer pre-increment (++x) to post-increment (x++) as it's more efficient. The reason why is that for post increment the compiler must generate a temporary (it takes a copy, increments the value and the returns the copy), which not necessary in pre-increment (it increments and returns the incremented value). The truth is, this isn't really a problem with intrinsic types (such as int, long etc)  but it can be a big problem when working with user defined types. Basically, it's a good idea to get into the habit so you don't need to think about it. Only use post-increment when you specifically need those semantics.

I hope this helps.

RunBorisAuthor Commented:
I wish I had seen your solution prior to accepting the answer on the other question. Thanks!

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now