使用的是Unicode码编译后出现 串联不匹配的字符串

C++语言 码拜 6年前 (2015-07-10) 1110次浏览 0个评论
 

使用的是Unicode码,程序段如下:
void CPicBrowserView::SetFile(LPCWSTR lpFileName)
{
CString tmp;
FILE * fp=NULL;//文件指针初始化为空
CString m_TempFile;//用于保存文件路径字符串
if(lpFileName!=NULL)//如果路径为空,不设置
{
tmp.Format(_T(“<html>”\
“<head>”\
“<TITLE>图片浏览器</TITLE>”\
“</head>”\
“<body>”\
“<DIV id=”image” class=”slides”><IMG src=”%s”></DIV>”\
“</body>”\
“</html>”),lpFileName);
m_TempFile=GetExeFilePath();
m_TempFile+=”\temp.html”;
//打开文件,如果文件存在,则删除,否则就创建
fp=fopen(“m_TempFile”,”w+”);
//写入文件
int nSize=fprintf(fp,”%s”,tmp);
//关闭文件
fclose(fp);
}
}
编译后出现问题:
>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“<head>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“<TITLE>图片浏览器</TITLE>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“</head>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“<body>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“<DIV id=”image” class=”slides”><IMG src=”%s”></DIV>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“</body>”
1>d:\我的文档\picbrowser\picbrowser\picbrowserview.cpp(123): error C2308: 串联不匹配的字符串
1> 连接 宽“<html>”与 窄“</html>”

 
程序崩溃时,中止程序,查看调用堆栈,定位到出错的代码处
 
是不是_T(“”) 和 每行的 结尾的“\” 出的问题
你先试着 把 _T中de内容写到一行(会很长),看能通过不能
多试试吧
 
引用 1 楼 zhangxiangDavaid 的回复:

程序崩溃时,中止程序,查看调用堆栈,定位到出错的代码处

没有生成可自行文件

 
引用 2 楼 king191923157 的回复:

是不是_T(“”) 和 每行的 结尾的“\” 出的问题
你先试着 把 _T中de内容写到一行(会很长),看能通过不能
多试试吧

出现错误:error C2017: 非法的转义序列
而且之前的错误也没消失

 
引用 4 楼 STRAVELER 的回复:
Quote: 引用 2 楼 king191923157 的回复:

是不是_T(“”) 和 每行的 结尾的“\” 出的问题
你先试着 把 _T中de内容写到一行(会很长),看能通过不能
多试试吧

出现错误:error C2017: 非法的转义序列
而且之前的错误也没消失

你试着写到一行的时候不会没有删除每一行结尾的 “\ 吧

10分

tmp.Format(_T(“<html><head><TITLE>图片浏览器</TITLE></head><body><DIV id=”image” class=”slides”><IMG src=”%s”></DIV></body></html>”),lpFileName);
 
