Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Error al repetir

Estas en el tema de Error al repetir en el foro de C/C++ en Foros del Web. Hola tengo la siguiente función: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C++: Ver original void show ( ) {     system ( "CLS" ) ;     ...
  #1 (permalink)  
Antiguo 02/06/2013, 14:11
Avatar de patilanz  
Fecha de Ingreso: enero-2012
Mensajes: 880
Antigüedad: 12 años, 4 meses
Puntos: 29
Error al repetir

Hola tengo la siguiente función:

Código C++:
Ver original
  1. void show(){
  2.     system("CLS");
  3.     fflush(stdin);
  4.     SetConsoleTextAttribute(h,20);
  5.     printf("El nombre: ");
  6.     char nombre[50];
  7.     SetConsoleTextAttribute(h,15);
  8.     gets_s(nombre);
  9.     sprintf_s(nombre,"%s.dat",nombre);
  10.     FILE *archivo;
  11.     fopen_s(&archivo,nombre,"rb+");
  12.     struct usuarios users2;
  13.     fread(&users2,sizeof(users2),1,archivo);
  14.     system("CLS");
  15.     SetConsoleTextAttribute(h,20);
  16.     printf("\n\tNombre: ");
  17.     SetConsoleTextAttribute(h,15);
  18.     printf("%s",users2.name);
  19.     SetConsoleTextAttribute(h,20);
  20.     printf("\n\tAno de nacimiento: ");
  21.     SetConsoleTextAttribute(h,15);
  22.     printf("%d",users2.nacimiento);
  23.     SetConsoleTextAttribute(h,20);
  24.     printf("\n\tAnos: ");
  25.     SetConsoleTextAttribute(h,15);
  26.     printf("%d",users2.anos);
  27.     SetConsoleTextAttribute(h,20);
  28.     printf("\n\tPais: ");
  29.     SetConsoleTextAttribute(h,15);
  30.     printf("%s",users2.pais);
  31.     SetConsoleTextAttribute(h,20);
  32.     printf("\n\tInfo: ");
  33.     SetConsoleTextAttribute(h,15);
  34.     printf("%s",users2.info);
  35.     printf("\n\n\tClick the boton \"a\" for see more and any other for back.");
  36.     char caracter;
  37.     caracter=_getch();
  38.     if(caracter=='a' || caracter=='A'){
  39.         show();
  40.     }else{
  41.         menuPrincipal();
  42.     }
  43. }

Cuando se ejecuta por primera hace todo bien(muestra los datos del archivo binario.) Pero si el usuario pulsa el boton "a" para repetir me muestra el siguiente error:

Código error:
Ver original
  1. Debug Assertion Failed!
  2.  
  3. Program: ....
  4. FIle: fread.c
  5. Line: 102
  6.  
  7. Expression:(stream!=NULL)
  8. ...

Creo que es porque se llena la variable nombre[50] y luego no hay espacio. Solo creo, no estoy nada seguro.

Como solucionar lo?

Saludos
  #2 (permalink)  
Antiguo 02/06/2013, 15:02
 
Fecha de Ingreso: julio-2012
Mensajes: 375
Antigüedad: 11 años, 9 meses
Puntos: 28
Respuesta: Error al repetir

Me faltan más datos.
  #3 (permalink)  
Antiguo 02/06/2013, 15:52
Avatar de patilanz  
Fecha de Ingreso: enero-2012
Mensajes: 880
Antigüedad: 12 años, 4 meses
Puntos: 29
Respuesta: Error al repetir

Cita:
Iniciado por amchacon Ver Mensaje
Me faltan más datos.
Todo el codigo:

Código C++:
Ver original
  1. // big test.cpp: define el punto de entrada de la aplicación de consola.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string.h>
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <windows.h>
  9. #include <conio.h>
  10. using namespace std;
  11.  
  12. //Structuras
  13. struct usuarios{
  14.     char name[50],info[255],pais[50];
  15.     int anos,nacimiento;
  16. };
  17.  
  18.  
  19. //Funciones
  20. void menuPrincipal(void);
  21. void add(void);
  22. void safe(struct usuarios);
  23. void show();
  24. void volver(char again[]);
  25. //Cosas
  26. HANDLE h= GetStdHandle(STD_OUTPUT_HANDLE);
  27. int main()
  28. {
  29.     menuPrincipal();
  30.     getchar();
  31. }
  32.  
  33. void menuPrincipal(void){
  34.     system("CLS");
  35.     SetConsoleTextAttribute(h,150);
  36.     printf("\t1. Add new people.\n");
  37.     SetConsoleTextAttribute(h,100);
  38.     printf("\t2. Show people. \n");
  39.     SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|20);
  40.     printf("\t3. Exit.          \n");
  41.     SetConsoleTextAttribute(h,15);
  42.     int numero;
  43.     scanf_s("%d",&numero);
  44.     if(numero==1){
  45.         add();
  46.     }
  47.     if(numero==2){
  48.         show();
  49.     }
  50.     if(numero==3){
  51.         exit(0);
  52.     }
  53.     fflush(stdin);
  54. }
  55.  
  56. void add(void){
  57.     system("CLS");
  58.     struct usuarios users;
  59.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  60.     printf("\tIme:                ");
  61.     SetConsoleTextAttribute(h,15);
  62.     fflush(stdin);
  63.     gets_s(users.name);
  64.     fflush(stdin);
  65.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  66.     printf("\n\tGodina na rajdane:");
  67.     SetConsoleTextAttribute(h,15);
  68.     scanf_s("%d[4]",&users.nacimiento);
  69.     fflush(stdin);
  70.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  71.     printf("\n\tGodini:           ");
  72.     SetConsoleTextAttribute(h,15);
  73.     scanf_s("%d[2]",&users.anos);
  74.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  75.     printf("\n\tStrana:           ");
  76.     SetConsoleTextAttribute(h,15);
  77.     fflush(stdin);
  78.     gets_s(users.pais);
  79.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  80.     printf("\n\tInfo:             ");
  81.     SetConsoleTextAttribute(h,15);
  82.     fflush(stdin);
  83.     gets_s(users.info);
  84.     system("CLS");
  85.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  86.     printf("\tIme: ");
  87.     SetConsoleTextAttribute(h,15);
  88.     printf("%s",users.name);
  89.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  90.     printf("\n\tGodina na rajdane: ");
  91.     SetConsoleTextAttribute(h,15);
  92.     printf("%d",users.nacimiento);
  93.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  94.     printf("\n\tGodini: ");
  95.     SetConsoleTextAttribute(h,15);
  96.     printf("%d",users.anos);
  97.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  98.     printf("\n\tStrana: ");
  99.     SetConsoleTextAttribute(h,15);
  100.     printf("%s",users.pais);
  101.     SetConsoleTextAttribute(h,FOREGROUND_INTENSITY|20);
  102.     printf("\n\tInfo: ");
  103.     SetConsoleTextAttribute(h,15);
  104.     printf("%s",users.info);
  105.     char correctos[10];
  106.    
  107.     do{
  108.     printf("\n\n\n\t\t\tLos datos son correctos? (Si\\No)");
  109.     gets_s(correctos);
  110.     _strlwr_s(correctos);
  111.     }while((strcmp(correctos,"si")!=0) && (strcmp(correctos,"no")!=0));
  112.     if(strcmp(correctos,"si")==0){
  113.         safe(users);
  114.     }else if(strcmp(correctos,"no")==0){
  115.         printf("\nVamos a introdusirlos de nuevo... \n click OK para continuar... ");
  116.         getchar();
  117.         add();
  118.     }
  119. }
  120.  
  121. void safe(struct usuarios users){
  122.     char name[60];
  123.     char nombre[70];
  124.     strcpy_s(nombre,users.name);
  125.     sprintf_s(name,"%s.dat",users.name);
  126.     FILE *archivo;
  127.     fopen_s(&archivo,name,"wb+");
  128.     fwrite(&users,sizeof(users),1,archivo);
  129.     struct usuarios users2;
  130.     fread(&users2,sizeof(users2),1,archivo);
  131.     if(strcmp(users.name,nombre)==0){
  132.         SetConsoleTextAttribute(h,FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  133.         printf("\tThe people was saved!");
  134.         printf("\\tnClick a boton for continue.");
  135.         getchar();
  136.         menuPrincipal();
  137.     }else{
  138.         SetConsoleTextAttribute(h,FOREGROUND_RED| FOREGROUND_INTENSITY);
  139.         printf("The people can't save. ");
  140.     }
  141. }
  142.  
  143. void show(){
  144.     system("CLS");
  145.     fflush(stdin);
  146.     SetConsoleTextAttribute(h,20);
  147.     printf("The name: ");
  148.     char nombre[50];
  149.     SetConsoleTextAttribute(h,15);
  150.     gets_s(nombre);
  151.     sprintf_s(nombre,"%s.dat",nombre);
  152.     FILE *archivo;
  153.     fopen_s(&archivo,nombre,"rb+");
  154.     struct usuarios users2;
  155.     fread(&users2,sizeof(users2),1,archivo);
  156.     system("CLS");
  157.     SetConsoleTextAttribute(h,20);
  158.     printf("\n\tIme: ");
  159.     SetConsoleTextAttribute(h,15);
  160.     printf("%s",users2.name);
  161.     SetConsoleTextAttribute(h,20);
  162.     printf("\n\tGodina na rajdane: ");
  163.     SetConsoleTextAttribute(h,15);
  164.     printf("%d",users2.nacimiento);
  165.     SetConsoleTextAttribute(h,20);
  166.     printf("\n\tGodini: ");
  167.     SetConsoleTextAttribute(h,15);
  168.     printf("%d",users2.anos);
  169.     SetConsoleTextAttribute(h,20);
  170.     printf("\n\tStrana: ");
  171.     SetConsoleTextAttribute(h,15);
  172.     printf("%s",users2.pais);
  173.     SetConsoleTextAttribute(h,20);
  174.     printf("\n\tInfo: ");
  175.     SetConsoleTextAttribute(h,15);
  176.     printf("%s",users2.info);
  177.     printf("\n\n\tClick the boton \"a\" for see more and any other for back.");
  178.     char caracter;
  179.     caracter=_getch();
  180.     if(caracter=='a' || caracter=='b'){
  181.         show();
  182.     }else{
  183.         menuPrincipal();
  184.     }
  185. }
  #4 (permalink)  
Antiguo 02/06/2013, 15:56
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Error al repetir

Asegúrate que en la parte de fread(&users2,sizeof(users2),1,archivo); archivo no sea NULL.
  #5 (permalink)  
Antiguo 02/06/2013, 23:48
Avatar de patilanz  
Fecha de Ingreso: enero-2012
Mensajes: 880
Antigüedad: 12 años, 4 meses
Puntos: 29
Respuesta: Error al repetir

Cita:
Iniciado por razpeitia Ver Mensaje
Asegúrate que en la parte de fread(&users2,sizeof(users2),1,archivo); archivo no sea NULL.
En mi caso no creo que es esto ya que pongo el mismo nombre para asegurarme pero igual seria algo asi?

Código C++:
Ver original
  1. if(users2==NULL){printf("Error");}else{Continuar}

Etiquetas: repetir, struct, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:18.