0

I am New on pointers and I had and excercise where you had to get info from a txt file then put that info into an structure. anyways when I get the info and call de copyname() function the compiler seems to stop working. I have also checked if theinstance of the alumno[i].nombreEst is created and it also crashes since the compiler does nothing when printing it. if anyone of you guys know what's the reason I would be thakful

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


using namespace std;


typedef struct{
    char *nombreEst;
    int nota;
}estudiantes;

estudiantes *alumno;

void copiarNombre(char info[], int i);


int main(int argc, char** argv) {

int n; //numero de registros que desea mostrar
char temp[50];    // cantidad maxima de caracteres por renglon
char info[50];    //variable que almacenara la informacion del archivo
int renglones;  //cuenta la cantidad de renglones en el archivo

FILE *f;                        
f=fopen("registros.txt","r");

if (f==NULL){
    cout << "no se ha podido abrir el archivo o no existe en la carpeta" << endl;
    exit(1);
}


while (!feof(f))  {  //Lee el contenido que se encuentra en el archivo. si retorna un valor distinto a 0 (por ejemplo cuando se acaba el archivo) se sale del while
    fgets(temp, 50, f);
    renglones++;
}

rewind(f); //retorna el lector del compilador que estaba en el ultimo renglon al primero

alumno = (estudiantes*)malloc(renglones*sizeof(estudiantes));  //crea el espacio de memoria de acuerdo a la cantidad de renglones(estudiantes)  por el tamaño de 1 estudiante)
if (alumno == NULL){
    cout << "no se ha podido reservar el espacio de memoria" << endl;
    exit(1);
}


cout << "ingrese el numero de registros que desea mostrar"<< endl; 
cin >> n;

while(n>=renglones || n<=0){
    cout << "No Existen tantos registros. ingrese el numero de registros que desea mostrar"<< endl; 
    cin >> n;
}

for (int i=0; i<=n; i++){
    char aux='0';
    for (int j=0; aux !='-'; j++){
        aux = fgetc(f);
        if (aux != '-'){
            info[j] = aux;
        }
    }
    copiarNombre(info, i);

    fgets (info, 4, f);
    alumno[i].nota = atoi(info);

    cout<< "Nombre: " << alumno[i].nombreEst << " Nota: " << alumno[i].nota << endl;
}



return 0;

}

void copiarNombre(char info[], int i){
int numCaracteres =  strlen(info) + 1;
alumno[i].nombreEst = (char*)malloc(numCaracteres*sizeof(char));
if (alumno[i].nombreEst = NULL){
    cout << "No se ha podido reservar memoria" << endl;
    exit(1);
}
strcpy(alumno[i].nombreEst, info);
Dr Jfrost
  • 134
  • 10
  • 3
    Typo in `if (alumno[i].nombreEst = NULL){`. You want `==`. – 001 Nov 11 '17 at 04:58
  • wowI didn't noticed it , I actually feel so sorry for asking this I have been like 3 hours checking my code and didn't noticed that thanks for it :D – Dr Jfrost Nov 11 '17 at 05:03
  • 2
    If you get into the habit of putting the constant on the left you will get a compiler error instead. Example: `if (NULL = alumno[i].nombreEst)...` – 001 Nov 11 '17 at 05:09

0 Answers0