g++ 编译器不能解决的有关问题 。(通用性有关问题)
g++ 编译器不能解决的问题 。。。(通用性问题)
有个问题:
问题源自:http://blog.****.net/haoel/archive/2006/11/19/1395358.aspx
int main()
{
char* p1 = "anything ";
char* p2 = "anything ";
printf(“ p1=%x, p2=%x \n”,p1,p2);
return 0;
}
时,出错:
[root@localhost Program]# gcc -g test_const_ptr.c -o t4
test_const_ptr.c: 在函数 ‘main’ 中:
test_const_ptr.c:8: 错误:程序中有游离的 ‘\342’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\200’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\234’
test_const_ptr.c:8: 错误:expected expression before ‘%’ token
test_const_ptr.c:8: 错误:程序中有游离的 ‘\’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\342’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\200’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\235’
这是编译器的错误?用vc能编译,不过gcc好像更符合标准啊,怎么会出错?
如果用gcc出错而有的编译器能通过,遇到这种情况程序怎么解决呢?
以前也遇到过类似的问题:
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main()
{
ifstream out( "t.txt ");
string line;
int i = 0;
while(1)
{
if(out.eof())
break;
out > > line ;
cout < < line < < " ";
}//while(!out.eof());
return 0;
}
t.txt 内容为Hello,world! I hope I can succeed!
输出却为Hello,world! I hope I can succeed! succeed!(succeed重复了两次!)
在dev_c++却输出正常。。。
这问题可以怎么解决呢?
谢谢!
------解决方案--------------------
printf(“ p1=%x, p2=%x \n”,p1,p2);输出指针用%p吧;
unix文件结尾和dos不同的,dos下是ctrl+z,unix是ctrl+d吧,注意细节就可以了吧
------解决方案--------------------
out.read() 判断结束
------解决方案--------------------
printf(“ p1=%x, p2=%x \n”,p1,p2);
这一句中的引号是中文(全角)!
------解决方案--------------------
我觉得可能是因为你的t.txt文件结尾(succeed!后面)有空格(0x20)或回车换行(0x0d, 0x0a).
在读了 "succeed! "后,out.eof()返回false,因为后面还有内容(空格/换行符),于是执行out > > line.但out中已无字符内容,此句执行失败,line还保留上次内容: "succeed! ",结果造成 "succeed! "输出了两次.
预防这种情况的一个方法是在 "out > > line; "前加一行:
line = " ";
------解决方案--------------------
何必搞那么复杂的逻辑,还容易出错。
while(out > > line )
{
cout < < line < < " ";
有个问题:
问题源自:http://blog.****.net/haoel/archive/2006/11/19/1395358.aspx
int main()
{
char* p1 = "anything ";
char* p2 = "anything ";
printf(“ p1=%x, p2=%x \n”,p1,p2);
return 0;
}
时,出错:
[root@localhost Program]# gcc -g test_const_ptr.c -o t4
test_const_ptr.c: 在函数 ‘main’ 中:
test_const_ptr.c:8: 错误:程序中有游离的 ‘\342’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\200’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\234’
test_const_ptr.c:8: 错误:expected expression before ‘%’ token
test_const_ptr.c:8: 错误:程序中有游离的 ‘\’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\342’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\200’
test_const_ptr.c:8: 错误:程序中有游离的 ‘\235’
这是编译器的错误?用vc能编译,不过gcc好像更符合标准啊,怎么会出错?
如果用gcc出错而有的编译器能通过,遇到这种情况程序怎么解决呢?
以前也遇到过类似的问题:
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main()
{
ifstream out( "t.txt ");
string line;
int i = 0;
while(1)
{
if(out.eof())
break;
out > > line ;
cout < < line < < " ";
}//while(!out.eof());
return 0;
}
t.txt 内容为Hello,world! I hope I can succeed!
输出却为Hello,world! I hope I can succeed! succeed!(succeed重复了两次!)
在dev_c++却输出正常。。。
这问题可以怎么解决呢?
谢谢!
------解决方案--------------------
printf(“ p1=%x, p2=%x \n”,p1,p2);输出指针用%p吧;
unix文件结尾和dos不同的,dos下是ctrl+z,unix是ctrl+d吧,注意细节就可以了吧
------解决方案--------------------
out.read() 判断结束
------解决方案--------------------
printf(“ p1=%x, p2=%x \n”,p1,p2);
这一句中的引号是中文(全角)!
------解决方案--------------------
我觉得可能是因为你的t.txt文件结尾(succeed!后面)有空格(0x20)或回车换行(0x0d, 0x0a).
在读了 "succeed! "后,out.eof()返回false,因为后面还有内容(空格/换行符),于是执行out > > line.但out中已无字符内容,此句执行失败,line还保留上次内容: "succeed! ",结果造成 "succeed! "输出了两次.
预防这种情况的一个方法是在 "out > > line; "前加一行:
line = " ";
------解决方案--------------------
何必搞那么复杂的逻辑,还容易出错。
while(out > > line )
{
cout < < line < < " ";