Jump to content

Recommended Posts

Posted

Se cere sa se implementeze comanda copy din MS-DOS.

Se creaza fisierul my_copy.cpp, ce cuprinde urmatorul cod:


#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

#define BUFLNG 1000            //lungimea buffer-ului folosit la copiere

#define FTMP "my_copy.tmp"          //fisier de lucru temporar

char sursa[13], dest[13];

void analiza_parametri(int nr_p, char *parametru[])       //nr_p= un numar intreg reprezentand numarul de parametri din linia de comanda, adica lungimea vectorului parametru

{


	strcpy(sursa, parametru[1]);

	strcpy(dest, parametru[2]);

	switch(nr_p)

	{


	case 1: printf("\nFisier sursa: ");

		gets(sursa);

	case 2: printf("\nFisier destinatie: ");

	        gets(dest);

	case 3: strcpy(sursa, sursa);

		strcpy(dest, dest);

		break;

	default:

		printf("\nPrea multi parametri.\n");

		exit(1);

	}

}

void copiere(FILE *f1, FILE *f2)

{

	char c, *buf_cit;

	int nb_cit, cit, nb_scr;

	if((f1=fopen(from, "rb"))==NULL)

	{

		printf("\nfisier de intrare eronat!\n");

		exit(1);

	}

	if((f2==fopen(dest, "rb"))!=NULL)

	{

		printf("\nFisier destinatie existent!\n");

		printf("Suprascriem [d/n]? ");

		c=getche();

		if(c=='N' || c=='n')

			strcpy(dest, FTMP);

	}

	if((f2==fopen(to, "wb"))==NULL)

	{

		printf("\n\nEroare de creare fisier destinatie!");

		exit(1);

	}

	if((buf_cit=(char *)malloc(BUFLNG))==NULL)

	{

		printf("\n\aMemorie insuficienta.\n");

		exit(1);

	}

	while((nb_cit=fread(buf_cit, 1, BUFLNG, f1))!=0 && (nb_scr=fwrite(buf_cit, 1, nb_cit, f2))==nb_cit)

  fclose(f1);

	fclose(f2);

  printf("\nCopiere");

	if(nb_cit!=nb_scr)

		printf("\a in");

	printf("\nCompleta.");


}

void main(int argc, char *argv[])

{


	FILE *pfrom, *pto;

	analiza_parametri(argc, argv);

	copiere(pfrom, pto);

}

In urma compilarii, se va crea executabilul my_copy.exe, care se va putea apela cu:
my_copy <fisier_sursa> <fisier_destinatie>

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.