Fedora Linux Support Community & Resources Center
  #1  
Old 27th April 2010, 03:34 AM
The Mishanator Offline
Registered User
 
Join Date: Aug 2009
Location: Washington (state)
Age: 20
Posts: 184
linuxfedorafirefox
naming spawned copies of files.

i have created a header that allows me to spawn copies of a file a given number of times with a random new name. however, the name it gives the files is not really controlled by anything i do and i don't understand where it comes from. here is the code for spawn.h:
Code:
#include <fstream>
using namespace std;

bool copy_paste(const char SRC[], const char DEST[])
{
	ifstream src;
	ofstream dest;
	src.open(SRC, ios::binary);
	dest.open(DEST, ios::binary);        
	dest << src.rdbuf();
	dest.close();
	src.close();
}

void spawn(const char SRC[], int copies)
{
	for(int i = 0; i != copies + 1; i++)
	{
		const char newName[] = {i};
		copy_paste(SRC, newName);
	}
}
so i would use it in something like
Code:
#include "spawn.h"
int main()
{
	spawn("somefile.extension", 5);
}
this is a very basic version of what i want the end to be so i know there are some redundant pieces. would someone please tell me how to control the name of the output file... i want it to increment (for example: a, b, c, ...) and have the same potential extension.

thanks in advance
Reply With Quote
  #2  
Old 27th April 2010, 09:27 AM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: naming spawned copies of files.

It is incorrect to say that the name of the file is not controlled by anything you do. It is controlled by this conversion of an int to a char
Code:
...
		const char newName[] = {i};
...

that is, you take an int and output its ASCII value. Now, for low positive integers, that means a control character, so I guess you're seeing quite strange file names....

To give it a proper name, you've got to convert the number to a string. sprintf is a way to do it:

Code:
char str[10];
sprintf(str, "%d", i);
This is C. I'm sure you'll easily figure out how this translates to C++

Last edited by giulix; 27th April 2010 at 09:35 AM.
Reply With Quote
  #3  
Old 28th April 2010, 01:21 AM
The Mishanator Offline
Registered User
 
Join Date: Aug 2009
Location: Washington (state)
Age: 20
Posts: 184
linuxfedorafirefox
Re: naming spawned copies of files.

part of the problem i think is that i and newName must be chars. only i don't know how to increment "const char" in a for loop.

by not controlled, i meant that it doesnt behave in the way i want it to.

---------- Post added at 05:21 PM CDT ---------- Previous post was at 03:01 PM CDT ----------

also i was trying to compile this code in windoze and it wouldnt compile
Code:
bool copy_paste(const char SRC[], const char DEST[])
so i had to change it to
Code:
void copy_paste(const char SRC[], const char DEST[])
why could this be?

Last edited by The Mishanator; 27th April 2010 at 11:09 PM.
Reply With Quote
  #4  
Old 28th April 2010, 08:26 AM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: naming spawned copies of files.

No idea, I despise windoze. As for your other question, you increment the integer as you already do, but when it's time to construct the file name you convert the integer to a string, like the example provided.
Reply With Quote
  #5  
Old 28th April 2010, 04:54 PM
The Mishanator Offline
Registered User
 
Join Date: Aug 2009
Location: Washington (state)
Age: 20
Posts: 184
linuxfedorafirefox
Re: naming spawned copies of files.

yes, it's sort of inconvenient.. it compiles just fine (no warnings even) with gcc. but windows gets 66 errors or so.
Reply With Quote
  #6  
Old 28th April 2010, 09:45 PM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: naming spawned copies of files.

Well, add another 6 and you get the idea where they're heading to...
Reply With Quote
  #7  
Old 28th April 2010, 10:40 PM
The Mishanator Offline
Registered User
 
Join Date: Aug 2009
Location: Washington (state)
Age: 20
Posts: 184
linuxfedorafirefox
Re: naming spawned copies of files.

hahaaha. yes, linux FTW
Reply With Quote
Reply

Tags
copies, files, naming, spawned

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
making dvd copies iamroot Linux Chat 1 15th August 2006 11:23 AM
Thumb Drive: Copies Files....Sometimes. Flyboy917 Using Fedora 4 14th December 2005 02:02 PM
GRIP -manually naming files and editing tags carl h Using Fedora 0 15th September 2005 08:50 PM


Current GMT-time: 08:50 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat