Leer TXT

Moderator: Rathinagiri

Post Reply
joposadas

Leer TXT

Post by joposadas » Mon May 06, 2013 4:16 am

Grupo,

Tengo que leer un archivo TXT, separado por " (comillas), hasta el momento he hecho esto:
#INCLUDE <Fileio.ch>
#INCLUDE <hmg.ch>
#INCLUDE <PS_Cnfg.ch>

PROCEDURE ReadTxtFile
LOCAL LsTxtFile:= ""
LsMyDocumentsFolder := "'"+GetMyDocumentsFolder()+"'"
SetCurrentFolder(LsMyDocumentsFolder)
LsTxtFile := "'" + GETFILE ({{"Archivo TXT a procesar","MAIN_*.*"}},"Archivo TXT a procesar", &LsMyDocumentsFolder, .F., .T.) + "'"
MsgInfo (LsTxtFile,"1")
MsgInfo (&LsTxtFile,"2")
IF !FILE(&LsTxtFile)
MsgInfo ("Archivo no encontrado o se canceló la opción.","Lectura de TXT")
RETURN(NIL)
ENDIF
CursorWait()
nManejador := FOPEN("MAIN_ART.txt", FO_READWRITE + FO_SHARED)
IF FERROR() != 0
MsgInfo ("No se puede abrir el fichero, error DOS ") // STR(FERROR()))
BREAK
ELSE
MsgInfo ("Archivo Localizado")
//Frm_Verificador.Edt_TxtFile.Value := nManejador
MsgInfo (str(nManejador))
ENDIF

RETURN

Ahora mi problema es saber leer el contenido del archivo y luego leer linea por linea,

no tengo idea de como hacerlo, ¿alguien me puede ayudar?

De antemano muchas gracias por la ayuda y atención a la presente.

User avatar
CalScot
Posts: 232
Joined: Thu Mar 21, 2013 12:22 am
Location: California
Been thanked: 1 time

Post by CalScot » Mon May 06, 2013 6:26 am

Hi, joposadas.

Have you tried using MEMOREAD()? If the text file contains carriage returns (CR) and line feeds (LF), you may also want to use MEMOTRAN().

Full details for these functions are in the CA-Clipper 5.3 section of the HMG Reference help files.

Hope that helps!
CalScot

User avatar
esgici
Posts: 4336
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 221 times
Been thanked: 77 times
Contact:

Post by esgici » Mon May 06, 2013 7:17 am

Code: Select all

TFileRead()

Read a file one line at a time
---------------------------------------------------------------------------------

 Syntax

        oFile := TFileRead():New( <cFileName> [, <nReadSize> ] )  

 Arguments

        <cFileName>   is the required name of the file to be read.    

        <nReadSize>   is the optional size to use when reading from the 
                  file. The default value is 4096 and the allowed range is
                  1 through 65535.  Any value outside of this range causes
                  the default value to be used.

 Returns

       An  instance of the File Reader class    

 Description

      TFileRead() is used to access a file one line at a time. You must
      specify the name of the file when an instance of the class is
      created.
      The class data should be considered private to the class.

      The class methods are as follows:

      New()              Creates a new instance of the TFileRead class.

      Open([<nFlags>])   Opens the file for reading. The optional nFlags
      parameter can use any of the FOPEN() flags from  fileio.ch. The
      default is FO_READ + FO_SHARED.  Calling this method when the file
      is already  open causes the next ReadLine() to start over  from the
      beginning of the file.

      Close()            Closes the file.

      ReadLine()         Returns one line from the file, stripping the
      newline characters. The following sequences are  treated as one
      newline: 1) CR CR LF; 2) CR LF;  3) LF; and 4) CR. Note: LF CR is 2
      newlines.
      Name()             Returns the name of the file.

      IsOpen()           Returns .T. if the file is open.

      MoreToRead()       Returns .T. if there are more lines to be read
      (think of it as an inverse EOF function).

      Error()            Returns .T. if an error has occurred.

      ErrorNo()          Returns the current error code.

      ErrorMsg([<cPre>]) Returns a formatted error message.

 Examples

      #ifdef __HARBOUR__
       #define NEW_LINE CHR( 10 )
      #else
       #define NEW_LINE CHR( 13 ) + CHR( 10 )
      #endif
      #include "fileio.ch"

      PROCEDURE Main( cFile )
      LOCAL oFile := TFileRead():New( cFile )

         oFile:Open()
         IF oFile:Error()
            QOUT( oFile:ErrorMsg( "FileRead: " ) )
         ELSE
            WHILE oFile:MoreToRead()
               OUTSTD( oFile:ReadLine() )
               OUTSTD( NEW_LINE )
            END WHILE
            oFile:Close()
         END IF
      QUIT

Viva INTERNATIONAL HMG :D

Post Reply