Write C array contain binary data to file -


c 1 language not know :) , question may silly of you. array contain file (style.css), listed part of it, question how write file? using linux - slackware.

static const char data_style_css[] = { 0x20, 0x31, 0x30, 0x30, 0x25, 0x29, 0x3b, 0x62, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x3a, 0x73, 0x6f, 0x6c, 0x69, 0x64, 0x20, 0x31, 0x70, 0x78, 0x20, 0x23, 0x32, 0x34, 0x37, 0x42, 0x45, 0x36, 0x3b, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x23, 0x46, 0x46, 0x46, 0x3b, 0x66, 0x6f, 0x6e, 0x74, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x3a, 0x31, 0x33, 0x70, 0x78, 0x3b, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x33, 0x30, 0x70, 0x78, 0x3b, 0x6c, 0x69, 0x6e, 0x65, 0x2d, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x33, 0x30, 0x70, 0x78, 0x3b, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x61, 0x6c, 0x69, 0x67, 0x6e, 0x3a, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3b, 0x77, 0x69, 0x64, 0x74, 0x68, 0x3a, 0x30, 0x7d, 0 }; 

thanks in advance

#include<stdio.h> int main()  { static const char data_style_css[] = {  0x20, 0x31, 0x30, 0x30, 0x25, 0x29, 0x3b, 0x62, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x3a,    0x73,  0x6f, 0x6c, 0x69, 0x64, 0x20, 0x31, 0x70, 0x78, 0x20, 0x23, 0x32, 0x34, 0x37, 0x42, 0x45,  0x36, 0x3b, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x23, 0x46, 0x46, 0x46, 0x3b, 0x66, 0x6f,  0x6e, 0x74, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x3a, 0x31, 0x33, 0x70, 0x78, 0x3b, 0x68, 0x65,  0x69, 0x67, 0x68, 0x74, 0x3a, 0x33, 0x30, 0x70, 0x78, 0x3b, 0x6c, 0x69, 0x6e, 0x65, 0x2d,  0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x33, 0x30, 0x70, 0x78, 0x3b, 0x74, 0x65, 0x78,  0x74, 0x2d, 0x61, 0x6c, 0x69, 0x67, 0x6e, 0x3a, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3b,  0x77, 0x69, 0x64, 0x74, 0x68, 0x3a, 0x30, 0x7d, 0 };  char ch='\0'; int i; file *fp;      fp=fopen("mystyle.css","w");//a new file created named mystyle.css (important!!!...if computer has file of same name erased.)      if(fp==null)//if file cannot created fp have null value.     {         printf("\nerror..cannot create mystyle.css");         return 1;     }      for(i=0;data_style_css[i]!=0;i++)//will end when array element reach 0..(as see array ended 0,hence end)     {         ch=data_style_css[i];//reading each character array variable ch          fprintf(fp,"%c",data_style_css[i]);//writing each character ch file fp(mystyle.css)     } fclose(fp); printf("\nfile created successfully.."); return 0;    } 

this program work when array end 0(so assume make sure everytime). file named mystyle.css created on path execute program.

all contents of array written file mystyle.css, open , check see output.

output got is:-

100%);border:solid 1px #247be6;color:#fff;font-size:13px;height:30px;line-height:30px;text-align:center;width:0}

i see .css file stored in array not have new line characters. out put not clean.all output show in single line in text file.you can optimize above program clean readable text file.(other wise manually have edit .css file set css tags on new line)

you can optimize loop in way:-

for(i=0;data_style_css[i]!=0;i++)//will end when array element reach 0..(as see array ended 0,hence end) {     ch=data_style_css[i];//reading each character array variable ch      if(ch=='{'||ch=='}')//when ch '{' or '}' (as new block started/ended on css print new line in file fp(mystyle.cc)         fprintf(fp,"%c",'\n');      fprintf(fp,"%c",data_style_css[i]);//writing each character ch file fp(mystyle.css)      if(ch==';')//when ch ';' (as css tags end semicolon next tag should on new line.hence print newline on fp(mystyle.css) )         fprintf(fp,"%c",'\n'); } 

with above optimization following output(clean , readable) on mystyle.css:-

100%);

border:solid 1px #247be6;

color:#fff;

font-size:13px;

height:30px;

line-height:30px;

text-align:center;

width:0

}


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -