Customer registration in C saving data in a.txt file

Asked

Viewed 915 times

1

Good morning guys, I’m trying to learn the C language, and I’m making a program to register customers without using a database, I need to save the data (such as name, address, phone, email and password), in a.txt file, my code is like this so far:

#include <stdio.h>

int main ()
{
    FILE * pFile;
    int n;
    char nome[50];
    char endereco[150];
    char telefone[12];
    char email[100];
    char senha[20];

   pFile = fopen ("myfile.txt","a");
   for (n=0 ; n<6 ; n++)
   {
     if(n == 0){
        puts ("Digite seu nome: ");
        gets (nome);
        fprintf (pFile, "Nome %d [%-100.100s]\n",nome);
        n++;
     }
     if(n == 1){
         puts ("Digite seu endereco: ");
         gets (endereco);
         fprintf (pFile, "Endereco %d [%-100.100s]\n",endereco);
         n++;
     }
     if(n == 2){
         puts ("Digite seu telefone: ");
         gets (telefone);
         fprintf (pFile, "Telefone %d [%-10.10s]\n",telefone);
         n++;
     }
     if(n == 3){
         puts ("Digite seu email: ");
         gets (email);
         fprintf (pFile, "Email %d [%-100.10S0s]\n",email);
         n++;
     }
     if(n == 4){
         puts ("Digite sua senha: ");
         gets (senha);
         fprintf (pFile, "Senha %d [%-10.10s]\n",senha);
         n++;
     }
     if(n == 5){
         puts ("Cliente cadastrado com sucesso!");
         n++;
     }
   }
   fclose (pFile);

   return 0;
}

Giving a researched found the fgets() but I didn’t understand how to use it (and I don’t even know if I should actually use it). I know it’s a very basic thing but I’m having a lot of difficulty, I appreciate the help!

According to the link that commented here I managed to progress in the project, however the program is not really saving the data in 'Myfile.txt' they are appearing so:

Name 2686726 [MZ
]

Address 2686576 [MZ
]

Telephone 2686564 [MZ ]

Email 2686464 [Password 2686444 [MZ ]

Would anyone have any solution or other site I might be studying more on the subject!

  • Do you want the fields in your output file to have a fixed size or do you want there to be a separator character between the fields? Your scanf commands for strings are wrong. See here the description and example of the use of the fprintf function: http://www.cplusplus.com/reference/cstdio/fprintf/

  • I didn’t quite understand the question but (pq really I’m right at the beginning) but I need to have space between them, because it will be written the full name, address and etc, I will take a look at the site you sent, thank you very much

  • 1

    On your fprintf if you are printing a string (for example: name) for what reason do you put the format specification %d (which is the format for integer numbers)? Idem for other strings. Try to put only %s and, if applicable, later you increase the format to a more suitable layout.

  • Thank you very much, as I said I’m learning C now and I didn’t even call in %d, I switched for %s and this working, the only problem now is that it saves the data and then adds the "[MZ ]" at the end you would know how to remove it ??

  • alias got here, thank you again for the help

Show 1 more comment
No answers

Browser other questions tagged

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