How do I write a file in c with names of several variables?

Asked

Viewed 688 times

0

Good morning. I have a problem to be able to create a file that stores the time when the user modified a particular program, for example: If the user ran the file at 2h34min32s, I want to save this date in a file with this format"cliente_2_34_32.txt". the cogido is below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct cliente{
 char nome[30];
 int cpf,cep;
};
void info(struct cliente *aux){
 int cpf,cep;
 printf("\n digite os dados:\n Nome:");
 gets(aux->nome);
 printf("\n CPF: ");
 scanf("%d",&cpf);
 fflush(stdin);
 printf("\n CEP: ");
 scanf("%d",&cep);
 fflush(stdin);
 aux->cpf=cpf;
 aux->cep=cep;
};
int main()
{
 time_t tempo;
 struct tm *tempoS;
 int n=0;
 struct cliente *vet,*aux;
 FILE *ptr;
 vet=(struct cliente*)malloc((n+1)*sizeof(struct 
 cliente));
 info(vet);
 int v,y=1;
 while(y){
    printf("\n Deseja continuar: 1- sim, outro numero- 
  nao");
    scanf("%d",&v);
    fflush(stdin);
    if(v==1){
        n++;
        vet=(struct cliente*)realloc(vet,(n+1)*sizeof(struct cliente));
        aux=&vet[n];
        info(aux);
    }else{
        y=0;
    }
}
free(vet);
tempo=time(NULL);
tempoS=localtime(&tempo);
char nome[]="vaca_%d.txt";

printf("cliente_%d_%d_%d",tempoS->tm_hour,tempoS->tm_min,tempoS->tm_sec);
ptr=fopen(nome,"w");
fclose(ptr);

return 0;
}

The answer screen is this: inserir a descrição da imagem aqui

I wanted to know how to make the printf phrase (cliente_17_14_43) be the name of the txt file.

  • 1

    What happens when you run the cogido? Does it give an error? If it does, already edit the question and describe it. If it does not give error, already edits the question and describes what is the observed behavior. What was the answer? What should it be? Does it even create the file? With what name?

1 answer

0

you can rename the file use the function name

PROTOTYPE:

int rename (const char *Caminho_com_nome_antigo, const char *Caminho_com_nome_novo);
  • I tried this way, but my problem is how to make the system variables become only a single sentence, and then make the name write in the txt file title.

  • already tried to convert to string and concatenate into a single variable the name ?

  • Vlw ai. Be able to resolve following your advice, I made the string receive the formatted name

Browser other questions tagged

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