qt读取文本的一个问题

sinat_33495068 2016-09-13 10:38:48
void myOpenGL::SetupWorld()
{
QFile file(":/new/prefix1/Data/World.txt");
if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
QMessageBox::warning(this,tr("warning"),tr("can't open this text"));
}
QTextStream in(&file);
int ipoint;
ipoint =0;
QString str;
QStringList strList;
int numtriangles;
while(!in.atEnd())
{
str=in.readLine();
strList.append(str);
}
QString str1=strList.at(ipoint++);
while(str1 == "")
str1=strList.at(ipoint++);
//读取面数
//int numtriangles;
sscanf(str1.toLocal8Bit().constData(),"NUMPOLLIES %d\n",&numtriangles);
float x,y,z,u,v;
sector1.numtriangles = numtriangles;
//读取文件中点数据,纹理数据
for (int loop = 0; loop < numtriangles; loop++)
{
for (int vert = 0; vert < 3; vert++)
{
//读取一行
str1 = strList.at(ipoint++);
while(str1 == "")
str1=strList.at(ipoint++);

sscanf(str1.toLocal8Bit().constData(), "%f %f %f %f %f", &x, &y, &z, &u, &v);
sector1.triangle[loop].vertex[vert].x = x;
sector1.triangle[loop].vertex[vert].y = y;
sector1.triangle[loop].vertex[vert].z = z;
sector1.triangle[loop].vertex[vert].u = u;
sector1.triangle[loop].vertex[vert].v = v;
}
}
}这段代码不知道哪里出现问题了,当程序运行到下面for循环时,开始没有问题,但是当运行到ipoint=12时就会出现错误,调试信息显示损坏内部程序状态,执行程序中发生生了缓冲区溢出,但是我检查了很多遍没有发现哪里有问题,还希望大神看到可以帮助下。
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_33495068 2016-09-14
  • 打赏
  • 举报
回复
引用 7 楼 zbw1185 的回复:
引用 3 楼 sinat_33495068 的回复:
[quote=引用 1 楼 zbw1185 的回复:] 字符串列表下标是从0开始的,你在第一个QString str1=strList.at(ipoint++);就已经吧str1初始化为第二行了。
QString str1=strList.at(ipoint++);str1此时还是指的是“0”这一行吧,ipoint的值在这个语句结束后才为1的,然后下面的语句就是对文件中的内容一行一行读啊,
你是对的,抱歉做了一个错误的回复。[/quote]哈哈没事没事,谁能不犯点错误啊。
画茧自缚 2016-09-14
  • 打赏
  • 举报
回复
引用 3 楼 sinat_33495068 的回复:
引用 1 楼 zbw1185 的回复:
字符串列表下标是从0开始的,你在第一个QString str1=strList.at(ipoint++);就已经吧str1初始化为第二行了。
QString str1=strList.at(ipoint++);str1此时还是指的是“0”这一行吧,ipoint的值在这个语句结束后才为1的,然后下面的语句就是对文件中的内容一行一行读啊,
你是对的,抱歉做了一个错误的回复。
unixpro 2016-09-13
  • 打赏
  • 举报
回复
跟楼主说两点: 1)你得把World.txt格式及内容贴出来,大家才能帮你尽快找出问题 2)代码就要粘贴成代码,不能纯文本,看起来太累,我给你贴到这里了

