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.
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.
21 Responses to C 2D Array via File Tutorial