i have creaed a loop that lets the user creates a file then copy a file to it and also allows the user to add more than 1 file to be copied. the problem that i found out is that when the progam goes back round the loop it drops what ever is in the pointer and only copies the recent selection. is there a solution to this ?


for example

while (1)
	{	
	
	printf("please type in the path for the file you want to archive\n");
	scanf("%s",&original_file);
	open_pointer = fopen( original_file, "r+b");								 
	original_pointer = fopen(original_file,"a+b");							
	copy_pointer = fopen( archive_name,"wa+b");
	
	if (copy_pointer != NULL)
	{
		printf("\n enter any charachter appart from x to add other files to be copied");
		puts("\n or enter  x to coninue");
        scanf("%s",&answer);
	if(answer != 'x')
		continue;
		}
		else
		{
		if( answer == 'x')
		break;
		}
		
		

	
	while ((n = fread(buffer,1,sizeof buffer, original_pointer)) > 0)
	{
	
	  
		fwrite(buffer,1, n , copy_pointer); //fwrite only writes what is curently in the pointer

Recommended Answers

All 7 Replies

"wa+b" isn't a legal open mode. Most likely your compiler is interpreting it as "w" rather than "a+b". Any open mode with a base of "w" will truncate the destination file upon opening it.

yeah i corrected that thanks is there a way of storing whats in the loop for example if i copied 2 files store thr 2 copied files in a temp plac the right it out to the archive file becase at the moment the program just drops the first file that is open and only copies the second file that is selected and does not copy the first one.

Or you could just fix your logic so the first file isn't dropped... :icon_rolleyes:

im not very good with logic i did a change but it only copies one byte :(

while (1)
		{	
		

		
		printf("please type in the path for the file you want to archive\n");
		scanf("%s",&original_file);
		open_pointer = fopen( original_file, "r+b");								 
		original_pointer = fopen(original_file,"a+b");							
		copy_pointer = fopen( archive_name,"w+b");
		
	
	if (n = fread(buffer,1,sizeof buffer, original_pointer) > 0);
	{
		
		fwrite(buffer,1, n , copy_pointer);
		}
		printf("\n enter any charachter appart from x to add other files to be copied");
		puts("\n or enter  x to coninue");
        scanf("%s",&answer);
	if(answer != 'x')
	{
		continue;
		}
		else
		{
		if( answer == 'x')
		break;
		}}

I believe what you want is available from the programs that create archives. I'm talking about zip, rar, pak, and all the rest.

Google up a website that deals with these archive makers and their algorithms, and dig in. I'd be sure to include Wikipedia in that search. Whatever content they have should be a great introduction to what you want to do.

i looked at the tar source code and oh my days that is some complicated stuff im just a beginner programmer trying to make a simple program that copies file and extracts them thats all

As I'm reading this, what you have is an archive file that you want to copy one or more source files into. So what you want to do is copy all of the first file into your archive, then to copy all of the second file, and then the third, and so on.

Is that correct? If it is, it might explain how you want to structure your loops.

Note that when you get to extraction, you're likely to have a problem with figuring out where one file ends and the next begins. You might want to consider how you want to deal with that issue before you go too much further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.