Code::Blocks simple guide – writing message into a file


We shall now look at how to print a message into a specific file. To do this, we need to provide a comment to the code to open a new file. Let us call this file as “result” file. To perform this task, we will insert a command line given as:

open(unit=1, file=’result’,status=’new’)


There are three information given in the command. The first item inside the parenthesis is to tag this file with a reference number. In this case, the reference number is 1. The second item refers to the name of the file. The third item refers to the status of the file. The option “new” here tells the code that the file is a new one.


Next, we are going to write the same message as we did for the previous two exercises into this “result” file. We will first comment on the previous write and print statements. We then insert the following line into our main.f95 file.

write(1,*) “Welcome to SSCP 2213 course!”


This line tells the code to write this message “Welcome to SSCP 2213 course!” into the file with unit 1 (i.e. the result file). Here is how the code looks like after these modification.


We will then build and compile the code as usual. After executing the code, we will see that there is no message appearing in the terminal. There will be instead a file named “result” in the same folder in which the project is located.


We will find in that file the message “Welcome to SSCP 2213 course!” – the very message which we ask the code to write. We can also write values into the file, the very same way we did for the message above. In fact, we can combine characters (e.g. the message above) and values (as in some results from calculations) in one single write statement. We will discuss this in the next discussion.


Before wrapping up this discussion, I would like to mention here that you will run into an error in the case when you try to run the main.f95 code again. This is because the option “new” in the open file statement is meant to ask the code to create a new file. Yet, when the code try to does that, it found that there is already an existing file with the same name.


In order to overcome this, you may want to replace the status of the file to “replace”. With this option, the code will overwrite the message (or value) printed into the file at each execution of the code. This may not be a big deal here since we are writing the same message but it will be of major consequence when we are doing calculations. As an example, let us consider a simple equation

y = A + B


where A and B are parameters in which the values will be given by the user. If at first execution of the code, the values provided are A = 2 and B = 3 thus yielding y = 5. Suppose we write the value of y into a file, then we will see y = 5 after the first execution.


On the second execution, we assume that the user chose A = 6 and B = 3. Therefore, we have y = 9. After the second execution of the code, we will only see y = 9 in the file and no trace of the previous value (i.e. y = 5).


In general, we do not require to know the results of previous code execution. However, it would be worth putting this in mind. There is actually a way to store previous data but we shall keep this for a discussion at a later time.