patterncppMinor
Universal File Duplicator
Viewed 0 times
universalfileduplicator
Problem
I made a simple Universal-File-Duplicator (Example: make 125 duplicates of one file).
Very useful if you want to fill a whole USB flash drive or an old harddisk with an important file (Example: Bitcoin wallet.dat or privatekey) and you don't want to hit Ctrl+V all the time...
Suggestions? Improvements?
Very useful if you want to fill a whole USB flash drive or an old harddisk with an important file (Example: Bitcoin wallet.dat or privatekey) and you don't want to hit Ctrl+V all the time...
Suggestions? Improvements?
#include "stdafx.h"
#include "stdio.h"
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
char nummer[1000000], sourcefile[255], targetfile[255], targetfileneu[255], endung[255];
int i = 0;
int anzahl = 0;
ifstream Quelldatei;
printf("Welcome! You are using UNIVERSAL-DATEI-DUPLIKATOR v1.0\n");
printf("Filename? (Example: test.txt)\n");
gets(sourcefile);
printf("New Filename without ending? (Example: filename)\n");
gets(targetfile);
printf("Ending? (Example: .txt)\n");
gets(endung);
printf("How many times do you want to create the file?\n");
scanf("%d", &anzahl);
for (i = 0; i = 0) { Zieldatei.put(c); }
}
memset(&targetfileneu[0], 0, sizeof(targetfileneu));
Quelldatei.close();
Zieldatei.close();
}
printf("SUCCESS!\n");
system("pause");
return 0;
}Solution
You've got a
Opening the input file only once and resetting the stream to the beginning for each copy may allow the stream to make better use of buffering, reducing the number of seeks required.
Also, streams can be copied directly to
using namespace std; in your code. Don't do that. (No, really).gets is dangerous; don't use it. Streams are so much more convenient anyway! For instance, you could write:std::string sourcefile, targetfile, endung;
std::cout > sourcefile;
std::cout > targetfile;
std::cout > endung;
int anzahl;
std::cout > anzahl;Opening the input file only once and resetting the stream to the beginning for each copy may allow the stream to make better use of buffering, reducing the number of seeks required.
Also, streams can be copied directly to
streambufs using operator>>.std::ifstream Quelldatei(sourcefile, std::ios::binary);
for (int i = 0; i > Zieldatei.rdbuf();
Zieldatei.close();
}
std::cout << "SUCCESS!" << std::endl;
system("pause");Code Snippets
std::string sourcefile, targetfile, endung;
std::cout << "Welcome! You are using UNIVERSAL-DATEI-DUPLIKATOR v1.0\n"
<< "Filename? (Example: test.txt)\n";
std::cin >> sourcefile;
std::cout << "New Filename without ending? (Example: filename)\n";
std::cin >> targetfile;
std::cout << "Ending? (Example: .txt)\n";
std::cin >> endung;
int anzahl;
std::cout << "How many times do you want to create the file?\n";
std::cin >> anzahl;std::ifstream Quelldatei(sourcefile, std::ios::binary);
for (int i = 0; i < anzahl; ++i) {
Quelldatei.seekg(0);
std::ostringstream output_filename(targetfile);
output_filename << i << endung;
std::ofstream Zieldatei(output_filename, std::ios::binary);
Quelldatei >> Zieldatei.rdbuf();
Zieldatei.close();
}
std::cout << "SUCCESS!" << std::endl;
system("pause");Context
StackExchange Code Review Q#37728, answer score: 3
Revisions (0)
No revisions yet.