void myOpenGL::SetupWorld()
{
    QFile file(":/new/prefix1/Data/World.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QMessageBox::warning(this,tr("warning"),tr("can't open this text"));
    }
    QTextStream in(&file);
    int ipoint;
    ipoint =0;
    QString str;
    QStringList strList;
    int numtriangles;
    while(!in.atEnd())
    {
        str=in.readLine();
        strList.append(str);
    }
    QString str1=strList.at(ipoint++);
    while(str1 == "")
        str1=strList.at(ipoint++);
    //读取面数
    //int numtriangles;
    sscanf(str1.toLocal8Bit().constData(),"NUMPOLLIES %d\n",&numtriangles);
    float x,y,z,u,v;
    sector1.numtriangles = numtriangles;
    //读取文件中点数据,纹理数据
    for (int loop = 0; loop < numtriangles; loop++)
    {
        for (int vert = 0; vert < 3; vert++)
        {
            //读取一行
            str1 = strList.at(ipoint++);
            while(str1 == "")
                str1=strList.at(ipoint++);
            
            sscanf(str1.toLocal8Bit().constData(), "%f %f %f %f %f", &x, &y, &z, &u, &v);
            sector1.triangle[loop].vertex[vert].x = x;
            sector1.triangle[loop].vertex[vert].y = y;
            sector1.triangle[loop].vertex[vert].z = z;
            sector1.triangle[loop].vertex[vert].u = u;
            sector1.triangle[loop].vertex[vert].v = v;
        }
    }
}
sinat_33495068 2016-09-13
  • 打赏
  • 举报
回复
引用 1 楼 zbw1185 的回复:
字符串列表下标是从0开始的,你在第一个QString str1=strList.at(ipoint++);就已经吧str1初始化为第二行了。
QString str1=strList.at(ipoint++);str1此时还是指的是“0”这一行吧,ipoint的值在这个语句结束后才为1的,然后下面的语句就是对文件中的内容一行一行读啊,
unixpro 2016-09-13
  • 打赏
  • 举报
回复
引用 1 楼 zbw1185 的回复:
字符串列表下标是从0开始的,你在第一个QString str1=strList.at(ipoint++);就已经吧str1初始化为第二行了。
这个正确的吧,ipoint++就是 0 呀
画茧自缚 2016-09-13
  • 打赏
  • 举报
回复
字符串列表下标是从0开始的,你在第一个QString str1=strList.at(ipoint++);就已经吧str1初始化为第二行了。
sinat_33495068 2016-09-13
  • 打赏
  • 举报
回复
今天问了老师,老师在24行下加了一行代码sector1.triangle = new TRIANGLE[numtriangles];现在能出来图形了。多谢大家了。
sinat_33495068 2016-09-13
  • 打赏
  • 举报
回复
嗯嗯嗯,多谢多谢。下次注意。我把这个文本贴上大家可以参考下。
NUMPOLLIES 36
-3.0  0.0 -3.0 0.0 6.0
-3.0  0.0  3.0 0.0 0.0
 3.0  0.0  3.0 6.0 0.0
-3.0  0.0 -3.0 0.0 6.0
 3.0  0.0 -3.0 6.0 6.0
 3.0  0.0  3.0 6.0 0.0
-3.0  1.0 -3.0 0.0 6.0
-3.0  1.0  3.0 0.0 0.0
 3.0  1.0  3.0 6.0 0.0
-3.0  1.0 -3.0 0.0 6.0
 3.0  1.0 -3.0 6.0 6.0
 3.0  1.0  3.0 6.0 0.0
-2.0  1.0  -2.0 0.0 1.0
-2.0  0.0  -2.0 0.0 0.0
-0.5  0.0  -2.0 1.5 0.0
-2.0  1.0  -2.0 0.0 1.0
-0.5  1.0  -2.0 1.5 1.0
-0.5  0.0  -2.0 1.5 0.0
 2.0  1.0  -2.0 2.0 1.0
 2.0  0.0  -2.0 2.0 0.0
 0.5  0.0  -2.0 0.5 0.0
 2.0  1.0  -2.0 2.0 1.0
 0.5  1.0  -2.0 0.5 1.0
 0.5  0.0  -2.0 0.5 0.0
-2.0  1.0  2.0 2.0  1.0
-2.0  0.0   2.0 2.0 0.0
-0.5  0.0   2.0 0.5 0.0
-2.0  1.0  2.0 2.0  1.0
-0.5  1.0  2.0 0.5  1.0
-0.5  0.0   2.0 0.5 0.0
 2.0  1.0  2.0 2.0  1.0
 2.0  0.0   2.0 2.0 0.0
 0.5  0.0   2.0 0.5 0.0
 2.0  1.0  2.0 2.0  1.0
 0.5  1.0  2.0 0.5  1.0
 0.5  0.0   2.0 0.5 0.0
