C 2D Array via File Tutorial

C 2D Array via File Tutorial

C 2D Array via File Tutorial

You don’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 command line tool to write C programs from the command line separate to any IDE usage.

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 question.

We use stdlib.h’s memory management function calloc to handle the memory management here. The use of calloc, malloc and realloc can aid with dynamically changeable arrangements with your C program’s memory management, and you may be interested in the related previous Xcode Debug C Strings Program Tutorial as shown below.

So feel free to download the C programming source code here and rename it to file_2d_array.c

Hope you enjoy this tutorial showing you some command line C gcc compilation work.

Roberts-MacBook-Pro:tools robertmetcalfe$ cat file_2d_array.c
// file_2d_array.c ... read input file's data into a 2d array
#include "stdio.h"
#include "stdlib.h"
int main (int argc, char* argv[]) {
FILE* spIn;
int rownum;
int colnum;
int** table;
int row;
int column;
int value;
spIn = fopen (argv[1], "r");
if (spIn == NULL)
{
printf (" Did not open haha an");
exit (1);
}
fscanf(spIn,"%d", &rownum);
fscanf(spIn,"%d", &colnum);
table = (int**)calloc ((rownum * colnum), sizeof(int*));
for (row = 0; row < rownum; row++)
{
table[row] = (int*)calloc ((rownum * 1), sizeof(int));
for (column = 0; column < colnum; column++)
{
fscanf(spIn, "%d", &value);
table[row][column] = value;
printf(" table[%d][%d] = %d n", row, column, table[row][column]);
}
}
fclose(spIn);
return 0;
}
Roberts-MacBook-Pro:tools robertmetcalfe$ cat file_2d_array.txt
2 3 1 2 3 12 12
Roberts-MacBook-Pro:tools robertmetcalfe$ gcc file_2d_array.c -o file_2d_array
Roberts-MacBook-Pro:tools robertmetcalfe$ ls -l file_2d_array*
-rwxr-xr-x 1 robertmetcalfe staff 8728 9 Mar 09:00 file_2d_array
-rw-r--r-- 1 robertmetcalfe staff 729 9 Mar 08:59 file_2d_array.c
-rw-r--r-- 1 robertmetcalfe staff 17 9 Mar 08:37 file_2d_array.txt
Roberts-MacBook-Pro:tools robertmetcalfe$ ./file_2d_array file_2d_array.txt
table[0][0] = 1
table[0][1] = 2
table[0][2] = 3
table[1][0] = 12
table[1][1] = 12
table[1][2] = 12
Roberts-MacBook-Pro:tools robertmetcalfe$

A really helpful tutorial for code above is shown here … thanks.


Previous Xcode Debug C Strings Program Tutorial is shown below.

Xcode Debug C Strings Program Tutorial

Xcode Debug C Strings Program Tutorial

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 … 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 … anyway, if you get into this, you’ll know what is going on.

Okay, so the C scenario in Xcode is a bit esoteric, as sometimes happens when you are answering “a brief” (a Yahoo Answers brief). The brief was not to use strcat 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 strcat and/or global strings, but my understanding of the brief would be that global variables 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 strcat, 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 malloc & realloc.

So feel free to download the Xcode C programming source code here and rename it to main.cpp (or main.c … is pretty conservative in its construction … probably would work with Digital Mars C).

Hope you enjoy this tutorial showing you some cute Xcode debugging functionality.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , . Bookmark the permalink.

21 Responses to C 2D Array via File Tutorial

  1. Only a smiling visitant here to share the love (:, btw outstanding design and style. “Make the most of your regrets… . To regret deeply is to live afresh.” by Henry David Thoreau.

  2. This can be really serious, You’re an incredibly efficient writer. I have joined with your feed and also will enjoy finding your extraordinary write-ups. In addition, I have shared your weblog post with our social networking sites.

  3. I truly wanted to jot down a brief remark to be able to express gratitude to you for all the splendid items that you are posting here. My incredibly lengthy web investigation has at the end been compensated with reasonable points to exchange with my colleagues. I would tell you that a lot of of us readers really are undeniably lucky to exist in a remarkable web-site with numerous perfect men and women with really valuable tips. I feel truly grateful to have used your entire web site and appear forward to some far more superb minutes reading right here. Thank you again to get a lot of items.

  4. Kissable says:

    sure, research is paying off. Is not it fantastic whenever you find a fantastic submit? My personal web browsings seem full.. thank you. So content to possess identified this submit..

  5. Fantastic thought processes you have here.. Isn’t it good whenever you uncover a great article? Supporting the page.. thanks alot :) So happy to get found this article..

  6. write says:

    Valuable information. Fortunate me I found your website by chance, and I’m shocked why this accident didn’t took place in advance! I bookmarked it.

  7. There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further.

  8. Toothache says:

    I needed to compose you this little note to finally thank you very much yet again just for the unique opinions you have discussed on this site. It is quite remarkably open-handed with people like you to give publicly all most people could have advertised for an e book to end up making some bucks for their own end, most importantly since you could possibly have done it if you ever decided. The thoughts as well worked like a great way to know that the rest have similar dream much like my very own to learn a little more in respect of this issue. Certainly there are lots of more enjoyable periods in the future for individuals who look over your website.

  9. Maintain websiteing stuff like this I actually am fond of it

  10. You will find some intriguing cut-off dates on this write-up but I don know if I see all of them center to heart. There is some validity nonetheless I will take maintain opinion until I appear into it further. Excellent article , thanks and we want extra! Added to FeedBurner as effectively

  11. Whiz says:

    certainly, research is paying off. yes, research is having to pay off. Great thoughts you might have here.. certainly, research is paying off.

  12. My personal web browsings seem complete.. thank you. My web browsings seem full.. thank you. Enjoying the posting.. best wishes Fantastic ideas you’ve got here..

  13. Definitely believe that which you stated. Your favorite reason seemed to be on the internet the easiest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks

  14. You might have brought up a extremely fantastic points , regards for the post.

  15. What i don’t understood is in truth how you are not actually a lot more smartly-appreciated than you might be right now. You’re so intelligent. You realize thus significantly in terms of this topic, made me personally believe it from a lot of varied angles. Its like men and women aren’t interested unless it is something to accomplish with Girl gaga! Your individual stuffs outstanding. Always handle it up!

  16. I would like to thank you for the efforts you’ve put in writing this site. I am hoping the same high-grade site post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own site now. Actually the blogging is spreading its wings rapidly. Your write up is a good example of it.

  17. Some genuinely quality content on this internet site , saved to favorites .

  18. Just desire to say your article is as astounding. The clearness in your post is simply great and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.

  19. I simply desired to appreciate you all over again. I’m not certain what I would’ve gone through in the absence of these tips revealed by you directly on my area. It was before a real frightening setting for me, however , seeing your specialized approach you dealt with that took me to jump for contentment. I’m happy for the service and even have high hopes you comprehend what a powerful job you are getting into educating the mediocre ones thru your web site. I know that you’ve never met any of us.

  20. Enjoyed looking through this, very good stuff, regards . “Love begets love, love knows no rules, this is the same for all.” by Virgil.

  21. Hi there very nice site!! Guy .. Excellent .. Superb .. I will bookmark your site and take the feeds additionally…I’m satisfied to find so many helpful information here in the submit, we want develop extra techniques on this regard, thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>