简体   繁体   English

如何将二维数组保存到C中的文件?

[英]How to save a 2-Dimensional array to a file in C?

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(eg main.c, header_file.h etc.) 我是一名初学者C程序员,目前正在从事使用C实现中提琴琼斯对象检测算法的项目。我想知道如何将二维数组中的数据存储到可以轻松实现的文件中通过不同的程序文件(例如main.c,header_file.h等)移植和访问

Thank you in advance. 先感谢您。

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. 正如其他人所建议的那样,这将为您留下.CSV文件,这是一个不错的选择,因为它易于读取和解析,并且您可以在记事本或Excel中打开文件并查看它没有问题。

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. 这是假设您确实打算使用C文件I / O来完成此操作,这是一种非常有效的处理方式,有些人只是觉得它有些陈旧。

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. 提示:它涉及在第二个for循环内的条目之前打印逗号,减少要在内部for循环中迭代的条目数量,并在内部for之前和之后立即打印出每行特殊的第一个和最后一个情况循环。 Harder to explain that to do, probably. 可能很难解释这一点。

Here is a reference for C-style file I/O, and here is a tutorial . 这是 C风格文件I / O的参考, 这是一个教程

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). 您需要选择一个定界符来分隔元素(制表符或空格是常见选择,分别是“ tsv”和“ csv”),然后用一些标记行尾(在这里换行是一个不错的选择) 。

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! 从简单开始,并根据需要添加!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM