Copy of a file

Hi together,

i wrote i code, which should copy the content of a file to another file. Synopsis of this program shall be as follows: simple_cp Source Destination.

Can someone tell me why this programm doesn't work?
i can compile but if i want to run , it gives me the Error:

*** Error in `./simple_cp': double free or corruption (top): 0x0000000001d9f010 ***
Abgebrochen (terminated) (Speicherabzug geschrieben)

thats the code:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>

int main(int argc, char *argv[])
{
int temp;
char *source = argv[1];
char *dest = argv[2];
FILE *src;
FILE *dst;

src = fopen(source, "w");
dst = fopen(dest, "w");
while (src && dst) {
if (3 != argc) {
printf("Your Argument should look like :");
printf("./simple_cp.c Source Destination\n");
return EXIT_FAILURE;
} else if (NULL == src || NULL == dst) {
printf("Konnte Source/Dest. Datei nicht öffnen!\n");
return EXIT_FAILURE;
} while (EOF != fgetc(src)) {
temp = fgetc(src);
fprintf(dst, "%c", temp);
}
fclose(src);
fclose(dst);
}
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>

int main (int argc, char* argv[])
{
	int temp;
	char* source = argv[1]; //dereferencing possible invalid arguments
	char* dest = argv[2];
	FILE* src;
	FILE* dst;

	src = fopen (source, "w"); //open for write, but intend to read
	dst = fopen (dest, "w");
	while (src && dst) //¿why while?
	{
		if (3 != argc) //this is too late, you already tried to open the files
		{
			printf ("Your Argument should look like :");
			printf ("./simple_cp.c Source Destination\n");
			return EXIT_FAILURE;
		}
		else if (NULL == src || NULL == dst) //already check on line 20
		{
			printf ("Konnte Source/Dest. Datei nicht öffnen!\n");
			return EXIT_FAILURE;
		}
		while (EOF != fgetc (src)) //extract one character
		{
			temp = fgetc (src); //extract another character
			fprintf (dst, "%c", temp); //you are missing half of the characters
		}
		//close does not set to NULL, because of your while you'll attemp to read from invalid files
		fclose (src); 
		fclose (dst);
	}
	return 0;
}
Last edited on
I m sorry that i dont familiar with the UC file operation,i wrote it in c++ fstream way;It does the same operation as yours;And it worked ,i tried;Hope this could help ;
Anything you dont understand,let me know;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
#include <iostream>
#include <fstream>
using namespace std;

void dup(char*fileorg,char*filecpy){
	ifstream ifs;
	ofstream ofs;

	ifs.open(fileorg,ios::binary|ios::in); 
	ofs.open(filecpy,ios::binary);		

/*  if(!(ifs.is_open()||ofs.is_open()))
		return ;
	   */
	string lineorg;
	
	while(getline(ifs,lineorg))          
		ofs<<lineorg<<'\n';		

	cout<<"File Duplicated"<<endl;

	ifs.close();
	ofs.close();
}

int main(int argc,char**argv){
	dup(argv[1],argv[2]);
	return 0;
}



Last edited on
Topic archived. No new replies allowed.