{"id":6728,"date":"2014-03-10T05:06:06","date_gmt":"2014-03-09T18:06:06","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=6728"},"modified":"2022-04-11T20:14:46","modified_gmt":"2022-04-11T10:14:46","slug":"c-2d-array-via-file-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-2d-array-via-file-tutorial\/","title":{"rendered":"C 2D Array via File Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/File2dArray\/File2dArray.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C 2D Array via File Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/File2dArray\/File2dArray.jpg\" title=\"C 2D Array via File Tutorial\"  \/><\/a><p class=\"wp-caption-text\">C 2D Array via File Tutorial<\/p><\/div>\n<p>You don&#8217;t have to use an IDE to do C programming.  There is <a target=_blank title='Digital Mars' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Digital+Mars'>Digital Mars<\/a> C as an example of that.   We have talked about the <a target=_blank title='Xcode and its Command Line Tools Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=5519'>Xcode command line tools<\/a> before, that frees the gcc compiler to (also) be a command line tool to write C programs from the command line separate to any IDE usage.<\/p>\n<p>So it is today with this tutorial reading a file of numbers that define the workings of a 2D int array the program requires, inspired by this (old) Yahoo Answers <a target=_blank title='Old Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20110702111230AA9XfOi'>question<\/a>.<\/p>\n<p>We use stdlib.h&#8217;s memory management function <a target=_blank title='calloc' href='http:\/\/www.cplusplus.com\/reference\/cstdlib\/calloc\/'>calloc<\/a> to handle the memory management here.   The use of calloc, malloc and realloc can aid with dynamically changeable arrangements with your C program&#8217;s memory management, and you may be interested in the related previous <a target=_blank title='' href='#xdcspt'>Xcode Debug C Strings Program Tutorial<\/a> as shown below.<\/p>\n<p>So feel free to download the C programming source code here and rename it to <a target=_blank title='main.cpp' href='http:\/\/www.rjmprogramming.com.au\/C\/File2dArray\/file_2d_array.c_GETME'>file_2d_array.c<\/a><\/p>\n<p>Hope you enjoy this <a target=_blank title=\"gcc compiling\" href=\"http:\/\/www.rjmprogramming.com.au\/C\/File2dArray\/File2dArray.jpg\">tutorial<\/a> showing you some command line C gcc compilation work.<\/p>\n<p><code>Roberts-MacBook-Pro:tools robertmetcalfe$ cat file_2d_array.c<br \/>\n\/\/ file_2d_array.c ... read input file's data into a 2d array<br \/>\n#include \"stdio.h\"<br \/>\n#include \"stdlib.h\"<br \/>\nint main (int argc, char* argv[]) {<br \/>\nFILE* spIn;<br \/>\nint rownum;<br \/>\nint colnum;<br \/>\nint** table;<br \/>\nint row;<br \/>\nint column;<br \/>\nint value;<br \/>\nspIn = fopen (argv[1], \"r\");<br \/>\nif (spIn == NULL)<br \/>\n{<br \/>\nprintf (\" Did not open haha an\");<br \/>\nexit (1);<br \/>\n} <br \/>\nfscanf(spIn,\"%d\", &#038;rownum);<br \/>\nfscanf(spIn,\"%d\", &#038;colnum);<br \/>\ntable = (int**)calloc ((rownum * colnum), sizeof(int*));<br \/>\nfor (row = 0; row < rownum; row++)<br \/>\n{<br \/>\ntable[row] = (int*)calloc ((rownum * 1), sizeof(int));<br \/>\nfor (column = 0; column < colnum; column++)<br \/>\n{<br \/>\nfscanf(spIn, \"%d\", &#038;value);<br \/>\ntable[row][column] = value;<br \/>\nprintf(\" table[%d][%d] = %d n\", row, column, table[row][column]);<br \/>\n}<br \/>\n}<br \/>\nfclose(spIn);<br \/>\nreturn 0;<br \/>\n}<br \/>\nRoberts-MacBook-Pro:tools robertmetcalfe$ cat file_2d_array.txt<br \/>\n2 3 1 2 3 12 12<br \/>\nRoberts-MacBook-Pro:tools robertmetcalfe$ gcc file_2d_array.c -o file_2d_array<br \/>\nRoberts-MacBook-Pro:tools robertmetcalfe$ ls -l file_2d_array*<br \/>\n-rwxr-xr-x  1 robertmetcalfe  staff  8728  9 Mar 09:00 file_2d_array<br \/>\n-rw-r--r--  1 robertmetcalfe  staff   729  9 Mar 08:59 file_2d_array.c<br \/>\n-rw-r--r--  1 robertmetcalfe  staff    17  9 Mar 08:37 file_2d_array.txt<br \/>\nRoberts-MacBook-Pro:tools robertmetcalfe$ .\/file_2d_array file_2d_array.txt<br \/>\n table[0][0] = 1<br \/>\n table[0][1] = 2<br \/>\n table[0][2] = 3<br \/>\n table[1][0] = 12<br \/>\n table[1][1] = 12<br \/>\n table[1][2] = 12<br \/>\nRoberts-MacBook-Pro:tools robertmetcalfe$<br \/>\n<\/code><\/p>\n<p>A really helpful tutorial for code above is shown <a target=_blank title='Good tutorial' href='http:\/\/stackoverflow.com\/questions\/16715633\/multidimensional-arrays-allocated-through-calloc'>here<\/a> &#8230; thanks.<\/p>\n<hr \/>\n<p id='xdcspt'>Previous <a target=_blank title='' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=5238'>Xcode Debug C Strings Program Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/NonGlobalStrings\/XcodeDebug_C_NonGlobalStrings.m4v\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Xcode Debug C Strings Program Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/NonGlobalStrings\/XcodeDebug_C_NonGlobalStrings.jpg\" title=\"Xcode Debug C Strings Program Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Xcode Debug C Strings Program Tutorial<\/p><\/div>\n<p>Have a bit of an esoteric C programming scenario today, and because it is esoteric, and some or all of you may fall asleep, am also going to show you how to debug in Xcode &#8230; and only now just realized with the podcast of this tutorial that we went, on a Mac, control-command-Y a lot and should have, instead, shown you this in menu manipulation terms as Product->Debug->Continue &#8230; anyway, if you get into this, you&#8217;ll know what is going on.<\/p>\n<p>Okay, so the C scenario in Xcode is a bit esoteric, as sometimes happens when you are answering &#8220;a brief&#8221; (a Yahoo Answers brief).  The brief was not to use <a target=_blank title='strcat' href='http:\/\/www.techonthenet.com\/c_language\/standard_library_functions\/string_h\/strcat.php'><i>strcat<\/i><\/a> in the making of a single string buffer from an array of zero-byte terminated char* strings in an array, with an added stipulation of a maximum string buffer length.   Sounds simple, and it is if you use <i>strcat<\/i> and\/or global strings, but my understanding of the brief would be that <a target=_blank title='Wikipedia information about global variables' href='http:\/\/en.wikipedia.org\/wiki\/Global_variable'>global variables<\/a> will be frowned upon, as a lot of programmers will tell you.   Mind you, personally, global strings are not a problem for me.   Not using global strings and not using <i>strcat<\/i>, and making this work in a self-sufficient single function is quite interesting, and basically that is what we do here with this tutorial, via memory management using <a target=_blank title='malloc' href='http:\/\/www.cplusplus.com\/reference\/cstdlib\/malloc\/'>malloc<\/a> &amp; <a target=_blank title='realloc' href='http:\/\/www.cplusplus.com\/reference\/cstdlib\/realloc\/?kw=realloc'>realloc<\/a>.<\/p>\n<p>So feel free to download the Xcode C programming source code here and rename it to <a target=_blank title='main.cpp' href='http:\/\/www.rjmprogramming.com.au\/C\/NonGlobalStrings\/main.cpp_GETME'>main.cpp<\/a> (or main.c &#8230; is pretty conservative in its construction &#8230; probably would work with Digital Mars C).<\/p>\n<p>Hope you enjoy this <a target=_blank title=\"Xcode debugging\" href=\"http:\/\/www.rjmprogramming.com.au\/C\/NonGlobalStrings\/XcodeDebug_C_NonGlobalStrings.m4v\">tutorial<\/a> showing you some cute Xcode debugging functionality.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d5238' onclick='var dv=document.getElementById(\"d5238\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Xcode\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d5238' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d6728' onclick='var dv=document.getElementById(\"d6728\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Digital+Mars\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d6728' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You don&#8217;t have to use an IDE to do C programming. There is Digital Mars C as an example of that. We have talked about the Xcode command line tools before, that frees the gcc compiler to (also) be a &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-2d-array-via-file-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[103,176,182,478,768,997,1319,1473,1488],"class_list":["post-6728","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-array","tag-c","tag-calloc","tag-gcc","tag-memory-management","tag-programming","tag-tutorial","tag-xcode","tag-yahoo-answers"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6728"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=6728"}],"version-history":[{"count":4,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6728\/revisions"}],"predecessor-version":[{"id":55384,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6728\/revisions\/55384"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=6728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=6728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=6728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}