2014-02-23 30 views
-2

嘿家伙,所以我有这个特殊字符的文本,我想“逃离”特殊字符能够编译我的程序,但我不知道是/: - =特殊字符?我还需要逃避它们吗?下面是例子c中有什么特别的字符?

static const char *postthis="text and spec chars"; 

,这里是示例文本的,我希望把

<text:Text 
text:text="http://text.text.text/text/text/" 
text:text="text:text:text-text-text"> 
<text:Text> 
<text text="http://text.com/text"> 
<productID>20630175</textID> 
</text> 
</text:text> 
</text:text> 

所以我把\ <前“,但再次我有错误,我需要什么逃脱,怎么样?

static const char *postthis="\<text:Text 
text:text=\"http://text.text.text/text/text/\" 
text:text=\"text:text:text-text-text\"\> 
\<text:Text\> 
\<text text="http://text.com/text\"\> 
\<textID\>20630175\</textID\> 
\</text\> 
\</text:text\> 
\</text:text\>"; 
+0

C“simple-escape-sequence”:\'\“\?\\ \ a \ b \ f \ n \ r \ t \ v – chux

回答

0

我觉得行香草Ç东西您需要在多行初始化程序 和引号中跳过该行的结尾。如果有标签或换行符,您需要将这些标签或标签越过\ t \ n。

static const char *postthis="\<text:Text\ 
text:text=\"http://text.text.text/text/text/\"\n\ 
text:text=\"text:text:text-text-text\">\n\ 
<text:Text>\n\ 
<text text="http://text.com/text\"\>\n\ 
<textID>20630175</textID>\n\ 
</text>\n\ 
</text:text>\n\ 
</text:text>"; 
0

您的时间更好花费将文本中的文件和读取。手工编辑XML是保证事与愿违。

如果你是一个工作在iOS项目你可以使用NSString的初始化器:

- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error 

或者,如果您使用的是沿

#include <stdio.h> 

#define BUFF_SIZE 1024 

int main(int argc, char **argv) { 

    char s[BUFF_SIZE]; 
    FILE *fp = fopen("file.xml", "r"); 
    int i = 0; 

    while (!feof(fp) && i < BUFF_SIZE-1) { 
     s[i++] = fgetc(fp); 
    } 
    s[i]='\0'; 

    printf("My string is here %s\n", s); 

} 
0

你需要一个C字符串内逃脱的字符只有\"。没有理由引用<。换行符可以被转义,或者你可以将它们包括为\ n(我更喜欢它,因为它可以让你缩进更好的IMO)。然而,你不能简单地嵌入无引号的换行符(这可能是你的主要问题)。

XML同时支持'"作为引用,因此您几乎总是可以通过使用'来简化您的生活。

static const char *postthis= 
"<text:Text\n" 
" text:text='http://text.text.text/text/text/'\n" 
" text:text='text:text:text-text-text'>\n" 
" <text:Text>\n" 
" <text text='http://text.com/text'>\n" 
"  <productID>20630175</textID>\n" 
" </text>\n" 
" </text:text>\n" 
"</text:text>"; 

当然XML不需要换行,所以你可以只删除它们,除非你想这是人类阅读。

请注意,以上使用多部分C字符串。 (多个“......”连接在一起)。我不记得C的哪个版本(或者它是GNU扩展)。如果你不能使用它,那么你可以逃脱换行符:

static const char *postthis= 
"<text:Text\ 
    text:text='http://text.text.text/text/text/'\ 
... 
</text:text>"; 

但它使压痕有点困难,而不使您的代码定制看起来更糟。

0

查看http://en.cppreference.com/w/cpp/language/escape对于字符串文字中的所有转义序列。如果您希望它们保持不变,这些将需要转义。您需要的字符串中的任何新行都需要写为\ n。

如前所述,您可以将文本保留在文件中并在运行时读取它。

如果你有很多要编译到你的程序的文本,另一个简单的办法是使用objcopy命令:

objcopy -I binary -O elf64-x86-64 -B i386 --rename-section .data=.rodata,alloc,load,readonly,data,contents your_text.txt your_text.o 

这将使你在它下面的符号的对象文件:

_binary_your_text_txt_start 
_binary_your_text_txt_end 
_binary_your_text_txt_size 

链接到your_text.o并使用符号访问文本。例如:

#include <stdio.h> 

extern char _binary_your_text_txt_start; 
extern char _binary_your_text_txt_end; 
extern char _binary_your_text_txt_size; 

int main(int argc, char *argv[]) 
{ 
    const char * b = &_binary_your_text_txt_start; 
    const char * e = &_binary_your_text_txt_end; 
    size_t s = (size_t)&_binary_your_text_txt_size; 

    fwrite(b, s, 1, stdout); 

    return 0; 
} 
相关问题