将包含二进制数据的 C 数组写入文件



C 是一种我不知道的语言:)我的问题对你们大多数人来说可能是愚蠢的。这个数组包含文件(style.css),只列出了它的一部分,问题是如何将其写入文件?使用 linux - 松弛软件。

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 };

提前致谢

#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='';
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%);边框:实心 1px #247BE6;颜色:#FFF;字体大小:13px;高度:30px;行高:30px;文本对齐:居中;宽度: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%);

边框:实心1px #247BE6;

颜色:#FFF;

字体大小:13px;

高度:30px;

行高:30px;

文本对齐:居中;

宽度: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

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

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

假设"data_fail_html"是包含二进制数据的数组。

来自Linux Dropbear Shell:

lua script.lua >> data_fail.html

谢谢大家抽出宝贵时间

相关内容

  • 没有找到相关文章