Doubt program that copies from one file to another in C with functions

Asked

Viewed 103 times

0

I have to do a C job that copies one file to another at release time (on linux using argc and argv). I’m not used to file manipulation, but here’s the code and it’s not working, what can I do to make it work? I did those three functions you ask for:

  • function that checks whether the arguments passed to the program are correct. This function returns 0 if the arguments are correct, otherwise it must return a value > 0, a value for each possible error;
  • function that prints error messages according to an error code passed to that function;
  • function that copies a file to the target file.
#include<stdio.h>
#include<stdlib.h>​
#define MAX 1000 // Atribuição para leitor char.​
#define MEN 500 // Atribuição para leitor char na função copiar arquivo​
​
void CopiaArq(FILE *arq1,FILE *arq2) // Função que cópia um arquivo e cola no outro.(2)​
int Arg(int n, char *v);// Função que retorna valoros maiores que 0 , quando há algum erro no programa.(1)​
char MensErro (char[MEN]);// Função que envia mnsagens de erro.(3)​
​
int​
main(int argc,char *argv)​
{​
    FILE *p==argv[2];// Nome do arquivo destino com atrubuição de valor no argv[2].​
    FILE *b==argv[4];// Nome do arquivo destino com atrubuição de valor no argv[4].​
​
    if(argc<5 || argc >5)​
    {​
        fprintf(stderr,"Erro de digitação no terminal\n");​
        return 1;​
    }​
    else​
    {​
​
    p=fopen("argv[2]","r"); // Abrindo arquivo no modo de leitura , somente para ver o seu conteúdo.​
    if(p==NULL)​
    {​
        fprintf(stderr,"Erro ao abrir o arquivo\n");​
        return 2;​
    }​
    b=fopen("argv[4]","w");// Abrindo o arquivo no modo escritra , possibilitando a copia dos arquivos.​
    if(b==NULL)​
    {​
        fprintf(stderr,"Erro ao abrir o arquivo\n");​
        return 3;​
    }​
    int Arg(argc, *argv); // Chamada da função(1).​
    char MensErro (char[500]); // Chamada da função(3).​
    void CopiaArq(p,b); // Chamada da função(2).​
    fclose(p); // Fechamento do arquivo p.​
    fclose(b);// Fechamento do arquivo b.​
    }​
  return 0;​
}​
​
void CopiaArq(FILE *arq1,FILE *arq2) // Função que coopia arquivos.​
{​
    char leitor[MAX];​
        while(fgets(leitor,MAX,arq1)!=NULL)​
        {​
            fputs(leitor,arq2);​
        }​
}​
​
​
int Arg(int n, char *v[])// funçãp que retorna valores maiores do que  zero se forem falsas e retorna 0 se for verdadeira.​
{​
    int n;​
    int i;​
    char v[4];​
​
    if(n!=5)​
    {​
        fprintf(stderr,"Erro de passagem nos argumentos para o terminal\n");​
        return 4;​
    }​
    for(i=0;i<4;i++)​
    {​
        fscanf(stdin,"%s",v[i]);​
        if (v[i]!=4)​
        {​
            fprintf(stderr,"Erro de passagem nos argumentos para o terminal\n");​
            return 5;​
        }​
        else​
        {​
            return 0;​
        }​
    }​
}​
char MensErro (char[MEN])// Função que mprime mensagem de erro.​
{​
    int i;​
    for(i=0;i<MEN;i++)​
    {​
        fprintf(stderr,"Codigo passado errado");​
    }​
    return MensErro;​
}​
​
  • 2

    Why isn’t it working? Gives error message, or is the output not expected? Have you used a debugger to know where the error might be occurring?

  • I ended up running out of time and will have to send the code as is, but anyway I will try to use the Debugger later, sometimes there is a chance of correction. Thank you very much!

  • FILE *p==argv[2]; here you are making a comparison? wouldn’t it be an assignment? (using = instead of ==)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.