News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

fileio questions

Started by lugnut, October 07, 2008, 09:59:04 PM

Previous topic - Next topic

lugnut

I found the sample file i/o code, but I couldn't find how to solve this particular issue. I will be reading a Windows-type file that is basically text. It has sets of three records. The first two are names (like "John Smith"), and then a 14 digit number (still in character format). Each record ends with CR/LF (X'0A and X'0D).

My first program is just to read this data into memory and count the sets of data. Then I will add code that translates this data to EBCDIC (the HLA text was excellent!). It must be written out to a binary file (no x'0a/x'0d at line end). The amount of data varies with the name lengths, and this length must be inserted in the output in "IBM" form. There is only one record for each set of three input records.

This is language #4 attempt for me. C will do this but I can't beat the compiler into submission.

For now I need to know how to read a "full line" into a storage location, and count the number of records read. Any hints on how to define this area for I/O and the counter field?

Sevag.K


The hla standard library has functions for reading lines of data from a file, along with many other related functions.  You can examine the sources to see how its done.

If you want to read the entire file to memory, you can use code similar to this:


fileio.open (fileName, fileio.r);
mov (eax, hfile);  // store the file handle to close it later
filesys.size (hfile);
mov (eax, size);
        mem.alloc (eax);
        mov (eax, hfem)
fileio.read ( hfile, [eax], size);
fileio.close (hfile);


From that point,  'hmem' will point to the first character of the file in memory and it will be of length 'size'

There are also several string-memory scanning functions in my hidelib library (actually, they work with any memory pointer that is zero-terminated), but those, along with the functions in the hla standard library are more for ascii related files, if you have binary data you will need to write some custom routines.  If you have mixed ascii/binary, you could also check out the sources for tBuffer class in hidelib, though that may be a bit complicated at your current level.

Standard library sources are at sourceforge:
https://sourceforge.net/projects/hla-stdlib/

hidelib sources are at my site:
http://sevag.krikorian.googlepages.com/hlaresources

lugnut

Perhaps my question is too nebulous. Let me try again.

In the HLA sample files, there is a demo program named 'SampleFileInput2' which contains the following statement:
 fileio.get( inputHandle, u );
 


This statement is reading one number at a time from the input file. My file is delineated by cr/lf (this is win/xp), and I need to read the entire line. How do I do that?

Sevag.K

Quote from: lugnut on October 08, 2008, 02:44:02 AM
Perhaps my question is too nebulous. Let me try again.

In the HLA sample files, there is a demo program named 'SampleFileInput2' which contains the following statement:
 fileio.get( inputHandle, u );
 


This statement is reading one number at a time from the input file. My file is delineated by cr/lf (this is win/xp), and I need to read the entire line. How do I do that?

The fileio.get macro does what you ask, you supply a string with enough storage for 'u'

Alternatively, you can use specific functions:

fileio.gets( inputHandle, s );  // you supply a string 's' with enough storage to read a line
fileio.a_gets( inputHandle ); // HLA creates a string on the heap and returns a pointer in EAX

You will then have to scan for individual components of the record in the string.  If you design the records a different way, by using an NL (new line) for each field, it would be easier to read in the fields, for example, you could have a loop with:

fileio.gets( inputHandle, firstName );
fileio.gets( inputHandle, lastName );
fileio.gets( inputHandle, idNumber );

... a little slower, but more convenient.


lugnut

Thanks.

Now I'm headed in the right direction, at least.

I was very confused about the creation of a receiving field for data. The name "static" implied "does not change" to me, so I made some bad assumptions.

I have no choice about the input data format. The data does come in 3 record sets. The first record is a 'name', but this name may contain all sorts of legalise. The second line is also a name, or it may contain '^??^' indicating that there is only one name. The third record contains a 14 digit number (all character representation). I will do no arithmetic on this. I will take this information and build a variable length hex data field. The ASCII names will be translated to EBCDIC, and there will be several fields where 'length' is inserted. This length is 1 byte, but there is one place where it is two bytes. The length must be in "IBM" format. I understand that IBM machines store data from most significant bit to least significant, whereas Intel machines do not. I will build a table to do this as well. The big thing is this hex data record may contain embedded X'00 data, so I have to be able to write by length.

The next obstacle is writing binary files. The usual way is to write this 'data' with two I/O's. The first would send 3 bytes (X'5A + 2 byte length). The second sends the actual data. At least that's the way I did it in C.

Thanks again.

Thanks for your assistance