-2.0  1.0  -2.0 0.0  1.0
-2.0  0.0   -2.0 0.0 0.0
-2.0  0.0   -0.5 1.5 0.0
-2.0  1.0  -2.0 0.0  1.0
-2.0  1.0  -0.5 1.5  1.0
-2.0  0.0   -0.5 1.5 0.0
-2.0  1.0   2.0 2.0 1.0
-2.0  0.0   2.0 2.0 0.0
-2.0  0.0   0.5 0.5 0.0
-2.0  1.0  2.0 2.0 1.0
-2.0  1.0  0.5 0.5 1.0
-2.0  0.0   0.5 0.5 0.0
2.0  1.0  -2.0 0.0 1.0
2.0  0.0   -2.0 0.0 0.0
2.0  0.0   -0.5 1.5 0.0
2.0  1.0  -2.0 0.0 1.0
2.0  1.0  -0.5 1.5 1.0
2.0  0.0   -0.5 1.5 0.0
2.0  1.0  2.0 2.0 1.0
2.0  0.0   2.0 2.0 0.0
2.0  0.0   0.5 0.5 0.0
2.0  1.0  2.0 2.0 1.0
2.0  1.0  0.5 0.5 1.0
2.0  0.0   0.5 0.5 0.0
-0.5  1.0  -3.0 0.0 1.0
-0.5  0.0   -3.0 0.0 0.0
-0.5  0.0   -2.0 1.0 0.0
-0.5  1.0  -3.0 0.0 1.0
-0.5  1.0  -2.0 1.0 1.0
-0.5  0.0   -2.0 1.0 0.0
0.5  1.0  -3.0 0.0 1.0
0.5  0.0   -3.0 0.0 0.0
0.5  0.0   -2.0 1.0 0.0
0.5  1.0  -3.0 0.0 1.0
0.5  1.0  -2.0 1.0 1.0
0.5  0.0   -2.0 1.0 0.0
-0.5  1.0  3.0 0.0 1.0
-0.5  0.0   3.0 0.0 0.0
-0.5  0.0   2.0 1.0 0.0
-0.5  1.0  3.0 0.0 1.0
-0.5  1.0  2.0 1.0 1.0
-0.5  0.0   2.0 1.0 0.0
0.5  1.0  3.0 0.0 1.0
0.5  0.0   3.0 0.0 0.0
0.5  0.0   2.0 1.0 0.0
0.5  1.0  3.0 0.0 1.0
0.5  1.0  2.0 1.0 1.0
0.5  0.0   2.0 1.0 0.0
-3.0  1.0  0.5 1.0 1.0
-3.0  0.0   0.5 1.0 0.0
-2.0  0.0   0.5 0.0 0.0
-3.0  1.0  0.5 1.0 1.0
-2.0  1.0  0.5 0.0 1.0
-2.0  0.0   0.5 0.0 0.0
-3.0  1.0  -0.5 1.0 1.0
-3.0  0.0   -0.5 1.0 0.0
-2.0  0.0   -0.5 0.0 0.0
-3.0  1.0  -0.5 1.0 1.0
-2.0  1.0  -0.5 0.0 1.0
-2.0  0.0   -0.5 0.0 0.0
3.0  1.0  0.5 1.0 1.0
3.0  0.0   0.5 1.0 0.0
2.0  0.0   0.5 0.0 0.0
3.0  1.0  0.5 1.0 1.0
2.0  1.0  0.5 0.0 1.0
2.0  0.0   0.5 0.0 0.0
3.0  1.0  -0.5 1.0 1.0
3.0  0.0   -0.5 1.0 0.0
2.0  0.0   -0.5 0.0 0.0
3.0  1.0  -0.5 1.0 1.0
2.0  1.0 -0.5 0.0 1.0
2.0  0.0   -0.5 0.0 0.0

16,212

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