Diary of an Amiga fanatic

Tag Archives: IDOS->FOpen

In this series called File attributes we will do some C programming related to files. In our first article we will open a file, check if we succeeded and close it again. Really basic stuff but also really cool to learn. When I say really basic stuff I mean in the eyes of experienced programmers. I talked about this before but I will bring it up again. Doing simple examples might seem silly. Why is this useful to me? It’s just opening a file and not programming some really cool demo effects. Remember The Karate Kid? Pointless exercises leading to nothing only to realize they do teach you and make you understand. It is the same with these simple examples. After doing so many you start to see the bigger picture at a certain moment.

We are going to use the Dos library to open our file. Just like the Exec library the Dos library is also already open when OS4.x is booted up. So there is no need for us to open it up and to close it when we are done.

Example text fileI created a file called “example.txt” containing one line of text in it that I use in our example. I place it in the same folder in which we will execute our example code.

One of the problems is to get the source code displayed in such a way that it is readable. To overcome this problem I have the source code file available for download and use screen shots showing parts of the code to explain it. Click here to download the (openfile.c) source code file.

File Attributes - Open File IncludesThe above lines of code are called includes. They include other files with code into our program. For example <stdio.h> includes code that will allow us to use the printf() statement in our program. The <dos/dos.h> include will allow us to use the dos functions to open and close files in our program. Normally I have a lot more in there even if I don’t use them. You might get some errors during the compiling of your code which is caused by not having a certain include file in there that provides the code for a function that you use. You will end up, like me, having a certain set of includes in your code even if you don’t use them. But I did not want to make it difficult by including a lot of includes.

File Attributes - Open File BPTR and startNow we are getting to the start of our program. The line “BPTR fh;” is setting up our file handle. What this means is that once we open our file we will store all the info in “fh”.

The line “int main(void)” plus the open curly bracket is the start of our program.

File Attributes - Open file FH statementNow we are getting to the main part of our program that will open our file. As you can see we will store all our info in “fh”. The statement “IDOS->FOpen” will open our file of choice. As you might have guessed the “example.txt” is the file we are trying to open here. The “MOD_OLDFILE” part indicates we want to open an existing file. There are other access modes but for now we will leave it at this. The last “0” bit has to do with the buffer size. The buffer size will determine how many bytes the stream buffer will have. What? Let’s say you want to move stuff from your house to the car and you have the choice of several boxes each having a different size. Will you pick a small box which can hold just a few items or a really big box that can hold a lot more items? Each has pros and cons. In our example we set it to 0 which means the default value. We won’t do anything with our file like reading or writing to it so it has no influence on our example.

File Attributes - Open File Checking the FileNext up we will have to check if the successfully opened our file. As you can see we use “fh” which will hold all the info including if we managed to open it or not. Actually it will be empty when we failed to open it. So if the value is anything but 0 we managed to open it. The statement “if (fh != 0)” will check if our file handle “fh” is not 0. The “!=” means NOT. If it is NOT 0 we managed to open the file and we will print that we managed to open the file. If we failed the “else” statement will print that we failed to open the file. Sounds rather easy doesn’t?

File Attributes - Open File Closing upWhat’s left to do is to close our file if we managed to open it. The statement “IDOS->FClose” will close our file. Again we use “fh” which is our file handle containing everything regarding our file. It makes sense to close whatever we have opened. The “return 0;” will end the program and let the system know we are done.

If you successfully compiled the program you will get the below output.

File Attributes - Open File OutputThat’s it for the first article regarding files. Next time we will do something else with our file. I hope you enjoyed it and more important learned something. Please let me know if something is not clear or if you have any other questions.