elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


  Mostrar Mensajes
Páginas: 1 [2]
11  Programación / Programación C/C++ / Re: Problema al leer registros en ficheros en: 25 Noviembre 2015, 14:09 pm
Ya está solucionado, el problema era que al visualizar los registros el bucle tiene que ser:

Código
  1. fread(p_per, sizeof(t_persona), 1, pf);
  2. while (!feof(pf) && !ferror(pf)) {
  3. printf("%s\n", p_per->nombre);
  4. printf("%s\n", p_per->direccion);
  5. printf("%d\n", p_per->telefono);
  6. fread(p_per, sizeof(t_persona), 1, pf);
  7. }
  8.  
12  Programación / Bases de Datos / Problema con relación 1:1 en: 25 Noviembre 2015, 14:01 pm
Tengo esta base de datos:

Código
  1. CREATE TABLE ehbd.Clientes (
  2. idClientes INT NOT NULL AUTO_INCREMENT ,
  3.    nombre VARCHAR(64) NOT NULL,
  4.    PRIMARY KEY (idClientes),
  5.    FOREIGN KEY (idUsuarios) REFERENCES ehbd.Usuarios(idUsuarios)
  6. ) engine=innodb;
  7.  
  8. CREATE TABLE ehbd.Usuarios (
  9. idUsuarios INT NOT NULL AUTO_INCREMENT ,
  10.    username VARCHAR(64) NOT NULL,
  11.    email VARCHAR(64) NOT NULL,
  12.    PRIMARY KEY(idUsuarios),
  13.    FOREIGN KEY(idClientes) REFERENCES ehbd.Clientes(idClientes)
  14. ) engine=innodb;
  15.  
  16.  

Necesito relacionar estas dos bases de datos con una relación 1:1. Esto me da error porque en la primera tabla defino una foreign key a una tabla Usuarios que todavía no existe.

¿Cómo podría hacer esto sin juntar las tablas?

Gracias anticipadas.

Un cordial saludo
13  Programación / Programación C/C++ / Problema al leer registros en ficheros en: 25 Noviembre 2015, 13:35 pm
Hola,
Tengo un problema con este programa. Como podéis ver cuando muestro los registros del fichero los duplica.

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "MemoryManager.h"
  6.  
  7. #define N 80
  8. #define SALIR 4
  9.  
  10. typedef struct {
  11. char nombre[N];
  12. char direccion[N];
  13. int telefono;
  14. } t_persona;
  15.  
  16.  
  17. int tolower(int);
  18. int menu(void);
  19. void crearFichero(FILE **pf);
  20. void introducirRegistros(FILE *pf);
  21. void visualizarFichero(FILE *pf);
  22.  
  23. /**
  24.  * Punto de entrada y salida al programa principal
  25.  */
  26. int main()
  27. {
  28. int op = 0;
  29. FILE *pf = NULL;
  30.  
  31. do {
  32. op = menu();
  33.  
  34. switch (op)
  35. {
  36. case 1: // crear fichero
  37. crearFichero(&pf);
  38. break;
  39.  
  40. case 2: // introducir registros
  41. introducirRegistros(pf);
  42. break;
  43.  
  44. case 3: // visualizar registros
  45. visualizarFichero(pf);
  46. break;
  47.  
  48. case 4: // salir
  49. printf("bye bye\n");
  50. break;
  51. }
  52. } while (op != SALIR);
  53.  
  54. fclose(pf);
  55. MemoryManager_DumpMemoryLeaks();
  56. return 0;
  57. }
  58.  
  59. int tolower(int c)
  60. {
  61. const int dsp = 'a' - 'A';
  62.  
  63. if (c >= 'A' && c <= 'Z')
  64. return c + dsp;
  65. return c;
  66. }
  67.  
  68. int menu(void)
  69. {
  70. int op = 0;
  71. char linea[N] = { 0 };
  72.  
  73. do {
  74. printf("\n");
  75. printf("1. Crear fichero\n");
  76. printf("2. Introducir registros\n");
  77. printf("3. visualizar registros\n");
  78. printf("4. salir\n");
  79.  
  80. printf("\topcion: ");
  81. fgets(linea, N, stdin);
  82. sscanf(linea, "%d", &op);
  83. } while (op < 1 || op > 4);
  84.  
  85. return op;
  86. }
  87.  
  88. void crearFichero(FILE **pf)
  89. {
  90. char nomFichero[N] = { 0 };
  91. char resp = 0;
  92. char linea[N] = { 0 };
  93.  
  94. printf("Nombre del fichero: ");
  95. fgets(nomFichero, N, stdin);
  96. nomFichero[strlen(nomFichero) - 1] = 0;
  97.  
  98. if (*pf != NULL) {
  99.  
  100. // existe, preguntar si se sobreesribe
  101. do {
  102. printf("Desea sobreescribir el fichero");
  103. fgets(linea, N, stdin);
  104. sscanf(linea, "%c", &resp);
  105. } while (tolower(resp) != 's'
  106. && tolower(resp) != 'n');
  107.  
  108. if (tolower(resp) == 'n')
  109. return;
  110. }
  111.  
  112. *pf = fopen(nomFichero, "w+b");
  113. }
  114.  
  115. void introducirRegistros(FILE *pf)
  116. {
  117. char resp = 0;
  118. char linea[N] = { 0 };
  119.  
  120. t_persona *p_per = malloc(sizeof(t_persona));
  121. memset(p_per, 0, sizeof(t_persona));
  122. if (p_per == NULL)
  123. {
  124. printf("memoria insuficiente");
  125. exit(-1);
  126. }
  127.  
  128.  
  129. do {
  130. printf("\n------\n");
  131. printf("Nombre: ");
  132. fgets(p_per->nombre, N, stdin);
  133. p_per->nombre[strlen(p_per->nombre) - 1] = 0;
  134.  
  135. printf("Direccion: ");
  136. fgets(p_per->direccion, N, stdin);
  137. p_per->direccion[strlen(p_per->direccion) - 1] = 0;
  138.  
  139. printf("Telefono: ");
  140. fgets(linea, N, stdin);
  141. sscanf(linea, "%d", &p_per->telefono);
  142.  
  143. rewind(pf);
  144. fwrite(p_per, sizeof(t_persona), 1, pf);
  145.  
  146. do {
  147. printf("\nDesea introducir otro registro: ");
  148. fgets(linea, N, stdin);
  149. sscanf(linea, "%c", &resp);
  150. } while (tolower(resp) != 's'
  151. && tolower(resp) != 'n');
  152. } while (tolower(resp) == 's');
  153.  
  154. free(p_per);
  155. }
  156.  
  157. void visualizarFichero(FILE *pf)
  158. {
  159. char linea[N] = { 0 };
  160. int pos = 0;
  161.  
  162. t_persona *p_per = malloc(sizeof(t_persona));
  163. if (p_per == NULL)
  164. {
  165. printf("memoria insuficiente");
  166. exit(-1);
  167. }
  168.  
  169. rewind(pf);
  170.  
  171. while (!feof(pf) && !ferror(pf)) {
  172. fread(p_per, sizeof(t_persona), 1, pf);
  173. printf("%s\n", p_per->nombre);
  174. printf("%s\n", p_per->direccion);
  175. printf("%d\n", p_per->telefono);
  176. }
  177.  
  178. free(p_per);
  179. }
  180.  


