How to sort the data of a file in C

0

I would like to know how I could order the data entered by the user in alphabetical order and then show them on the screen. This is my code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

//Contiene los datos que se necesita de cada alumno
struct Datos_Personales{
 char Nombre[MAX];
 char Apellidos[MAX];
 char Num_Matricula[MAX];
 char Direccion[MAX];
 char Correo[MAX];
 char Nota_P1[MAX];
 char Nota_P2[MAX];
 char Nota_P3[MAX];
 char Laboratorios[MAX];
}datos;
//Abre el archivo como variable global
FILE *fd;
int Agregar_Alumno(){
int Decision;
//Abre el archivo de texto y le asigna propiedades
fd = fopen("AlumnosDatos.txt","a+"); //at = add text -- agregar texto

if(fd == NULL){
    printf("Error al tratar de leer el archivo");
    return -1;
}
printf("\n\t.:Agregando Alumnos:.\n");

do{
    //Guarda la informacion en los estruct
    fflush(stdin);
    printf("\nNombre: ");
    gets(datos.Nombre);
    printf("Apellidos: ");
    gets(datos.Apellidos);
    printf("Email: ");
    gets(datos.Correo);
    //La informacion de los estruct la escribe en el archivo
    fprintf(fd,"\n");
    fwrite(datos.Nombre,1,strlen(datos.Nombre),fd);
    fprintf(fd,";");
    fwrite(datos.Apellidos,1,strlen(datos.Apellidos),fd);
    fprintf(fd,";");
    fwrite(datos.Correo,1,strlen(datos.Correo),fd);
    printf("¿Desea seguir agregando Alumno(s)?\n");
    printf("1) SI \n");
    printf("2) NO \n");
    printf("Su opcion fue: ");
    scanf("%d", & Decision);
    system("cls");
}
while(Decision == 1);   
fclose(fd);
}

int Visualizar_Lista(){
int c;

fd = fopen("AlumnosDatos.txt","r"); // r = read - leer

if(fd == NULL){
    printf("Error al tratar de leer el archivo");
    return 1;
}

printf("\n\t.:Visualizando Lista de Alumnos:.\n\n");

while((c=fgetc(fd))!=EOF){
    if(c == '\n'){
        printf("\n");
    }
    else{
        putchar(c);
    }
}
printf("\n\n");
}

int main(){
int Opcion;

do{
    printf("\n\t.:MENU:.");
    printf("\n1. Agregar Alumno");
    printf("\n2. Eliminar Alumno");
    printf("\n3. Visualizar Lista de Alumnos");
    printf("\n4. Salir");
    printf("\nOpcion: ");
    scanf("%i",&Opcion);

    switch(Opcion){
        case 1: Agregar_Alumno();
        break;
        case 2: 
        break;
        case 3: Visualizar_Lista();
        break;
    }

}
while(Opcion !=4);
fclose(fd);
return 0;
}

I still do not believe the way to order it because I do not know anything about how to do it, so any contribution is much appreciated: (

    
asked by Wolf 02.12.2018 в 08:30
source

0 answers