0

I am an beginner C programmer and I am currently working on a project to implement viola jones object detection algorithm using C. I would like to know how I would be able to store data in a 2-Dimensional array to a file that can be easily ported and accessed by different program files(e.g. main.c, header_file.h etc.)

Thank you in advance.

medz91
  • 105
  • 3
  • 13
  • 1
    This link might help: http://stackoverflow.com/questions/17721186 What your looking for is called serialize and un-serialize. One of the options suggested is json. I use json in PHP a lot and find it very handy. – TecBrat Nov 17 '13 at 03:01
  • Do you want to store it in text or binary? – gongzhitaao Nov 17 '13 at 03:06
  • Can you tell us more about your data? Is it integers, text or objects? – alexroussos Nov 17 '13 at 03:08
  • You're asking the wrong question. The question you should be asking is, "if I were going to initialize a 2d array from a file, what format of the file would make my job easiest?" Once you answer that question, you've answered this question. With that said, the structure of a `.csv` file is essentially a 2d array... – nhgrif Nov 17 '13 at 03:08
  • Store it as text. Format is your choice, but i'd personally go with a csv. Unless you're porting to an ebcdic platform (and I strongly doubt you are), you just solved 99% of your portability woes. – WhozCraig Nov 17 '13 at 03:22
  • @alexroussos i will be storing floating point data. – medz91 Nov 17 '13 at 09:35

2 Answers2

1

There's not quite enough detail to be sure what you're looking for, but the basic structure of what you want to do is going to look something like this:

open file.csv for writing
for(iterate through one dimension of the array using i)
{
for(iterate through the other dimension of the array using j)
   { 
   fprintf(yourfilehandle,"%d,",yourvalue[i][j]);
   }
fprintf(yourfilehandle,"\n");
}
close your file

As has been suggested by others, this will leave you with a .CSV file, which is a pretty good choice, as it's easy to read in and parse, and you can open your file in Notepad or Excel and view it no problems.

This is assuming you really meant to do this with C file I/O, which is a perfectly valid way of doing things, some just feel it's a bit dated.

Note this leaves an extraneous comma at the end of the line. If that bugs you it's easy enough to do the pre and post conditions to only get commas where you want. Hint: it involves printing the comma before the entry inside the second for loop, reducing the number of entries you iterate over for the interior for loop, and printing out the first and last case of each row special, immediately before and after the inner for loop, respectively. Harder to explain that to do, probably.

Here is a reference for C-style file I/O, and here is a tutorial.

kmort
  • 2,848
  • 2
  • 32
  • 54
  • Thanks for this I will read more on this. Also I am planning to port it to a digital signal processor so will .csv files work properly ? – medz91 Nov 17 '13 at 09:38
  • Does your DSP even have a filesystem? Are you programming it in C or assembly? DSPs are meant to process signals not parse text files so given how low-level DSPs are, you'll probably want to define your format at the byte level. – alexroussos Nov 17 '13 at 16:25
  • @alexroussos is right about DSP boards. I'd check to make sure you can write files to it. I've seen some that take C and do have filesystems, so this is not out of the realm of possibility, but it's certainly not guaranteed. – kmort Nov 17 '13 at 23:42
  • The DSP am using is part of a development board which runs on a cortex A8 processor. The board is running on ubuntu 10.04. With this setup wil l it work with CSV files? And will they be read fast ? – medz91 Nov 18 '13 at 14:44
  • @medz91 If you can write out to some mounted file system, then yes, this will absolutely work. One other thing to consider is line endings. Most of the CSV files I've used have `\r\n` line endings, but it is not required. – kmort Nov 18 '13 at 14:56
  • @kmort Thanks a lot this will surely help me a lot – medz91 Nov 18 '13 at 15:38
  • @medz91 No problem. When you're satisfied with an answer, click the check mark next to it to select it as the answer. This give points to the person who answered it, but more importantly allows folks who find your question by searching the internet to know what successfully answered your question. Good luck. :-) – kmort Nov 18 '13 at 15:52
0

Without knowing anything about what type of data you're storing, I would say to store this as a matrix. You'll need to choose a delimiter to separate your elements (tab or space are common choices, aka 'tsv' and 'csv', respectively) and then something to mark the end of a row (new line is a good choice here).

So your saved file might look something like:

10 162 1 5
7 1 4 12
9 2 2 0

You can also define your format as having some metadata in the first line -- the number of rows and columns may be useful if you want to pre-allocate memory, along with other information like character encoding. Start simple and add as necessary!

alexroussos
  • 2,671
  • 1
  • 25
  • 38