Código:
1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 1
Nombre del fichero: red.datos

1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 2

------
Nombre: Marina
Direccion: vereda
Telefono: 234
Desea introducir otro registro: n

1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 3

Marina
vereda
234
Marina
vereda
234

Gracias anticipadas.

Un cordial saludo
14  Programación / Programación C/C++ / Re: Dudas sobre system() en: 5 Mayo 2012, 01:48 am
Si lo necesitas siempre puedes hacer una compilación condicional

Código
  1. #ifdef WIN32
  2. system("cls");
  3. #else
  4. system("clear");
  5. #endif
  6.  
15  Programación / Programación C/C++ / Re: Segmentation fault (core dumped) en: 3 Mayo 2012, 17:03 pm
Vale, por lo que me dices, entiendo que el sistema operativo detecta el fallo de segmento cuando te sales del rango de direcciones que te asigna, ¿verdad?

Creo que voy a tener que leer más sobre el heap...
16  Programación / Programación C/C++ / Re: ingresando datos unidimensionalmente en: 3 Mayo 2012, 16:57 pm
Lo que intentas se puede solucionar con realloc:

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void leer(double **);
  6.  
  7. int main()
  8. {
  9.    double *p = NULL;
  10.    leer(&p);
  11.    free(p);
  12.  
  13.    return 0;
  14. }
  15.  
  16. void leer(double **pdata)
  17. {
  18.    int i = 1;
  19.    char opc;
  20.  
  21.    do {
  22.        printf("dato: ");
  23.        *pdata = realloc(*pdata, sizeof(double) * i);
  24.        scanf("%lf", *pdata+(i-1));
  25.        i++;
  26.        getchar();
  27.        printf("mas datos (s/n)? ");
  28.        scanf("%c", &opc);
  29.    } while (opc == 's' || opc == 'S');
  30. }
  31.  
  32.  
17  Programación / Programación C/C++ / Segmentation fault (core dumped) en: 3 Mayo 2012, 16:03 pm
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SUP 33799
  6.  
  7. int main()
  8. {
  9.    int *cad = NULL;
  10.    int i;
  11.  
  12.    cad = malloc(sizeof(int) * 2);
  13.  
  14.    for (i = 0; i < SUP; i++) {
  15.        cad[i] = i;
  16.        printf("%d ", cad[i]);
  17.    }
  18.    printf("\n");
  19.  
  20.    return EXIT_SUCCESS;
  21. }
  22.  

Si SUP es, por ejemplo, 20, no hay Segment fault. Si le doy un valor, por ejemplo, 33799 lanza este error al ejecutar el programa.

¿Por qué no da error de segmento a partir de cad[2]?

Gracias de antemano
Páginas: 1 [2]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines