Question

Hi everyone well I have to create a file that is .dat with should be named whatever the user puts in and this file contains the persons name (doesn't matter if the full name is together), date and his/her weight. Now the problem is that the file "name".dat is created but inside the file it should be just the users name instead in my program i shows up as "name".dat(just like the file name and extension). Here is me code:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;

struct Registro 
{
    int dia,mes,anio; //day,month,year
    float peso; //weight
};
int main(int argc, char** argv) {
     FILE* archivo; 
     int  numero=0;
     char nombre[100],c;
     Registro regis;

     cout<<"Nombre del paciente: ";
     scanf("%s", &nombre);
     getchar();

     strcat(nombre,".dat"); 

     archivo = fopen(nombre, "a+");

     if(archivo)
     {
         cout<<"En que dia estamos?(dd): ";
         scanf ("%d",&regis.dia);
         cout<<"En que mes estamos?: ";
         scanf ("%d",&regis.mes);
         cout<<"En que anio estamos?(aaaa): ";
         scanf ("%d",&regis.anio);
         cout<<"Cual es su peso?: ";
         scanf ("%a",&regis.peso);
        fprintf(archivo,"\r\n# ");
    fprintf(archivo, " Nombre: %s  " " Dia: %d" " Mes: %d" " Anio: %d" " Peso: %g"
                , nombre, regis.dia, regis.mes, regis.anio , regis.peso );


        rewind(archivo);
        do
        {
                c = fgetc(archivo);
        if ( c == '#' )
        {
            numero++;
        }

        }while(c != EOF);

        cout<<"El numero de registros es de "<<numero;

          fclose(archivo);
     }
     else 
          printf("Error al crear archivo.");

     return 0;
}

I think where I'm wrong it this part:

strcat(nombre,".dat"); 

         archivo = fopen(nombre, "a+");

I've tried puting it like this:

archivo = fopen(nombre".dat", "a+");

and

archivo = fopen(nombre,".dat", "a+");

both of these without the

strcat(nombre,".dat");

Thank you guys in advanced.

Was it helpful?

Solution

Look at this pieces of code:

 cout<<"Nombre del paciente: ";
 scanf("%s", &nombre);

you put inside nombre the name of a person. Then you do this:

 strcat(nombre,".dat");

so you append .dat to nombre what effectively becomes: nombre.dat and uses that to write into your archive

By the way, note this much more an 'C' question than an 'C++' question, once you are using, in this majority, 'C' functions.

To solve this, just create a new variable and populates it, like:

char filename[100];

strcpy(filename, nombre);
strcat(filename, ".dat");

OTHER TIPS

You're adding .dat to the variable containing the user's name, so you no longer have the name by itself. You need to use different variables for the user's name and the filename.

 char nombre[100], filename[100], c;

 cout<<"Nombre del paciente: ";
 scanf("%s", &nombre);
 getchar();

 strcpy(filename, nombre);
 strcat(filename,".dat"); 
 archivo = fopen(filename, "a+");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top