‎2016 Jan 25 2:21 PM
Hello gurus,
I would like to know the following:
If I have to open a file (with 'OPEN DATASET [...]'), how do I know whether it is a text or a binary one?
If it is a text one, I have to perform some tasks; and if it's a binary one, I have to do others.
I think a command in Tx. 'SM69' could be used but... Isn't any other easier way available?
Thanks a lot!,
Eloi
‎2016 Jan 25 2:51 PM
Hi,
The only sure way is to read the data and then compare it with some arbitrary values to see if any of the data is outside of what is considered a text file.
Are the files structured in anyway ? do they have header records or flags ?? Is the byte at the start of a binary file always (for example) 'FF' ?
If your system is running on Linux or Unix you could create an SM69 command for the command 'file' and then parse the output of that which will tell you
Check to see if there are any 0a0d byte combinations - binary files shouldn't contain linefeed/carriage return pairs.
Regards
Rich
‎2016 Jan 25 2:51 PM
Hi,
The only sure way is to read the data and then compare it with some arbitrary values to see if any of the data is outside of what is considered a text file.
Are the files structured in anyway ? do they have header records or flags ?? Is the byte at the start of a binary file always (for example) 'FF' ?
If your system is running on Linux or Unix you could create an SM69 command for the command 'file' and then parse the output of that which will tell you
Check to see if there are any 0a0d byte combinations - binary files shouldn't contain linefeed/carriage return pairs.
Regards
Rich
‎2016 Jan 25 3:07 PM
Hello Eloi,
Even though the file is binary or text (at the source), the file type at the target is determined by what mode you are using for 'OPEN DATASET' to read the file.
- A file can be opened in Binary or ASCII irrespective of what is the source format.
I understand you are trying do the following.
IF BIN_FILE..
Do XXXXX.
ELSEIF TEXT_FILE.
Do XXXXX.
ENDIF.
If I were to do the above, I will do it the following way.
OPEN DATASET file_path FOR INPUT IN BINARY MODE.
IF successfully_opened.
<<Do what you want to do when the file is opened in binary mode>>
ENDIF.
or
OPEN DATASET file_path FOR INPUT in TEXT MODE ENCODING <UTF-8>
IF successfully_opened.
<<Do what you want to do when the file is opened in TEXT mode>>
ENDIF.
Yet, if you want to know the file type ( before using 'OPEN DATASET'), you can use the FM PC_SPLIT_COMPLETE_FILENAME - presuming the file exists with an extension!
Best Regards,
Pradeep.
‎2016 Jan 25 3:12 PM
Hi,
"Yet, if you want to know the file type ( before using 'OPEN DATASET'), you can use the FM PC_SPLIT_COMPLETE_FILENAME - presuming the file exists with an extension!"
And assuming the extension doesn't lie. A common one is the use of XLS to denote Excel files. A lot of people use XLS to denote a file readable by Excel - a subtle difference. Native XLS is generally considered a binary file. Whereas it may contain for example tab or comma delimited ascii files (in otherwords a text file). So don't rely on the file extension.
Rich
‎2016 Jan 28 8:20 AM
Thanks for your help Pradeep but I may even don't have any extension at all in the file!
That is why I need to know which kind of file is prior to opening it.
Eloi
‎2016 Jan 28 8:34 AM
Even if you know all the files you receive are text - you have to know something about the structure to even begin to parse it. It's simply not possible to write a generic program that can read anything.
You know where the data is coming from, you know what application is writing it, you know quite a lot, probably, about the contents. Why wouldn't you know whether it is binary?
What is your scenario? Why don't you know?
‎2016 Jan 28 10:38 AM
Hi Matthew,
I know that if I receive a text file have to proceed in one way and if I receive a binary file have to proceed in the other.
The problem is that there is not any clue available in file name (or size) that determines one path or the other.
I have a server directory full of files and have to be attached as GOS in their corresponding documents (but some of them might be just text files). I know, at least, to which document has the file to be associated.
Thanks a lot for your contribution,
Eloi
‎2016 Jan 28 10:41 AM
The only real way to proceed is to read some of thedata in the file. See my first answer.
Rich
‎2016 Jan 28 10:55 AM
‎2016 Jan 28 11:27 AM
There is no sure way to determine this.
Why don't you always attach it in GOS as binary file?
Let the OS/extension settings to decide how to open that file later.
‎2016 Jan 28 9:46 AM