File Text – Ejemplo 2

Enunciado

Escribir un programa que lea datos desde un archivo de texto y los almacene en un archivo directo. Cada línea del archivo de texto contiene un valor numérico y una cadena de caracteres. Cada linea del archivo directo debe tener la siguiente estructura: código numérico tipo longint – descripción tipo string[ 10 ]

Código

Python

import os
import pickle

def main():
    print( "Nombre archivo lectura/", end="" )
    print( "Read file name: " )
    nomtexto = input( )
    
    if not os.path.isfile(nomtexto):
        print( "Archivo inexistente/", end="" )
        print( "File does not exist\n" )
        input( "Presionar/Press Enter to exit " )
    else:
        texto = open(nomtexto, "r")
        print( "Nombre archivo escritura/", end="" )
        print( "Write file name: " )
        nomarch = input( ) 
        arch = open(nomarch, "wb")
        c = 0
        rec = texto.readline().strip()
        while rec != "":
            c += 1
            print( rec ) 
            pickle.dump( rec, arch ) 
            rec = texto.readline().strip()

        arch.close() ;
        texto.close() ;
        
        # Prueba de lectura del archivo directo
        # Direct file reading test
        print()
        arch = open(nomarch, "rb")
        for i in range ( c ):
            rec = pickle.load( arch )
            print( rec )
            
        arch.close()
    input( "Presionar/Press Enter to exit " )
main()

C++

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std ;
#include <conio.h>

struct registro
{
    long int clave ;
    char nom[10] ;
};

int main()
{
    registro rec ;
    string nomtexto, nomarch ;
    string linea ;

    cout << "Nombre archivo lectura/Read file name: " ; 
    cin >> nomtexto ;
    ifstream texto(  nomtexto.c_str() ) ;
	
    if (!texto)
    {
	cout << "Archivo inexistente/" ;
	cout << "File does not exist" << endl ;
    }
    else
    {
       cout << "Nombre archivo escritura/" ;
       cout( "Write file name: " ; 
       cin >> nomarch ;
       ofstream arch( nomarch.c_str(), ios::binary ) ;
       do
       {
   	  texto >> rec.clave >> rec.nom ;
   	  cout << rec.clave << setw(10) 
               << rec.nom << endl ;
   	  arch.write( (char*)&rec, sizeof(registro) ) ;
	} while( !texto.eof() && !arch.fail() ) ; 
		
	arch.close() ;
	texto.close() ;
    }
	
    // Prueba lectura del archivo directo
    // Direct file reading test
    if (texto)
    {
	cout << endl ;
	ifstream arch( nomarch.c_str(), ios::binary );
	do
	{
	   arch.read( (char*)&rec, sizeof(registro) ) ;
	   if ( arch.good() )
	      cout << rec.clave << setw(10) 
                   << rec.nom << endl ;
	} while( !arch.eof() ) ;
	arch.close() ;
    }
    cout << endl ;
    cout << "Presionar/Press Enter to exit " ;
    getch() ;
    return 0 ;
}

Pascal

Program Problema21_2 ;
uses CRT ;
type
   registro = record
                 clave: longint ;
                 nom: string[10]
              end ;
   archivo = file of registro ;
var
   arch: archivo ;
   texto: text ;
   rec: registro ;
   nomtexto, nomarch: string[ 12 ] ;

begin
   ClrScr ;
   write( 'Nombre archivo lectura/Read file name: ' ) ; 
   readln( nomtexto ) ;
   assign( texto , nomtexto ) ;
   reset( texto ) ;
   write( 'Nombre arch escritura/Write file name: ' ) ; 
   readln( nomarch ) ;
   assign( arch , nomarch ) ;
   rewrite( arch ) ; 
   while not EOF( texto ) do
   begin
	read( texto , rec.clave , rec.nom ) ;
	writeln( rec.clave,' ', rec.nom ) ;
	write( arch , rec ) ;
   end ;
   close( texto ) ; close( arch ) ;
	
   { Prueba lectura del archivo directo 
     Direct file reading test }
   writeln ;
   reset( arch ) ; 
   while not EOF( arch ) do
   begin
	read( arch , rec ) ;
	writeln( rec.clave,' ', rec.nom ) ;
   end ;
	
   writeln ;
   writeln( 'Presionar/Press Enter to exit ' ) ;
   readln ;
end.

Diagramas