偶遇到类似问题都是用
“每次用/*…*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
 
引用 5 楼 king191923157 的回复:
Quote: 引用 4 楼 STRAVELER 的回复:
Quote: 引用 2 楼 king191923157 的回复:

是不是_T(“”) 和 每行的 结尾的“\” 出的问题
你先试着 把 _T中de内容写到一行(会很长),看能通过不能
多试试吧

出现错误:error C2017: 非法的转义序列
而且之前的错误也没消失

你试着写到一行的时候不会没有删除每一行结尾的 “\ 吧

第一次用写字符串生成HTML文件的程序,“/”删除后非法的转义序列没了,但是还是之前的错误。。

 
引用 7 楼 zhao4zhong1 的回复:

偶遇到类似问题都是用
“每次用/*…*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。

因为对HTML格式不太了解,这个注释您能说的具体点吗

 
不会吧,你这个放到一行应该没问题的,看错误 明显 是_T()指对第一行产生效果,其他行没产生效果造成的

你试这个了么?

tmp.Format(_T(“<html><head><TITLE>图片浏览器</TITLE></head><body><DIV id=”image” class=”slides”><IMG src=”%s”></DIV></body></html>”),lpFileName);

另外这一行
fp=fopen(“m_TempFile”,”w+”); 是不是写错了 改写成fp=fopen(m_TempFile,”w+”);

另附C2308 说明
https://msdn.microsoft.com/zh-cn/library/86888ehe%28v=VS.100%29.aspx

 
你试试把你的字符串每行都用_T(“”)包括起来,_T是一个宏只能对一行有效果
 
引用 11 楼 king191923157 的回复:

你试试把你的字符串每行都用_T(“”)包括起来,_T是一个宏只能对一行有效果

感谢您的热心回答:
1.”m_TempFile”表示路径没错;
2.“”全包住我之前试过,不对。。
3.确实是放到一行了,错误不变。。

20分

tmp.Format(
    _T("<html>")
    _T("<head>")
    _T("<TITLE>图片浏览器</TITLE>")
    _T("</head>")
    _T("<body>")
    _T("<DIV id="image" class="slides"><IMG src="%s"></DIV>")
    _T("</body>")
    _T("</html>")
    ,lpFileName);

C++ String Literals
A string literal consists of zero or more characters from the source character set surrounded by double quotation marks (“). A string literal represents a sequence of characters that, taken together, form a null-terminated string.

Syntax

string-literal :

“s-char-sequenceopt”
L”s-char-sequenceopt”

s-char-sequence :

s-char
s-char-sequence s-char

s-char :

any member of the source character set except the double quotation mark (“), backslash (\), or newline character
escape-sequence

C++ strings have these types:

Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating “”\0″” that marks the end of the string

Array of wchar_t, for wide-character strings
The result of modifying a string constant is undefined. For example:

char *szStr = “1234”;
szStr[2] = “”A””; // Results undefined

Microsoft Specific

In some cases, identical string literals can be “pooled” to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. The/Gf compiler option enables string pooling.

END Microsoft Specific

When specifying string literals, adjacent strings are concatenated. Therefore, this declaration:

char szStr[] = “12” “34”;

is identical to this declaration:

char szStr[] = “1234”;

This concatenation of adjacent strings makes it easy to specify long strings across multiple lines:

cout << “Four score and seven years ”
“ago, our forefathers brought forth ”
“upon this continent a new nation.”;

In the preceding example, the entire string Four score and seven years ago, our forefathers brought forth upon this continent a new nation. is spliced together. This string can also be specified using line splicing as follows:

cout << “Four score and seven years \
ago, our forefathers brought forth \
upon this continent a new nation.”;

After all adjacent strings in the constant have been concatenated, the NULL character, “”\0″”, is appended to provide an end-of-string marker for C string-handling functions.

When the first string contains an escape character, string concatenation can yield surprising results. Consider the following two declarations:

char szStr1[] = “\01” “23”;
char szStr2[] = “\0123”;

Although it is natural to assume that szStr1 and szStr2 contain the same values, the values they actually contain are shown in Figure 1.1.

Figure 1.1 Escapes and String Concatenation

Microsoft Specific

The maximum length of a string literal is approximately 2,048 bytes. This limit applies to strings of type char[] and wchar_t[]. If a string literal consists of parts enclosed in double quotation marks, the preprocessor concatenates the parts into a single string, and for each line concatenated, it adds an extra byte to the total number of bytes.

For example, suppose a string consists of 40 lines with 50 characters per line (2,000 characters), and one line with 7 characters, and each line is surrounded by double quotation marks. This adds up to 2,007 bytes plus one byte for the terminating null character, for a total of 2,008 bytes. On concatenation, an extra character is added to the total number of bytes for each of the first 40 lines. This makes a total of 2,048 bytes. (The extra characters are not actually written to the string.) Note, however, that if line continuations (\) are used instead of double quotation marks, the preprocessor does not add an extra character for each line.

END Microsoft Specific

Determine the size of string objects by counting the number of characters and adding 1 for the terminating “”\0″” or 2 for type wchar_t.

Because the double quotation mark (“) encloses strings, use the escape sequence (“) to represent enclosed double quotation marks. The single quotation mark (“”) can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a string, you must type two backslashes (\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)

To specify a string of type wide-character (wchar_t[]), precede the opening double quotation mark with the character L. For example:

wchar_t wszStr[] = L”1a1g”;

All normal escape codes listed in Character Constants are valid in string constants. For example:

cout << “First line\nSecond line”;
cout << “Error! Take corrective action\a”;

Because the escape code terminates at the first character that is not a hexadecimal digit, specification of string constants with embedded hexadecimal escape codes can cause unexpected results. The following example is intended to create a string literal containing ASCII 5, followed by the characters five:

\x05five”

The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters ive. The following example produces the desired results:

“\005five” // Use octal constant.
“\x05” “five” // Use string splicing.

 
赵老师正解,太感谢了
 
_T貌似只是在字符串前加L,你这样写只有第一个字符串是wchar——t

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明使用的是Unicode码编译后出现 串联不匹配的字符串
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!