Iphone 如何在iOS中将双[]存储到文件或任何持久性文件中?
我有一个双[n][20],其中n可能最多为200。如何将其存储到文件中?Iphone 如何在iOS中将双[]存储到文件或任何持久性文件中?,iphone,objective-c,ios,persistence,Iphone,Objective C,Ios,Persistence,我有一个双[n][20],其中n可能最多为200。如何将其存储到文件中?#include//for open(),close() #include <fcntl.h> // for open(), close() #include <unistd.h> // for write() int fd = open(path, O_WRONLY|O_BINARY); // system call write(fd, d, sizeof(double)*n*20); close
#include//for open(),close()
#include <fcntl.h> // for open(), close()
#include <unistd.h> // for write()
int fd = open(path, O_WRONLY|O_BINARY); // system call
write(fd, d, sizeof(double)*n*20);
close(fd);
#包含//用于写入()
int fd=open(路径,O_WRONLY | O_BINARY);//系统调用
写入(fd、d、sizeof(双)*n*20);
关闭(fd);
#包括//对于open(),close())
#包含//用于写入()
int fd=open(路径,O_WRONLY | O_BINARY);//系统调用
写入(fd、d、sizeof(双)*n*20);
关闭(fd);
我要么用ANSI-C编写自己的文件写入例程,要么使用NSArray文件写入功能。取决于偏好 实际上,我写了一段代码,以确保这在iOS平台上能够正常工作。我将在这里发布洞的东西,你可以选择你想要的。基本上,我创建了一个5 x 5的静态数组用于测试,但它可以用于任何大小的数组。这提供了以二进制格式写入文件(保存原始双精度的最快方法和最佳方法)和读回文件的代码。如您所见,我在UIViewController的viewDidLoad例程中实现了主代码。我在数组中填充了随机数(并不是像我在rand例程中植入静态79那样) 在我定义的头文件中
double values[5][5];
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle * myBundle = [NSBundle mainBundle];
NSString * absPath = [myBundle resourcePath];
NSString * currPath;
srand(79);
FILE * aFile;
currPath=[absPath stringByAppendingString: @"/myfile.bin"];
if( NULL == ( aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"rb") ) ){
//Generate some random doubles
printf("generating 5 x 5 random values\r\n");
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
values[i][j] = (double)rand() / (double)rand();
printf("%.3f, ", values[i][j]);
}
printf("\r\n");
}
aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"wb");
fwrite (values , 1 , sizeof(double) * 5 * 5 , aFile );
fclose(aFile);
}else{
printf("File myfile.bin exists. Reading in File.\r\n");
fread(&values, sizeof(double), 5 * 5, aFile);
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
printf("%.3f, ", values[i][j]);
}
printf("\r\n");
}
fclose(aFile);
}
}
希望这能有所帮助。我要么用ANSI-C编写自己的文件写入例程,要么使用NSArray的文件写入功能。取决于偏好 实际上,我写了一段代码,以确保这在iOS平台上能够正常工作。我将在这里发布洞的东西,你可以选择你想要的。基本上,我创建了一个5 x 5的静态数组用于测试,但它可以用于任何大小的数组。这提供了以二进制格式写入文件(保存原始双精度的最快方法和最佳方法)和读回文件的代码。如您所见,我在UIViewController的viewDidLoad例程中实现了主代码。我在数组中填充了随机数(并不是像我在rand例程中植入静态79那样) 在我定义的头文件中
double values[5][5];
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle * myBundle = [NSBundle mainBundle];
NSString * absPath = [myBundle resourcePath];
NSString * currPath;
srand(79);
FILE * aFile;
currPath=[absPath stringByAppendingString: @"/myfile.bin"];
if( NULL == ( aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"rb") ) ){
//Generate some random doubles
printf("generating 5 x 5 random values\r\n");
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
values[i][j] = (double)rand() / (double)rand();
printf("%.3f, ", values[i][j]);
}
printf("\r\n");
}
aFile = fopen([currPath cStringUsingEncoding:[NSString defaultCStringEncoding]],"wb");
fwrite (values , 1 , sizeof(double) * 5 * 5 , aFile );
fclose(aFile);
}else{
printf("File myfile.bin exists. Reading in File.\r\n");
fread(&values, sizeof(double), 5 * 5, aFile);
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
printf("%.3f, ", values[i][j]);
}
printf("\r\n");
}
fclose(aFile);
}
}
希望这能有所帮助。尝试以下方法:
void writeData(double[][20] data, int n, const char name[]) {
FILE* output = fopen(name, "w");
int i, j;
fprintf(output, "%d\n", n);
for(i = 0; i < n; i++) {
for(j = 0; j < 20; j++) {
fprintf(output, "%lf\n", data[i][j]);
}
}
}
double** readData(const char name[]) {
FILE* input = fopen(name, "r");
int i, j, n;
fscanf(input, "%d", &n);
double** ret = (double**)malloc(n*sizeof(double*));
for(int i = 0; i < n; i++) {
ret[i] = (double*)malloc(20*sizeof(double));
for(int j = 0; j < 20; j++) {
fscanf(input, "%lf", &ret[i][j]);
}
}
return ret;
}
void writeData(双[][20]数据,整数n,常量字符名[]){
文件*output=fopen(名称,“w”);
int i,j;
fprintf(输出,“%d\n”,n);
对于(i=0;i
仅供参考,这是未经测试的代码,但它应该让您了解如何做到这一点。这只是一个文本文件,第一行包含长度(n),其余行包含双精度
您还可以使用plist,因为标记可以包含双精度标记。使用NSNumber*s写入嵌套在s中的双精度数
void writeData(double** data, int n, const char name[]) {
FILE* output = fopen(name, "w");
int i, j;
fprintf(output, "%d\n", n);
for(i = 0; i < n; i++) {
for(j = 0; j < LPCC_DIM; j++) {
fprintf(output, "%lf\n", data[i][j]);
}
}
}
double** readData(const char name[], int* getN) {
FILE* input = fopen(name, "r");
int n;
fscanf(input, "%d\n", &n);
*getN = n;
double** ret = (double**)malloc(n*sizeof(double*));
for(int i = 0; i < n; i++) {
ret[i] = (double*)malloc(LPCC_DIM*sizeof(double));
for(int j = 0; j < LPCC_DIM; j++) {
fscanf(input, "%lf", &ret[i][j]);
}
}
return ret;
}
void writeData(双**数据,整数n,常量字符名[]){
文件*output=fopen(名称,“w”);
int i,j;
fprintf(输出,“%d\n”,n);
对于(i=0;i
尝试以下方法:
void writeData(double[][20] data, int n, const char name[]) {
FILE* output = fopen(name, "w");
int i, j;
fprintf(output, "%d\n", n);
for(i = 0; i < n; i++) {
for(j = 0; j < 20; j++) {
fprintf(output, "%lf\n", data[i][j]);
}
}
}
double** readData(const char name[]) {
FILE* input = fopen(name, "r");
int i, j, n;
fscanf(input, "%d", &n);
double** ret = (double**)malloc(n*sizeof(double*));
for(int i = 0; i < n; i++) {
ret[i] = (double*)malloc(20*sizeof(double));
for(int j = 0; j < 20; j++) {
fscanf(input, "%lf", &ret[i][j]);
}
}
return ret;
}
void writeData(双[][20]数据,整数n,常量字符名[]){
文件*output=fopen(名称,“w”);
int i,j;
fprintf(输出,“%d\n”,n);
对于(i=0;i
仅供参考,这是未经测试的代码,但它应该让您了解如何做到这一点。这只是一个文本文件,第一行包含长度(n),其余行包含双精度
您还可以使用plist,因为标记可以包含双精度标记。使用NSNumber*s写入嵌套在s中的双精度数
void writeData(double** data, int n, const char name[]) {
FILE* output = fopen(name, "w");
int i, j;
fprintf(output, "%d\n", n);
for(i = 0; i < n; i++) {
for(j = 0; j < LPCC_DIM; j++) {
fprintf(output, "%lf\n", data[i][j]);
}
}
}
double** readData(const char name[], int* getN) {
FILE* input = fopen(name, "r");
int n;
fscanf(input, "%d\n", &n);
*getN = n;
double** ret = (double**)malloc(n*sizeof(double*));
for(int i = 0; i < n; i++) {
ret[i] = (double*)malloc(LPCC_DIM*sizeof(double));
for(int j = 0; j < LPCC_DIM; j++) {
fscanf(input, "%lf", &ret[i][j]);
}
}
return ret;
}
void writeData(双**数据,整数n,常量字符名[]){
文件*output=fopen(名称,“w”);
int i,j;
fprintf(输出,“%d\n”,n);
对于(i=0;i
我在文档中找不到这个write()。write()是一个标准的C函数,它使用文件描述符(int)而不是文件指针(file*)。它是一个较低级别的函数(我想它是一个系统调用?)而不是f*函数。O_WRONLY | O_BINARY用于二进制输出我在文档中找不到这个write()。write()是一个sta