1

I have a problem with putting the value of creditos and nota in the structure.

I have a file with this, for example:

Grade: 4.5

I'm using strtok, but it returns a char pointer, and I need a int and a float.

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

typedef struct{
char *nombre;
int creditos;
float nota;
}curso;

int conteo(FILE *_entrada);
void semestre(FILE *_entrada,curso *_materias,int *_cantidad,int *_ganadas,int *_perdidas,float *_promedio);
void imprimir(curso *_materias,int *_cantidad,int *_ganadas, int *_perdidas,float *_promedio);


int main(int argc, char *argv[]){
int ganadas=0;
int perdidas=0;
float promedio=0.0;
int cantidad=0;
char archivoEntrada[256];
curso *materias;
printf("Archivo de entrada \n");
scanf("%255s",archivoEntrada);
FILE *entrada;
entrada=fopen(archivoEntrada,"r");
if(entrada==NULL){
printf("No se logro abrir el archivo de entrada\n");
exit(EXIT_FAILURE);
}

cantidad=conteo(entrada);

materias=(curso *)malloc(sizeof(curso)*cantidad);

semestre(entrada,materias,&cantidad,&ganadas,&perdidas,&promedio);

imprimir(materias,&cantidad,&ganadas,&perdidas,&promedio);

free(materias);
}


int conteo(FILE *_entrada){
int i=0;
char auxiliar[40];
while(!feof(_entrada)){
fgets(auxiliar,40,_entrada);
i++;
}
rewind(_entrada);
return i/3;
}

void semestre(FILE *_entrada,curso *_materias,int *_cantidad,int *_ganadas,int *_perdidas,float *_promedio){
int i=0;
int sumaCreditos=0;
float sumaNotas=0.0;
char auxiliar2[100];
char *token;
fgets(auxiliar2 ,100,_entrada);
while(i<*_cantidad){
fgets(auxiliar2,100,_entrada);
token=strtok(auxiliar2,":");
token=strtok(NULL,":");
printf("Dato: %s \n",token);
(_materias->nombre)=token;

fgets(auxiliar2 ,100,_entrada);
token=strtok(auxiliar2,":");
(&_materias->creditos)=token;
sumaCreditos=sumaCreditos+(_materias->creditos);

fgets(auxiliar2 ,100,_entrada);
token=strtok(auxiliar2,":");
(&_materias->nota)=token;

if((_materias->nota)>3.0){
*_ganadas=(*_ganadas)+1;
}
else{
*_perdidas=(*_perdidas)+1;
}
sumaNotas=sumaNotas+((_materias->nota)*(_materias->creditos));
i++;
*_materias++;
}
*_promedio=(sumaNotas/sumaCreditos);
}

void imprimir(curso *_materias,int *_cantidad,int *_ganadas, int *_perdidas,float *_promedio){
char archivoSalida[256];
FILE *salida;
printf("Archivo de salida \n");
scanf("%255s",archivoSalida);
salida=fopen(archivoSalida,"w");
if(salida==NULL){
printf("No se logro abrir el archivo de salida\n");
exit(EXIT_FAILURE);
}
int i=0;
fprintf(salida,"Archivo de Salida: \n");
fprintf(salida,"Materia\tNota\tCreditos \n");
while(i<*_cantidad){
fprintf(salida,"%s\t%f\t%d \n",(_materias->nombre),(_materias->nota),(_materias->creditos));
_materias++;
i++;
}
fprintf(salida,"\nTotal de materias: %d \n",*_cantidad);
fprintf(salida,"Materias ganadas: %d \n",*_ganadas);
fprintf(salida,"Materias perdidas: %d \n",*_perdidas);
fprintf(salida,"Promedio ponderado: %f \n",*_promedio);
}
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • So you're asking how to convert a string to an int, and how to convert a string to a float? – user253751 Oct 02 '15 at 23:33
  • Please format your code neatly and preserve the indenting. I started to do it but it looked like there were unbalanced braces and that it wouldn't even compile. Like @immibis asked, can you please narrow down the question and example to the point you specifically need help for? – clearlight Oct 02 '15 at 23:35
  • its a pointer, from char pointer to float or int – J Mateo Pineda Lop Oct 02 '15 at 23:35
  • fgets(auxiliar2 ,100,_entrada); token=strtok(auxiliar2,":"); (&_materias->nota)=token; here is my problem, i need a parsing but i dont know – J Mateo Pineda Lop Oct 02 '15 at 23:37
  • [do not cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Dec 16 '15 at 13:19

1 Answers1

0

In your code, token is a pointer to char, which is the same as string in C.

Use atoi to convert a string to a int:

_materias->creditos = atoi(token);

Use atof to convert a string to a float:

_materias->nota = atof(token);

If you need to make your program more robust to errors in your input file, use strtol and strtof instead. They are harder to use but more secure if your input file can have errors (i.e. always).


Alternatively, use sscanf - it's probably easier to use than strtol and strtof, and supports detection of errors, but it has no type safety, so may lead to hard-to-fix frustrating crashes.

Usage:

if (sscanf(token, "%d", &_materias->creditos) != 1)
    fprintf(stderr, "Error\n"); 

if (sscanf(token, "%f", &_materias->nota) != 1)
    fprintf(stderr, "Error\n"); 
anatolyg
  • 26,506
  • 9
  • 60
  • 134