Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/10/2010, 18:45
joiner3700
 
Fecha de Ingreso: septiembre-2010
Mensajes: 16
Antigüedad: 13 años, 8 meses
Puntos: 0
Urgente por favor ayudenme!!

hola que tal amigos me mandaron a hacer un programa en la universidad de manejo de archivos y no me sale aqui esta el enunciado y mi codigo

"Escriba un programa en C que le permita al usuario actualizar una fila determinada de un archivo. El usuario deberá indicar el número de la fila por teclado y el texto por el cual desea reemplazar el contenido actual."

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void validar(FILE*archivo2)
  5.       {
  6.     char caracter[80];
  7.     int a=0;
  8.     archivo2= fopen("actualizar.txt","a");
  9.     if(archivo2==NULL)
  10.     {
  11.     system("CLS");
  12.     printf("error");
  13.     system("pause>nul");    
  14.     exit(1);    
  15.     }else
  16.     {
  17.     system("CLS");
  18.     printf("succes");
  19.     system("pause>nul");    
  20.     }    
  21.            
  22.       }
  23. //----------------------------------------------------------------------------------------------------------------------------              
  24. void leer()
  25.    {
  26.       FILE *archivo;    
  27.       char caracter[80];
  28.       int a=0;
  29.       char c;
  30.       archivo=fopen("actualizar.txt","r+");  
  31.       if(feof(archivo)!=0){system("CLS");system("pause>nul");printf("no se encontro el final");exit(1);}
  32.       while(feof(archivo)==0)
  33.                              {
  34.                              
  35.                               fgets(caracter,80,archivo);
  36.                               printf("%s",caracter);
  37.                               }
  38.       fclose(archivo);
  39.       }
  40. //----------------------------------------------------------------------------------------------------------------------------  
  41. void parametros_incorrectos()
  42.                    {
  43.                    system("CLS");
  44.                    printf("Parametros incorrectos");
  45.                    system("pause>nul");          
  46.                              
  47.                    }
  48. //------------------------------------------------------------------------------------------------------------------------------
  49. void final()
  50.     {
  51.     system("CLS");
  52.     printf("GRACIAS POR UTILIZAR ESTE PROGRAMA VUELVA PRONTO");                                                                                                                    
  53.     system("pause>nul");
  54.     exit(1);
  55.                                                                                                                        
  56.                                                                                                                        
  57.     }
  58. //-------------------------------------------------------------------------------------------------------------------------------
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62. system("color 0b");
  63. //----------------------variables------------------------
  64. FILE *archivo;
  65. FILE *archivo2;
  66. archivo=fopen("actualizar.txt","a");
  67. archivo2=fopen("buffer.txt","a");
  68. int op=0;
  69. int op2=0;
  70. int a=0;
  71. char texto[255];
  72. char texto2[255];
  73. //-------------------------------------------------------
  74. validar(archivo);
  75. do
  76. {
  77. system("CLS");
  78. printf("\n-------------------BIENVENIDOS AL PROGRAMA QUE ACTUALIZA LINEAS-------------------\n");
  79. printf("EL texto original dice:\n\n");
  80. leer(archivo);  
  81. printf("\n\nEscoja una opcion:");
  82. printf("\n1.Ingresar fila a sustituir");
  83. printf("\n2.Salir");
  84. printf("\nSu opcion elegida fue:");
  85. fflush(stdin);
  86. scanf ("%d",&op);
  87. fflush(stdin);  
  88. switch(op)
  89.   {
  90.   case 1:
  91.          printf("\nEditara la fila nro.");
  92.          scanf ("%d",&op2);
  93.          printf("\nTexto que desea incluir:\n");
  94.          scanf ("%s",&texto);
  95.          gets(texto2);
  96.          //printf("%s",texto2);
  97.          fputs(texto2,archivo2);
  98.          while(feof(archivo)==0)
  99.                                 {
  100.                                 fgets(texto,100,archivo);
  101.                                 a++;
  102.                                 if(a==op2)
  103.                                           {
  104.                                           fgets(texto,100,archivo2);
  105.                                           strcpy(texto2,fgets(texto,100,archivo2));
  106.                                           fprintf(archivo,"%s",texto2);  
  107.                                           }                
  108.                                 }
  109.   leer(archivo);
  110.   fclose(archivo);
  111.   fclose(archivo2);
  112.   system("pause>nul");
  113.   break;
  114.   case 2:
  115.          final();
  116.   break;
  117.   default:
  118.          parametros_incorrectos();
  119.   break;    
  120.   }  
  121. }while(op!=2);