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 };
提前致谢
最佳答案
#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%);边框:实心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
}
关于c - 将包含二进制数据的 C 数组写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894117/