0

I have a segmentation fault in the moment of get the second file in the function imprimir, and i dont know whats happend, because he open and read the firts but not other file, is very confused for me, please help me

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

typedef struct
{
    char nombre[40];
    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;
    curso *materias;
    printf("Archivo de entrada \n");
    scanf("%s",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);
    printf("El numero de materias es: %d \n",cantidad);

    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;
    while(i<*_cantidad)
    {
        fscanf(_entrada, "%s", _materias->nombre);
        fscanf(_entrada, "%d", &_materias->creditos);
        sumaCreditos=sumaCreditos+(_materias->creditos);
        fscanf(_entrada, "%f", &_materias->nota);
        if((_materias->nota)>3.0)
        {
            *_ganadas=(*_ganadas)+1;
        }
    }
}

void imprimir(curso *_materias,int *_cantidad,int *_ganadas, int *_perdidas,float *_promedio)
{
    fflush(stdin);
    printf("Ganadas %d \n",*_ganadas);
    printf("perdidas %d \n",*_perdidas);
    printf("prom %f \n",*_promedio);
    char *archivoSalida;
    FILE *salida;
    printf("Archivo de salida \n");
    scanf("%s",archivoSalida);
    salida=fopen(archivoSalida,"w");
    if(salida==NULL)
    {
        printf("No se logro abrir el archivo de salida\n");
        exit(EXIT_FAILURE);
    }
    //Implementacion imprimir en archivo salida.txt
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • It looks like part of your code is missing. The braces aren't balanced, and I don't think you really meant to define the `imprimir()` function inside the `semestre()` function. – Barmar Oct 02 '15 at 20:52
  • 1
    Don't do `fflush(stdin)`, it's undefined in the C specification. – Some programmer dude Oct 02 '15 at 20:53
  • 1
    [while (!feof(_entrada)) is wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Oct 02 '15 at 20:53

1 Answers1

3

You have

char *archivoSalida;

and then

scanf("%s",archivoSalida);

The problem here is that the pointer archivoSalida doesn't actually point anywhere special. Uninitialized local variables (like archivoSalida is) have an indeterminate value. Using them without initialization leads to undefined behavior, a very common cause of crashes.

Instead you might want to use a fixed-size array:

char archivoSalida[256];
...
scanf("%255s",archivoSalida);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621