In our previous tutorial in the “File Attributes” series we opened a file and printed if we managed to open it or not. In this tutorial we are going to obtain some information of a certain file. We will obtain it’s name, comment info and the file size.

Comment example

If you are not familiar with the comment part you just have a look at the above picture. For this example I created a file called “example.txt” for which I added a comment as well. Make sure you have this file in the same folder in which you would run the example.

Click here to download the (fileinfo.c) source code file.

In order for us to get this info we will use the function “ExamineObject” from the DOS Library. As you know we don’t have to open the DOS Library but we also don’t have to open our file to get the info we want. On to the show!

File Attributes - File info

Line 8 is setting up our ExamineData structure called “data”. In here all the info of our file will be stored if we succeed to get this. And as you might have guessed we will also use this to check if we managed to get all the file info.

Line 13 will try to get all the info of our file. IDOS->ExamineObjectTags is the function that will obtain this. It will require us to tell it a few things. EX_StringNameInput is telling it that the file we want to use is not open and will be given in a text string which we will provide in the next part. I don’t want to make it to complicated but we can also tell it to use a file that is already open and if we would we would use for example EX_FileHandleInput. Next up is indeed the name of our file in a text string. TAG_END will tell it that we have reached the end of our instructions. So what we are doing is get the info from a file called “example.txt” which is not currently open and store it in a box called data.

Line 15 will check if we succeeded in doing this. Our pointer (or box if you like) called data will be empty if we failed and full if we succeeded. Line 22 till 28 will take care of the part if it failed. If you want to see if this part actually works if it does fail try to change the name of the file (in the source code or the actual file) and you will be greeted by the failure message.

Line 18 till 20 will get the info we want. As you might have guess line 18 will get the name, line 19 the comment info and line 20 the files size. This will be printed on the screen and in my case I will be greeted with the below info.

File Attributes - Outcome

There is one more part left which is what we are doing on line 30. We need to cleanup and on line 30 we are freeing up what we have used using the IDOS->FreeDosObject function. We are telling it that we have DOS info in our data box which we no longer need so dispose of it. That’s it! Please let me know if something is not clear, you can’t get it to work or you have any other questions. I hope you learned something and until next time; Happy coding!