We help IT Professionals succeed at work.

Reading .DAT file

ampapa asked
I am exporting a file from another program to be read into MSFlexGrid in my program. When the file is exported from my other program it defaults to a .DAT file. When I read this file with the Open Input #1 in my program it reads the entire contents of the .DAT file as 1 line? Why is this? If I open the .DAT file in WordPad and the save the .DAT file, my program reads the file a line at a time and works correctly.

Can someone enlighten me on what's going on and how I can read this file without having to open it in WordPad before running it through my program.


Watch Question

Head of Software Services
This is because the file is being saved with only line feed characters at the end of each line (or only carriage return). When you open it in wordpad, the LineFeed is being converted to CarriageReturn + LineFeed which is then being correctly interpreted. As a workaround, you could read the whole file into a string and then use the split function:

Dim aryLines() As String
aryLines = Split(TextFromFile,Chr(10))

Then each element of the array represents a seperate line and you can process the contents from there.


I've never heard of the Split() function. What are the parameters and what library is it in?

As Tim said earlier

Dim DataArray() As String
DataArray = Split(ResFlag, ",", , vbTextCompare)

Split(expression [, delimiter [, count [, compare]]])

delimiter- is the character that separates the substrings, space character is used if omitted.

count- indicates the number of substings to return
     defalut is -1 all strings returned

compare- inidates the comparision method used when evaluating
     vbBinaryCompare - 0
     vbTextCompare - 1

The function I believe is refernced in the Visual Basic For Applications library(MSVBVM60.DLL) that is what I have referenced in my project

Hope this helps.


I'm running VB% is why the Split() function is not available. I will edit my code on a VB6 machine and re-run my code.


Explore More ContentExplore courses, solutions, and other research materials related to this topic.