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

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Error Fichero
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Error Fichero  (Leído 1,923 veces)
mapers


Desconectado Desconectado

Mensajes: 638


Ver Perfil
Error Fichero
« en: 1 Abril 2011, 17:13 pm »

Buenas señores estoy trabajando con archivos de texto plano ...pero con este tipo de estructuras me sale todo bien.... lo unico que no me sale es cuando llamo al fichero e ingresoi nuevos reportes chanca los datos que hay ,lo que  deberia hacer es  ingresar y lguardarlos  cpon los anteriores a ver si me dan una mano y me dicen el error
Código
  1. # define DELIM_C "@"
  2. # define DELIM_R "*"
  3.  
  4. //-------------------------------------------------//
  5. //-------------------------------------------------//
  6.  
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include <conio.h>
  10. # include <io.h>
  11. # include <fcntl.h>
  12. # include <sys\stat.h>
  13.  
  14.  
  15. //-------------------------------------------------//
  16. //-------------------------------------------------//
  17.  
  18. using namespace std;
  19.  
  20. int fichero;
  21. char nom[10];
  22. int edad;
  23. char nombre[10];
  24.  
  25. //-------------------------------------------------//
  26. //-------------------------------------------------//  
  27.  
  28. void crear(char nombre[30])
  29. {
  30. fichero= creat( nombre , S_IWRITE | S_IREAD );
  31.  
  32. if ( fichero < 0 )
  33.    {
  34.      printf(" El Archivo No se Pudo crear !!! ");
  35.       getch();
  36.      return;    
  37.    }
  38.  
  39. else
  40.    printf(" \t\t\t El Archivo Se Creo Correctamente !!!! ");  
  41.     getch();
  42. }
  43.  
  44. //-------------------------------------------------//
  45. //-------------------------------------------------//
  46.  
  47.  
  48. void abrir(char nombre[30])
  49. {
  50. fichero = open ( nombre , O_RDWR);
  51.  
  52.   if ( fichero < 0 )
  53.      printf(" El Archivo No se Pudo Abrir !!! ");
  54. }
  55.  
  56. //-------------------------------------------------//
  57. //-------------------------------------------------//
  58.  
  59. void escribir(int N)
  60. {
  61. abrir(nombre);
  62. printf("\n");
  63. printf("\t Ingrese sus Datos ......  \n");
  64. getch();
  65.  
  66. for(int j=1;j<=N;j++)
  67.   {
  68.    fflush(stdin);
  69.    cout<<endl;
  70.    printf("  -->  Ingrese Nombre   : ");
  71.    fflush(stdin);
  72.    gets(nom);
  73.  
  74.    printf("  --> Ingrese Edad     : ");
  75.    fflush(stdin);
  76.    cin>>edad;
  77.    fflush(stdin);
  78.  
  79.  
  80.    write( fichero, nom , strlen(nom) );
  81.    write( fichero , DELIM_C , 1 );
  82.    write( fichero , &edad , 2 );
  83.    write( fichero, DELIM_R , 1 );
  84.  }
  85.  
  86.  close(fichero);
  87.  
  88. }
  89.  
  90. //-------------------------------------------------//
  91. //-------------------------------------------------//
  92.  
  93. void leer()
  94. {
  95.  char K;
  96.  int i = 0;
  97.   printf("\nTeclee El nombre del Fichero a Acceder ");
  98.   fflush(stdin);
  99.   gets(nombre);
  100.   abrir(nombre);
  101.  
  102.  while ( read ( fichero , &K , 1 ) > 0 )
  103.        {
  104.         if( K == '*' )  
  105.           {
  106.            printf("\t\t    ");
  107.            printf("\n");
  108.            continue;
  109.           }
  110.  
  111.         if( K == '@' )  
  112.           {
  113.           printf("  ");
  114.            read(fichero,&edad,2);
  115.            cout<<edad;
  116.           continue;
  117.           }
  118.  
  119.            cout<<K;
  120.        }
  121. getch();
  122. close(fichero);
  123.  
  124. }
  125. //-------------------------------------------------//
  126. //-------------------------------------------------//
  127.  
  128. void menu()
  129. {
  130.  printf("\n");
  131.  system("cls");  
  132.  printf("\n");
  133.  system("color a");
  134.  printf("\t ÈÍÍÍÍÍÍÍÍÍÍÍÍ<< Reguistros de Longitud Variable >>ÍÍÍÍÍÍÍÍÍÍͼ\n");
  135.  printf("\n\t\t º º     MENU           ");
  136.  printf("\n\t\t º º                      ");
  137.  printf("\n\t\t º º1.  Crear fichero            ");
  138.  printf("\n\t\t º º2.  Insertar datos         ");
  139.  printf("\n\t\t º º3.  Abrir y leer datos Guardados             ");
  140.  printf("\n\t\t º º4.  Salir           \n");
  141.  printf("\t\t º º\n");
  142.  printf("\t\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
  143.  
  144. }
  145.  
  146.  
  147. //-------------------------------------------------//
  148. //-------------------------------------------------//
  149.  
  150.  
  151. int main()
  152. {
  153.  
  154.  
  155. int N;
  156.  
  157. int opcion;  
  158. menu();
  159.  
  160. while(1)
  161. {
  162.   printf("\n\n");
  163.  
  164.   printf("\n");
  165.   for(int j=0;j<10;j++)
  166.   {
  167.   menu();
  168.   }
  169.   //leer();
  170.  
  171.   do
  172.    {
  173.  
  174.     printf("\n\n   -> Ingrese una Opcion");
  175.     printf("      --------------->  ");
  176.     cin>>opcion;
  177.  
  178.    }
  179.  
  180.  while(opcion < 1 || opcion > 4);
  181.  
  182. switch (opcion)
  183. {
  184.  case 1:      
  185.            printf("\n\tIngrese el nombre del Fichero ");
  186.             fflush(stdin);
  187.            gets(nombre);
  188.            crear(nombre);
  189.            break;
  190.  
  191.  case 2:
  192.            cout<<endl<<"   - Cantidad de Datos : ";
  193.            cin>>N;
  194.            escribir(N);
  195.            break;
  196.  
  197.  case 3:
  198.            leer();
  199.            break;
  200.  
  201.  case 4:
  202.  
  203.            printf("\n\t      *****  Se Guardaran los cambios ****" );
  204.            printf("\n\n\n");
  205.            getch();
  206.           exit(0);
  207.            break;
  208.   default:
  209.           printf("\nopcion no valida");
  210. }
  211. }
  212.  
  213. printf("\n\n\n");
  214.  
  215. system("pause");
  216. }
  217.  
  218. //-------------------------------------------------//
  219. //-------------------------------------------------//
  220.  
  221.  






En línea

Leber


Desconectado Desconectado

Mensajes: 338


"Fracta, non verba"


Ver Perfil WWW
Re: Error Fichero
« Respuesta #1 en: 1 Abril 2011, 18:40 pm »

Hola,

Te esta machacando los datos, y no te guarda los anteriores, porque estas abriendo el fichero por el principio. Si miras la documentacion de "open", vemos que el flag O_APPEND dice:


Código:
O_APPEND
              The file is opened in append mode.  Before each write(2), the file offset is positioned at the end of the file, as if with lseek(2).  O_APPEND  may  lead  to  cor‐
              rupted  files on NFS file systems if more than one process appends data to a file at once.  This is because NFS does not support appending to a file, so the client
              kernel has to simulate it, which can't be done without a race condition.

Saludos


En línea

"Solo los tontos carecen de preucupaciones." Johann Wolfgang Goethe
mapers


Desconectado Desconectado

Mensajes: 638


Ver Perfil
Re: Error Fichero
« Respuesta #2 en: 1 Abril 2011, 20:38 pm »

entonces como haria ???? una manitaaa...........
En línea

Leber


Desconectado Desconectado

Mensajes: 338


"Fracta, non verba"


Ver Perfil WWW
Re: Error Fichero
« Respuesta #3 en: 2 Abril 2011, 01:22 am »

Le tienes que pasar el flag O_APPEND a la funcion open.

Código:
open( path, O_RDWR | O_APPEND)
En línea

"Solo los tontos carecen de preucupaciones." Johann Wolfgang Goethe
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Error en lectura de Fichero
Java
Afsoon 8 7,165 Último mensaje 14 Septiembre 2010, 18:26 pm
por Debci
Error al leer un fichero de texto en Python « 1 2 »
Scripting
CaronteGold 17 11,955 Último mensaje 20 Diciembre 2010, 20:26 pm
por CaronteGold
script error fichero con espacio
Scripting
juanxo12 0 1,863 Último mensaje 5 Julio 2017, 17:42 pm
por juanxo12
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines