写C数组包含二进制数据到文件

时间:2014-04-06 12:37:07

标签: c arrays file binary

C是我不知道的一种语言:)对我们大多数人来说,我的问题可能很愚蠢。 这个数组包含文件(style.css),只列出了它的一部分,问题是如何将其写入文件?使用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 };

提前致谢

3 个答案:

答案 0 :(得分:0)

您的数组表示标准的以null结尾的字符串。您可以使用标准C I / O库将其写入文件。

这是一个简单的例子:

FILE* f = fopen("output_file", "w");
if (f == NULL) {
    perror("fopen");
}
else {
    fprintf(f, "%s", data_style_css);
    fclose(f);
}

此链接将为您提供有关这些功能的完整详细信息(以及更多内容):http://en.wikipedia.org/wiki/C_file_input/output

答案 1 :(得分:0)

#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 will be created named mystyle.css (IMPORTANT!!!...if your computer already has a file of same name then it will be erased.)

    if(fp==NULL)//if file cannot be created fp will have NULL value.
    {
        printf("\nError..Cannot Create a mystyle.css");
        return 1;
    }

    for(i=0;data_style_css[i]!=0;i++)//will end when your array element will reach 0..(as i see your array is ended with 0,hence it will end)
    {
        ch=data_style_css[i];//reading each character from your array into variable ch

        fprintf(fp,"%c",data_style_css[i]);//writing each character from ch into file fp(mystyle.css)
    }
fclose(fp);
printf("\nFile Created Successfully..");
return 0;   
}

此程序仅在您的数组以0结尾时才有效(因此我假设您将确保每次都执行此操作)。将在您执行此程序的路径上创建名为MyStyle.css的文件。

数组的所有内容都将写入文件Mystyle.css,打开并检查输出。

我得到的输出是: -

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

我看到存储在数组中的.css文件没有换行符号。因此输出不干净。所有输出都在文本文件中以单行显示。您可以优化上述程序以获得干净的可读文本文件。(另外,您需要手动编辑.css文件来设置所有这些css新行上的标签)

您可以通过以下方式优化for循环: -

for(i=0;data_style_css[i]!=0;i++)//will end when your array element will reach 0..(as i see your array is ended with 0,hence it will end)
{
    ch=data_style_css[i];//reading each character from your array into variable ch

    if(ch=='{'||ch=='}')//when ch is '{' or '}' (As new block is 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 from ch into file fp(mystyle.css)

    if(ch==';')//when ch is ';' (As css tags end with semicolon the next tag should be on new line.Hence print newline on fp(Mystyle.css) )
        fprintf(fp,"%c",'\n');
}

通过以上优化,我在Mystyle.css上获得以下 输出(干净且可读) : -

100%);

border:solid 1px#247BE6;

颜色:#FFF;

字体大小:13像素;

高度:30像素;

行高:30像素;

文本对齐:中心;

宽度:0

}

答案 2 :(得分:0)

我尝试了一些C答案但没有成功,因此建议阅读一些C书是正确的。我设法用我理解的程序语言做我想要的 - LUA这里是脚本:

all=""   
for i, v in ipairs(data_fail_html)  do all=all..string.char(v) end
        print (all)

假设&#34; data_fail_html&#34;是数组包含二进制数据。

来自linux dropbear shell的

lua script.lua >> data_fail.html

谢谢大家的时间