ARchivos C

Iniciado por mapers, 15 Abril 2011, 07:53 AM

0 Miembros y 1 Visitante están viendo este tema.

mapers

Buenas señores desearia que me ayudaran con este tipo de archivos ,este programa nos paso mi profesor pero necesito mas ejemplso si ustedes tuvieran algunos y  me pudieran explicar algo  les agradeceria bastante .....
estos son  archivos con encabezado........

Código (cpp) [Seleccionar]

#include <cstdlib>
#include <iostream>
#include <IO.h>
#include <string.h>
#include <sys\STAT.h>
#include <FCNTL.h>
using namespace std;
long int pos=0;
int fd = 0,lr,le,x;

struct encabezado{
         int NRS;
         int PR;
       }e;   
struct registro{
         int NR;
         char NOM[20];
         int PTJE;
         int SR;
}n,a,s;

void escribir()
{
  char rpta;
  char band;
  if((fd = creat("a3.txt",S_IWRITE | S_IREAD)) < 0)//devuelve un entero si es q lo logra crear
       {
        cout<<"No se pudo crear"<<endl;
        system("PAUSE");
        exit(0);     
       }
           
  le = sizeof(struct encabezado);     
  lr = sizeof(struct registro);
  lseek(fd,le,0);
  e.PR = -1;
  e.NRS = 0;
 
  do{
      band='F';
      fflush(stdin);
      n.NR = ++e.NRS;
      cout<<"Nombre: ";gets(n.NOM);
      fflush(stdin);
      cout<<"Puntaje: ";cin>>n.PTJE;
      if(e.PR==-1)
          { n.SR=e.PR;
            e.PR=n.NR;
          }
      else
          {
          x=e.PR;   
          while(x!=-1)
                  {
                  pos=(x-1)*lr + le;
                  lseek(fd,pos,0);
                  read(fd,&s,lr);
                  if(strcmp(n.NOM,s.NOM)>0)
                      {band='V'; a=s; x=s.SR; continue;}
                  if(strcmp(n.NOM,s.NOM)<0) x=-1;
                  };
          if(band=='F')
              {n.SR=e.PR;
               e.PR=n.NR;
               lseek(fd,0,2);
              }
          else
              { n.SR=a.SR;
                a.SR=n.NR;
                pos=(a.NR-1)*lr+le;
                lseek(fd,pos,0);
                write(fd,&a,lr);
                lseek(fd,0,2);                             
              }
         }
      write(fd,&n,lr);
      cout<<"INGRESAR MAS REGISTROS (s/n): "<<endl;
      cin>>rpta;
  }while(rpta == 's');
 
  lseek(fd,0,0);
  write(fd,&e,le);
  close(fd);
}

void leer()
{
     if((fd = open("a3.txt",O_TEXT)) < 0)//devuelve un entero si es q lo logra crear
       {
        cout<<"No se pudo abrir"<<endl;
        system("PAUSE");
        exit(0);     
       }
     le = sizeof(struct encabezado);     
     lr = sizeof(struct registro); 
     read(fd,&e,le);
       
     cout<<"PR : "<<e.PR<<" NRS : "<<e.NRS<<endl;
     cout<<"NR\t " << "Nombre\t" << "Ptje\t"<<"SR"<<endl;
     x=e.PR;   
     while(x!=-1)
        {
        pos=(x-1)*lr + le;
        lseek(fd,pos,0);
        read(fd,&a,lr);
        cout<<a.NR<<"\t"<<a.NOM<<"\t"<<a.PTJE<<"\t"<<a.SR<<endl;
        x=a.SR;
        }
     close(fd);   
      }



int main(int argc, char *argv[])
{
    int op;
    do{ cout<<"LISTA ENLAZADA ORDENADA"<<endl;
        cout<<"1.escribir  2.leer   3.Salir"<<endl;
        cin>>op;
        switch(op){
                   case 1:    escribir();break;
                   case 2:    leer();break;
                   case 3:    break;
                   };
        }while(op!=3);
    system("PAUSE");
    return EXIT_SUCCESS;
}