Código
al insertar "clave" me sale que el programa dejo de funcionar
struct sEstado_1 { char Nombre[40]; int Codigo; char Estado[10]; } E1; struct sDatos_1 { float Materia1; float Materia2; float Materia3; float Materia4; float Materia5; float Materia6; float Materia7; } D1; void Buscar (){ FILE *Estado1; int clave; printf("Digite El Codigo Del Estudiante Q quiere Buscar: "); scanf("%d", clave); Estado1 = fopen("fichero.txt", "r"); if(Estado1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } while(fread(&E1, sizeof(struct sEstado_1), 1, Estado1)==1) { // desplegando estructura buscada if ( clave == E1.Codigo) { printf("CODIGO =%d ",E1.Codigo); printf(" NOMBRE =%s ",E1.Nombre); printf(" ESTADO =%d ",E1.Estado); printf("\n"); }// fin if }//fin while fclose(Estado1); }
Codigo completo
Código
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <cstdlib> #include <windows.h> using namespace std; void menu(); void CrearFichero(FILE *Estado1); void InsertarDatos(FILE *Estado1); void VerDatos(FILE *Estado1); void CrearArchivo(FILE *Datos1); void InsertarArchivos(FILE *Datos1); void VerArchivos(FILE *Datos1); struct sEstado_1 { char Nombre[40]; int Codigo; char Estado[10]; } E1; struct sDatos_1 { float Materia1; float Materia2; float Materia3; float Materia4; float Materia5; float Materia6; float Materia7; } D1; void Buscar (){ FILE *Estado1; int clave; printf("Digite El Codigo Del Estudiante Q quiere Buscar: "); scanf("%d", clave); Estado1 = fopen("fichero.txt", "r"); if(Estado1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } while(fread(&E1, sizeof(struct sEstado_1), 1, Estado1)==1) { // desplegando estructura buscada if ( clave == E1.Codigo) { printf("CODIGO =%d ",E1.Codigo); printf(" NOMBRE =%s ",E1.Nombre); printf(" ESTADO =%d ",E1.Estado); printf("\n"); }// fin if }//fin while fclose(Estado1); } void PlanEstudio () { int opcion4; FILE *Estado1; FILE *Datos1; int exit = 0; printf("\n *** PLAN DE ESTUDIOS ***\n"); printf("\n\t1. Conultar Materias 1º Semestre"); printf("\n\t2. Conultar Materias 2º Semestre"); printf("\n\t3. Conultar Materias 3º Semestre"); printf("\n\t4. Conultar Materias 4º Semestre"); printf("\n\t5. Conultar Materias 5º Semestre"); printf("\n\t6. Conultar Materias 6º Semestre"); printf("\n\t7. Salir"); while (!exit) { printf("\nOpcion: "); scanf("%d", &opcion4); switch(opcion4) { case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: exit = 1; break; default: printf("\n ** Esta opcion no es valida **\n"); } } return ; } void menuBusqueda() { int opcion3; FILE *Estado1; FILE *Datos1; int exit = 0; printf("\n *** MENU DE BUSQUEDA ***\n"); printf("\n\t1. Conultar Lista De Estudiantes");// lista printf("\n\t2. Estado Del Estudiante");// datos: direccion, tel, etc... printf("\n\t3. Plan De Estudio");// plan proyecto curricular printf("\n\t4. Consultar Estudiante");// materias printf("\n\t5. Volver al inicio"); printf("\n\t6. Salir"); while (!exit) { printf("\nOpcion: "); scanf("%d", &opcion3); switch(opcion3) { case 1: VerDatos(Estado1); break; case 2: cout <<"Estado Del Estudiante"; break; case 3: PlanEstudio(); break; case 4: Buscar(); break; case 5: menu(); break; case 6: exit = 1; break; default: printf("\n ** Esta opcion no es valida **\n"); } } return; } void menuLista(){ int opcion2; FILE *Estado1; FILE *Datos1; int exit = 0; printf("\n *** MENU LISTA ***\n"); printf("\n\t1. Crear Archivo Lista"); printf("\n\t2. Insertar Datos a Lista"); printf("\n\t3. Ver Datos Lista"); printf("\n\t4. Volver Al Inicio"); printf("\n\t5. Salir Del Programa"); while (!exit) { printf("\nOpcion: "); scanf("%d", &opcion2); switch(opcion2) { case 1: CrearArchivo(Datos1); break; case 2: InsertarArchivos(Datos1); break; case 3: VerArchivos(Datos1); break; case 4: menu(); break; case 5: exit = 1; break; default: printf("\nOpcion no valida"); } } return; } void menuAdministrativo() { int opcion2; FILE *Estado1; FILE *Datos1; int exit = 0; printf("\n *** MENU ADMIISTRATIVO ***\n"); printf("\n\t1. Crear Fichero"); printf("\n\t2. Insertar Datos"); printf("\n\t3. Ver Datos Parciales"); printf("\n\t4. Volver Al Inicio"); printf("\n\t5. Salir Del Programa"); while (!exit) { printf("\nOpcion: "); scanf("%d", &opcion2); switch(opcion2) { case 1: CrearFichero(Estado1); break; case 2: InsertarDatos(Estado1); break; case 3: VerDatos(Estado1); break; case 4: menu(); break; case 5: exit = 1; break; default: printf("\nOpcion no valida"); } } return; } void menu() { int opcion, exit=0; printf("\n *** MENU PRINCIPAL ***\n "); printf("\n\t1. Ir al Menu Administrativo "); printf("\n\t2. Ir al Menu De Busqueda "); printf("\n\t3. Ir al Menu Lista"); printf("\n\t4. Salir"); while (!exit) { printf("\n\nOpcion: "); scanf("%d", &opcion); switch(opcion) { case 1: system("CLS"); menuAdministrativo(); case 2: system("CLS"); menuBusqueda(); case 3: system("CLS"); menuLista(); case 4: system("CLS"); exit = 1; break; default: printf("\nOpcion no valida"); } } return ; } int main () { system("color f"); int opcion, opcion2, opcion3, opcion4; int exit = 0; FILE *Estado1; FILE *Datos1; menu(); } void CrearFichero(FILE *Estado1) { Estado1 = fopen("fichero.txt", "r"); if(!Estado1) { Estado1 = fopen("fichero.txt", "w"); printf("\nArchivo creado!"); } else { printf("\nEl fichero ya existe!"); } fclose (Estado1); return; } void InsertarDatos(FILE *Estado1) { Estado1 = fopen("fichero.txt", "a+"); if(Estado1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } for (int a=1; a<=4; a++){ printf("\nDigita el Nombre: "); scanf("%s", &E1.Nombre); printf("\nDigita el Codigo: "); scanf("%d", &E1.Codigo); printf("\nEstado Del Estudiante: "); scanf("%s", &E1.Estado); fwrite(&E1, sizeof(struct sEstado_1), 1, Estado1); } fclose(Estado1); return; } void VerDatos(FILE *Estado1) { int numero = 1; Estado1 = fopen("fichero.txt", "r"); if(Estado1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } fread(&E1, sizeof(struct sEstado_1), 1, Estado1); printf("\nNumero \tNombre \tCodigo \tEstado Del Estudiate"); while(!feof(Estado1)) { printf("\n%d \t%s \t%d \t%s", numero, E1.Nombre, E1.Codigo, E1.Estado); fread(&E1, sizeof(struct sEstado_1), 1, Estado1); numero++; } fclose(Estado1); return; } void CrearArchivo(FILE *Datos1) { Datos1 = fopen("datos.txt", "r"); if(!Datos1) { Datos1 = fopen("fichero.txt", "w"); printf("\nArchivo creado!"); } else { printf("\nEl fichero ya existe!"); } fclose (Datos1); return; } void InsertarArchivos(FILE *Datos1) { Datos1 = fopen("datos.txt", "a+"); if(Datos1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } printf("\nDigite la nota de Algoritmos: "); scanf("%f", &D1.Materia1); printf("\nDigite la nota de Calculo: "); scanf("%f", &D1.Materia2); printf("\nDigite la nota de Catedra FJDC: "); scanf("%f", &D1.Materia3); printf("\nDigite la nota de Catedra DYC: "); scanf("%f", &D1.Materia4); printf("\nDigite la nota de Logica: "); scanf("%f", &D1.Materia5); printf("\nDigite la nota de Algebra: "); scanf("%f", &D1.Materia6); printf("\nDigite la nota de Textos: "); scanf("%f", &D1.Materia7); fwrite(&D1, sizeof(struct sDatos_1), 1, Datos1); fclose(Datos1); return; } void VerArchivos(FILE *Datos1) { int numero = 1; Datos1 = fopen("datos.txt", "r"); if(Datos1 == NULL) { printf("\nFichero no existe! \nPor favor creelo"); return; } fread(&D1, sizeof(struct sDatos_1), 1, Datos1); printf("\nNumero \tAlgoritmos \tCalculo \tCatedra FJDC \tCatedra DYC \tLogica \tAlgebra \tTextos"); while(!feof(Datos1)) { printf("\n%d \t%f \t%f \t%f \t%f \t%f \t%f \t%f", numero, D1.Materia1, D1.Materia2, D1.Materia3, D1.Materia4, D1.Materia5, D1.Materia6, D1.Materia7); fread(&D1, sizeof(struct sDatos_1), 1, Datos1); numero++; } fclose(Datos1); return; }