{"id":712,"date":"2017-08-10T15:42:11","date_gmt":"2017-08-10T15:42:11","guid":{"rendered":"http:\/\/people.utm.my\/kohmenghock\/?page_id=712"},"modified":"2017-08-10T15:42:11","modified_gmt":"2017-08-10T15:42:11","slug":"codeblocks-simple-guide-writing-message-into-a-file","status":"publish","type":"page","link":"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-writing-message-into-a-file\/","title":{"rendered":"Code::Blocks simple guide &#8211; writing message into a file"},"content":{"rendered":"<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nWe 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 &#8220;result&#8221; file. To perform this task, we will insert a command line given as:<\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><strong>open(unit=1, file=&#8217;result&#8217;,status=&#8217;new&#8217;)<\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThere 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 &#8220;new&#8221; here tells the code that the file is a new one.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nNext, we are going to write the same message as we did for the previous two exercises into this &#8220;result&#8221; file. We will first comment on the previous write and print statements. We then insert the following line into our main.f95 file.<\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><strong>write(1,*) &#8220;Welcome to SSCP 2213 course!&#8221;<\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nThis line tells the code to write this message\u00a0&#8220;Welcome to SSCP 2213 course!&#8221; into the file with unit 1 (i.e. the result file). Here is how the code looks like after these modification.<br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-713 size-large\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file-1024x544.png\" alt=\"\" width=\"1024\" height=\"544\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file-1024x544.png 1024w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file-300x159.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file-768x408.png 768w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/write_statement_into_file.png 1920w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nWe 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 &#8220;result&#8221; in the same folder in which the project is located.<br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-715 size-large\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file-1024x640.png\" alt=\"\" width=\"1024\" height=\"640\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file-1024x640.png 1024w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file-300x188.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file-768x480.png 768w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/08\/locating_result_file.png 1136w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nWe will find in that file the message &#8220;Welcome to SSCP 2213 course!&#8221; &#8211; 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.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nBefore 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 &#8220;new&#8221; 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.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn order to overcome this, you may want to replace the status of the file to &#8220;replace&#8221;. 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<br \/>\n<\/span><\/p>\n<p style=\"text-align: center\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><strong>y = A + B<\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nwhere 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.<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nOn 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).<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn 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.<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;result&#8221; file. To perform this task, we will insert a command line given as: open(unit=1, file=&#8217;result&#8217;,status=&#8217;new&#8217;) There are &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-writing-message-into-a-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Code::Blocks simple guide &#8211; writing message into a file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14180,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-712","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/users\/14180"}],"replies":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":0,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/media?parent=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}