Commit 24112e05 authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Can write a record from C, only name is currently supported

parent ef618bee
<?xml version='1.0' encoding='UTF-8'?>
<Project Type="Project" LVVersion="18008000">
<Property Name="CCSymbols" Type="Str"></Property>
<Property Name="NI.LV.All.SourceOnly" Type="Bool">true</Property>
<Property Name="NI.LV.All.SourceOnly" Type="Bool">false</Property>
<Property Name="NI.Project.Description" Type="Str"></Property>
<Property Name="SMProvider.SMVersion" Type="Int">201310</Property>
<Item Name="My Computer" Type="My Computer">
......@@ -25,6 +25,7 @@
<Property Name="specify.custom.address" Type="Bool">false</Property>
<Item Name="TestStorage.vi" Type="VI" URL="../TestStorage.vi"/>
<Item Name="BinStorage.lvclass" Type="LVClass" URL="../BinStorage.lvclass"/>
<Item Name="TestOneName.vi" Type="VI" URL="../TestOneName.vi"/>
<Item Name="Dependencies" Type="Dependencies"/>
<Item Name="Build Specifications" Type="Build"/>
</Item>
......
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <byteswap.h>
typedef struct storage {
FILE* file;
unsigned long* contents;
unsigned int con_idx;
size_t buf_size;
} BinaryStorage;
BinaryStorage bs_open(char* filename){
BinaryStorage storage;
storage.file = fopen(filename,"wb");
storage.buf_size = 500;
storage.con_idx = 0;
storage.contents = (long*) malloc(sizeof(unsigned long)*storage.buf_size);
char header[]="SNS-BIET2020";
fwrite(header,sizeof(header)-1,1,storage.file);
storage.contents[storage.con_idx]=ftell(storage.file);
storage.con_idx++;
return storage;
}
int write_record(BinaryStorage *bs, char* name){
unsigned char name_len = strlen(name);
unsigned int total_length = bswap_32(1+1+name_len+1);
fwrite(&total_length,4,1,bs->file);
fputc(1,bs->file);
fwrite(&name_len,1,1,bs->file);
fwrite(name, 1,name_len,bs->file);
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
}
void bs_close(BinaryStorage *bs){
unsigned int total_length = bswap_32(1+1);
fwrite(&total_length,4,1,bs->file);
fputc(255,bs->file);
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
int i;
printf("total = %d\n",bs->con_idx);
for(i=0;i<bs->con_idx;i++){
unsigned long swapped = bswap_64(bs->contents[i]);
printf("%d\n",bs->contents[i]);
fwrite(&swapped,8,1,bs->file);
}
unsigned int num_of_rec = bswap_32(bs->con_idx);
fwrite(&num_of_rec,4,1,bs->file);
fclose(bs->file);
free(bs->contents);
}
int main(){
printf("Hello World!\n");
BinaryStorage storage = bs_open("test_c.bin");
write_record(&storage,"Hello World!");
bs_close(&storage);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment