Results 1 to 5 of 5

Thread: Inputting one text file, and writing out another... Can this be done?

  1. #1
    Junior Member gregorywest's Avatar
    Join Date
    Jan 2015
    Location
    Winnipeg Manitoba Canada
    Posts
    3
    Rep Power
    0

    Question Inputting one text file, and writing out another... Can this be done?

    I have hit an issue that should be very simple to deal with.

    I have one text file from the IBM AS/400 with invoice information. Not I could download the information, but that is a lot of work, not the least of whiich is converting EBCDIC to ASCII and getting all the AS/400 ports and security correct. Instead, since I print through a windows computer it is easier to print the invoices to a TXT file.

    Now all I have to do is write a quick down and dirty basic program to read in the data massage it and write out the import file I need.
    My problem is getting the ThinBASIC syntax to open the two files up. I do have 100% control over the file names, "S:\Invoice_Export.exe" and "S:\Invoice_Import.txt" The Export file would be opened read only, and written out to the Import file overwriting any import file that might be there already.

    Basically the program would look like this:

    Open exportfile readonly
    Open importfile overwrite
    write importfile "Header information and formatting"
    Loop: gosub process_invoice
    write importfile invoice_string 'created by process invoice
    if not eof(exportfile) goto Loop
    close exportfile
    close importfile
    end

    function process_invoice
    this is a little complex
    basically a bunch of reads from exportfile and logic to figure out what was read in.
    endfn


    The file itself would look something like:
    S983663523

    8765432

    2015-01-15 23432 432423 4324232 4324224

    01 8373764 A DESCRIPTION LINE 2 123.00 246.00
    MORE DESCRIPTION

    02 948873 AND ON WE GO FOR MULTIPLE LINES 1 123.00 123.00

    I can write the code to deal with what is in the file, just looking for the syntax of how to open, read, write to the TXT files.


    Thanks in advance
    Greg

  2. #2
    Junior Member gregorywest's Avatar
    Join Date
    Jan 2015
    Location
    Winnipeg Manitoba Canada
    Posts
    3
    Rep Power
    0

    WOW this looks very simple compared to other languages

    I started looking at the example code. This one of the examples is exactly what I need. Will just have to massage it a bit and will work wonders for me. Great program guys!!!!

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    you found it already?


    There are two ways, either use the FILE-module and have an arsenal of file-functions available - or you might just use core-functions Load_File/Save_File to read/write the file in one go- they are equivalent to File_Load & File_Save.

    String sData = Load_File(sFilename)
    '...
    Save_File(sFilename, sData)
    
    The data you obtain then is a common dynamic string and you might use string-functions of core-module to modify/parse data. Enhanced you can use "Tokenizer"-module for fast splitting data into separate tokens - but then you might have to re-assemble the string before saving data.

    If you have numerical/fixed size data that follows a certain pattern, thinBasic allows you to use a very simple way to interpret this data as udt.
    Just place some udt dimensioned to the count of records in your data upon your data.

    example:
    ' setup some udt that matches the data-pattern but it must not contain dynamic strings!
    
    Type t_myUDT
      A As Long
      B As Byte
      C As Double
      D As String * 32  ' fixed size strings are allowed
    '...
    End Type
    
    ' get data from file into a string
    String sData = Load_File("C:\myFile.dat")
    
    ' obtain number of data-elements/records
    Long nRecords = Len(sData) / SizeOf(t_myUDT)
    
    ' place the udt upon the string to have an array of single records now:
    Dim record(nRecords) As t_myUDT At StrPtr(sData)
    
    
    ' now can access each single record() of sData.
    
    ' you could add some blank record at the end like this:
    sData &= Repeat$( SizeOf(t_myUDT), MKBYT$(0) ) ' append 0-bytes
    
    ' be aware:
    ' if you change the size of string sData you also have to
    ' Redim record( Len(sData)/SizeOf(t_myUDT) ) At StrPtr(sData)
    
    If your data altogether is very large then you might consider to use "StrPtrLen(StrPtr(sData))" instead of "Len(sData)" - same result but Len() will create a local string thats allocation consumes time and memory while requesting the StrPtrLen of a StrPtr will only peek out a dword in front of the stringpointer.
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    Junior Member gregorywest's Avatar
    Join Date
    Jan 2015
    Location
    Winnipeg Manitoba Canada
    Posts
    3
    Rep Power
    0

    Nice solution!

    That looks good too. Not sure would work for the situation I am in right now, but for the future, yup that would be great.

    The input file I am working with now is between 2K and 700M in size. Could be either extreme, and it can be processes top to bottom, no need to load the entire file at once. But I do like your solution!!

    Greg

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Hi Greg,

    thanks a lot to be here with us and for your donation.

    regarding reading /writing text files, you can see some examples going into \thinBasic\SampleScripts\File\ directory.
    There are some different ways you can read text files, each has some pro/cons depending on what you need to to.

    \thinBasic\SampleScripts\File\File_ReadAndPrintLine.tbasic can be a start.
    It uses easy to use File_LineInput and File_LinePrint functions. The cons is that reading line by line can be slow on big files due to end of line parsing.

    If the input file is fixed length line, binary reading a fixed number of bytes is much more efficient.

    Regarding GOTO ...
    ThinBasic has no GOTO keyword. The main reason is that having GOTO would produce quite unreadable code.
    ThinBasic has a lot of different execution control flow that can be used to place for GOTO.

    Anyway, we are here to help if we can so if any doubt ... post here and we will reply.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. OT: text file question
    By LCSims in forum thinBasic General
    Replies: 5
    Last Post: 25-09-2012, 09:48
  2. Writing down worries boosts performance
    By Charles Pegge in forum Shout Box Area
    Replies: 5
    Last Post: 18-01-2011, 09:27
  3. save text file...
    By Lionheart008 in forum thinAir General
    Replies: 4
    Last Post: 13-05-2009, 22:50
  4. getting a line from text file
    By sandyrepope in forum UI (User Interface)
    Replies: 6
    Last Post: 04-03-2007, 05:36

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •