• 6301阅读
  • 10回复

请教一个读文本取字符的问题! 已解决,谢谢大家 [复制链接]

上一主题 下一主题
离线无根野草
 
只看楼主 倒序阅读 楼主  发表于: 2008-03-05
— 本帖被 XChinux 执行加亮操作(2008-03-05) —
文本devices.txt中有这如下这么一段:

。。。。。。
I: Bus=0011 Vendor=0002 Product=0007 Version=01b1
N: Name="SynPS/2 Synaptics TouchPad"
P: Phys=isa0060/serio4/input0
S: Sysfs=/class/input/input2
U: Uniq=
H: Handlers=mouse1 event2
B: EV=b
B: KEY=6420 0 70000 0 0 0 0 0 0 0 0
B: ABS=11000003
。。。。。。

我想通过 "SynPS/2 Synaptics TouchPad"这一关键字定位到如上这个段落,然后提取该段中的event2关键字,请问该如何操作,大家给指导一下,小弟谢了
[ 此贴被无根野草在2008-03-06 10:31重新编辑 ]
在线XChinux

只看该作者 1楼 发表于: 2008-03-05
devices.txt有多大?
如果不大的话直接全部 进来然后就像处理普通字符串那样处理。
否则读文件一行一行的读,每读一行搜索是否有SynPS/2 Synaptics TouchPad,如果有则做标记,然后再见往下读四行即可。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线无根野草
只看该作者 2楼 发表于: 2008-03-05
能不能给个示例程序,小弟刚接触QT,看了QT的QFile,QString,QStringlist等类,不是很明白,大哥给个示例就好了,谢谢
离线无根野草
只看该作者 3楼 发表于: 2008-03-05
刚学QT,折腾了一点点,大家帮改一下
                QFile file("devices.txt");
                QStringList lines;
                if ( file.open( IO_ReadOnly ) ) {
                    QTextStream stream( &file );
                    QString line[255];
                    int n = 0;
                    while ( !stream.atEnd() ) {
                    for (n=0;!line[n].contains("\" Synaptics TouchPad\"");n++)
                    {
                        line[n] = stream.readLine();
                    }
                        printf( "%s\n", line[n+4] );
                    }
                    file.close();
                }

说是有段错误,不知道那出错了。。。
[ 此贴被无根野草在2008-03-05 15:56重新编辑 ]
离线无根野草
只看该作者 4楼 发表于: 2008-03-05
说是有段错误,不知道那出问题了。。。
离线浪漫天使
只看该作者 5楼 发表于: 2008-03-05
QString line[255];
n>255怎么办?
离线无根野草
只看该作者 6楼 发表于: 2008-03-05
那应该如何定义呢?请指教下
                QFile file("/proc/bus/input/devices");
                if ( file.open( IO_ReadOnly ) ) {
                    QTextStream stream( &file );
                    QString line[255];
                    int n = 0;
                    for (n=0;!stream.atEnd();n++)
                    {
                        line[n] = stream.readLine();
                    if (line[n].contains("\" Synaptics TouchPad\""))
                        cout<<endl<<"TouchPad adapter:"<<line[n+4].latin1()<<endl<<endl;
                    }
                    file.close();
                }
如上改了后虽然没有段错误,但还是没有显示任何数据
离线tingxx

只看该作者 7楼 发表于: 2008-03-05
QString line[255];
这是一个什么东东?为什么不用QStringList??
QT自带的文档真是个好东西
一定要好好看呀
离线无根野草
只看该作者 8楼 发表于: 2008-03-05
大虾们能不能直接给个示例啊,小弟刚学QT,好多东西不太明白.....
离线zncggaofei
只看该作者 9楼 发表于: 2008-03-05
首先分析一下这段代码:
QFile file("/proc/bus/input/devices");
                if ( file.open( IO_ReadOnly ) )
                {
                    QTextStream stream( &file );
                    QString line[255];                            //这里定义了一个类数组
                    int n = 0;
                    for (n=0;!stream.atEnd();n++)
                    {
                        line[n] = stream.readLine();
                        if (line[n].contains("\" Synaptics TouchPad\""))
                        {
                                cout<<endl<<"TouchPad adapter:"<<line[n+4].latin1()<<endl<<endl;  //你的n+4还没有读出来 怎么会有显示
                        }
                    file.close();
              }

这是我的代码,如有不妥请指出, 谢谢
QFile file("../devices.txt");
    if (file.open(QIODevice::ReadOnly))
    {
        QTextStream stream(&file);
        QString line[255];
        static int a;
        for (int n=0;!stream.atEnd();n++)
        {
            line[n] = stream.readLine();
            if (line[n].contains("Synaptics TouchPad"))
            {
                a = n + 4;
            }
        }
        cout <<"TouchPad adapter:"<< line[a].toLatin1().data()<<endl;
    }
    file.close();

我估计是丑陋了一点 呵呵
There is someone that is coming or passing away in your life around the clock, so you may lose sight of those seen, and forget those remembered. There is gain and loss in your life, so you may catch sight of those unseen, and remember those forgotten. Nevertheless, doesn’t the unseen exist for sure? Will the remembered remain forever?
离线无根野草
只看该作者 10楼 发表于: 2008-03-06
已解决,谢谢大家,贴出代码:
                  QStringList lines;
                  QFile file( "/proc/bus/input/devices" );
                  if ( file.open( IO_ReadOnly ) ) {
                  QTextStream stream( &file );
                  QString line;
                  int n = 1;
                  while ( !stream.eof() ) {
                  line = stream.readLine();
                    if (line.contains("Synaptics TouchPad"))
                      {
                        for(n=1;n<=4;n++)
                        {
                        line = stream.readLine();
                        }
                    cout<<endl<<"TouchPad adapter:"<<line.latin1()<<endl<<endl;
                      }
                    lines += line;
                  }
                    file.close();
                }
快速回复
限100 字节
 
上一个 下一个