简体   繁体   English

以多平台方式将4个数组写入文件的最简单方法是什么?

[英]What is the simplest way to write 4 arrays into a file in a multiplatform way?

Say we have a struct of an int array, a float array etc. and we need to write them into a binary format file for fast reloading. 假设我们有一个int数组,float数组等结构,我们需要将它们写入二进制格式文件以进行快速重载。 Can this be done in a simple way? 可以用简单的方法完成吗?

Should the files be several for each of the array? 每个数组的文件应该是几个吗?

Write in plain text ... and then zip it 用纯文本编写...然后将其压缩

Presto! 快点! binary format 二进制格式

Unless you have a huge amount of data, just write a standard text format. 除非您有大量数据,否则只需编写标准文本格式即可。 If you can assume c99 on both ends, use the %a formatter for floating-point data to insulate yourselves from the vagaries of binary-decimal conversion. 如果可以假设两端都使用c99,请对浮点数据使用%a格式化程序,以使自己与二进制十进制转换的变数隔离。

If the amount of data is huge, or you need to use a "raw data" format for other reasons, you will want to convert your data into a known endianness before writing, and convert back to the host endianness after reading. 如果数据量巨大,或者出于其他原因需要使用“原始数据”格式,则需要在写入之前将数据转换为已知的字节序,并在读取后转换回主机的字节序。 Any reasonably sane OS has library routines for doing these conversions. 任何合理的操作系统都具有用于执行这些转换的库例程。

I'm a bit rusty in C, but it's likely someone has written support for serializing arrays into binary data specifically for writing to and/or reading from file. 我对C有点生疏,但可能有人写过将阵列序列化为二进制数据的支持,专门用于写入和/或读取文件。 Google serialization for your specific language and it's likely someone has already solved this issue for you. Google针对您的特定语言进行了序列化,可能有人已经为您解决了此问题。

man fwrite/fread . man fwrite / fread

However, Endianness might be a problem, if you use some weird platform. 但是,如果您使用一些奇怪的平台, 字节序可能是个问题。 Also you should probably use types that have fixed size on all platforms. 另外,您可能应该在所有平台上使用固定大小的类型。

If you are just looking for an example, the following is an extremely simple one with absolutely no error checking. 如果您只是在寻找示例,则以下是一个非常简单的示例,其中完全没有错误检查。 It writes the contents of a structure with some integers in it to a file and then reads them back out again. 它将具有一些整数的结构的内容写入文件,然后再次读出它们。 The points made by others about byte order, though, are very germane and would need to be addressed if the file is to be used across different platforms. 但是,其他人关于字节顺序的观点非常紧密,如果要在不同平台上使用该文件,则需要解决。

typedef struct {
   int count;
   int *values;
} SomeInts;


int main(int argc, char* argv[])
{
   SomeInts ints;
   int i;
   FILE *fh;

   ints.count = 5;
   ints.values = (int*)malloc( ints.count * sizeof(int));
   for ( i = 0; i < ints.count; i++ )
      ints.values[i] = i * 42;

   // write it
   fh = fopen( argv[1], "wb" );
   // really should check amount written to verify it worked
   fwrite( &ints.count, sizeof( ints.count ), 1, fh );
   fwrite( ints.values, sizeof(ints.values[0]), ints.count, fh );
   fclose(fh);

   // read them back in.
   free( ints.values );
   memset( &ints, 0, sizeof( ints ));


   fh = fopen( argv[1], "rb" );
   // read how many ints (should also check for errors)
   fread( &ints.count, sizeof( ints.count ), 1, fh );
   ints.values = (int*)malloc( ints.count * sizeof(int));
   fread( ints.values, sizeof(ints.values[0]), ints.count, fh );
   fclose(fh);

   for ( i = 0; i < ints.count; i++ )
      printf( "%d\n", ints.values[i] );

   free( ints.values );

}

If you use pragma pack(1), you can do the writes/reads in one hit on a chunk of memory. 如果您使用pragma pack(1),则可以在一块内存中进行一次写入/读取操作。

#include <stdio.h>  
#include <memory.h>  
typedef struct ca_tag{  
int i[4];  
float f[3];  
}ca_type;  

#pragma pack(1)  
void init(ca_type* c)  // fill it with something
{  
   c->i[0] = 1; c->i[1] = 2; c->i[2] = 3; c->i[3] = 12;  
   c->f[0] = 2.3; c->f[1] = 32.3; c->f[2] = 42.3;  
}  
int main()  
{  
   FILE *stream;  
   ca_type ca;  
   char *ptr = (char*)&ca;  
   char *ptr2 = (char*)&ca;  
   init(&ca);  
   if( (stream = fopen( "test.out", "wb" )) != NULL )  
      fwrite( ptr, sizeof( ca ), 1, stream );  
   else  
      printf( "Problem opening for write\n" );  
   fclose(stream);  
   memset((void *)&ca, 0, sizeof(ca));// zero the lot  
   if( (stream = fopen( "test.out", "rb" )) != NULL )  
     fread( (void*)ptr2, sizeof( ca ), 1, stream );  
   else  
      printf( "Problem opening for read\n" );  
   return 0;  
}  

Error checking needs doing as before 错误检查需要像以前一样

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

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