I have a problem with the a struct´s fields.The code compile but doesn´t work as is should, This is how i define my struct.(Srry for the bad english, its my first time in stackoverflow :)
struct registro {
char Pais[3];
char Nombre[20];
char Apellido[20];
char Clase[1];
char Nivel[2];
char Genero[1];
int Edad;
};
i can write on struct without problem. The words in bold are the ones that i typed
=======Agregue los datos del nuevo estudiante======= Ingrese Pais : **ARG** Ingrese Nombre del Estudiante : **Gonzalo** Ingrese Apellido : **Suarez** Ingrese Clase Social : **A** Ingrese Nivel de Ingles : **C1** Ingrese Edad : **20** Ingrese Genero : **M** ______________________________ ALUMNO AGREGADO DE FORMA EXITOSA! :) Desea Agregar otro estudiante: S/N
The problem is when i want to se file this happen:
=======Archivos de Estudiantes======= ARCHIVO : ___________ Pais : ARGGonzalo Nombre y Apellido : Gonzalo Suarez Clase : AC1M edad : 20 Nivel de ingles : C1M Genero : M ________________________________
i discovered that for some reason the code saves the field Pais,Clase and Nivel wrong, i checked and its not a problem in the printf, i dont know why or how to fix it, I think that theres some library o im using wrong the scanf heres the code:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
void agregar();
void ver_archivo();
void buscar();
void eliminar();
struct registro {
char Pais[3];
char Nombre[20];
char Apellido[20];
char Clase[1];
char Nivel[2];
char Genero[1];
int Edad;
};
void agregar(){
char respuesta2;
FILE *fp;
int n,i;
struct registro reg;
do{
system("cls");
printf("\t\t\t\t=======Agregue los datos del nuevo estudiante=======\n\n\n");
fp=fopen("Datos.dat","w");
printf("\n\t\t\tIngrese Pais : ");
scanf("%s",®.Pais);
printf("\n\t\t\tIngrese Nombre del Estudiante : ");
scanf("%s",®.Nombre);
printf("\n\t\t\tIngrese Apellido : ");
scanf("%s",®.Apellido);
printf("\n\t\t\tIngrese Clase Social : ");
scanf("%s",®.Clase);
printf("\n\t\t\tIngrese Nivel de Ingles : ");
scanf("%s",®.Nivel);
printf("\n\t\t\tIngrese Edad : ");
scanf("%d",®.Edad);
printf("\n\t\t\tIngrese Genero : ");
scanf("%s",®.Genero);
printf("\n\t\t\t______________________________\n");
if(fp==NULL){
fprintf(stderr,"ERROR: NO SE PUDO ABRIR EL ARCHIVO");
}else{
printf("\t\t\tALUMNO AGREGADO DE FORMA EXITOSA! :)\n");
}
fwrite(®, sizeof(struct registro), 1, fp);
fclose(fp);
printf("\t\t\tDesea Agregar otro estudiante: S/N");
scanf("%s",&respuesta2);
}while(respuesta2=='S');
}
my profesor suggest me to change
fp=fopen("Datos.dat","w");
to
fp=fopen("Datos.dat","wb");
but didnt work