【问题标题】:Write C array contain binary data to file将包含二进制数据的 C 数组写入文件
【发布时间】:2014-05-18 14:46:45
【问题描述】:

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

提前致谢

【问题讨论】:

  • 读一些关于 C 的书怎么样,尤其是关于输入和输出的一章?
  • fprintf(fp, "%s", data_style_css);

标签: c arrays file binary


【解决方案1】:
#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%);

边框:实心 1px #247BE6;

颜色:#FFF;

字体大小:13px;

高度:30px;

行高:30px;

文本对齐:居中;

宽度:0

}

【讨论】:

  • 非常感谢您的快速回答,我放弃了 C ,设法用我理解的语言来做这件事 - 只要网站允许我回答我自己的问题,LUA 就会发布脚本。输出文件确实没有 CR&LF 字符。这对浏览器来说不是问题。
  • 不客气...:) 并且不要放弃,顺便说一句,你是如何将 .css 文件放入数组的。?????
【解决方案2】:

您的数组代表一个标准的以空值结尾的字符串。您可以使用标准 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

【讨论】:

    【解决方案3】:

    我尝试了一些 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
    

    感谢大家的宝贵时间

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多