在NON gui应用程序中浏览数据日志文件(fileio) [英] Browse data log file (fileio) in NON gui application

查看:110
本文介绍了在NON gui应用程序中浏览数据日志文件(fileio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Non Gui Application。我需要从命令行浏览数据记录文件(datalog.txt)。如何在没有Gui的情况下从计算机驱动器浏览文件(datalog.txt)?我正在使用QT。



我尝试了什么:



我不知道命令行。所以任何帮助将不胜感激

I am working on Non Gui Application. I need to browse a datalog file (datalog.txt) from comand line. How I can browse file(datalog.txt) from computer drive without Gui?? I am using QT.

What I have tried:

I dont have any idea of command line. so any help would be appreciated

推荐答案

浏览数据记录文件是什么意思?

如果你只想打开一个文件对话框,尝试使用 QFileDialog类,如下所示:



What do you mean by "browse a datalog file"?
If you just want to open a file dialog, try to use QFileDialog Class like this:

fileName = QFileDialog::getOpenFileName(this,
    tr("Open Image"), "/home/jana", tr("Image Files (*.txt)"));


Qt是一个GUI框架。如果您不需要GUI,则无需使用Qt。然后只使用标准的C和/或C ++库函数。



目前还不是很清楚你想要实现什么。你想打开文本文件并将内容打印到控制台或逐行处理吗?



这很容易。打开文件并逐行读取。或者,将整个内容读入已分配的缓冲区并通过搜索行结束标记来处理行(使用0x0A / LF /换行将适用于Linux和Windows)。



第一个选项的简单示例:

Qt is a GUI framework. If you don't want a GUI, there is no need to use Qt. Then just use standard C and/or C++ library functions.

It is not very clear what you want to achieve. Do you want to open a text file and print the content to the console or process it line by line?

That is quite easy. Open the file and read it line by line. Alternatively, read the whole content into an allocated buffer and process the lines by searching for end of line markers (using 0x0A / LF / line feed will work for Linux and Windows).

A simple example for the first option:
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    // Buffer must be large enough to store the max. line length from file
    char lineBuffer[256];

    if (argc < 2)
    {
        printf("No file name specified on command line\r\n");
        return 1;
    }

    FILE *f = fopen(argv[1], "r");
    if (NULL == f)
    {
        perror("Error opening file");
        return 1;
    }
    else
    {
        while (fgets(lineBuffer, sizeof(lineBuffer), f))
        {
            // Process line here; printf will print it to the console
            printf("%s", lineBuffer);
        }
        fclose(f);
    }
    return 0;
}
</errno.h></stdio.h>



我还没有测试过,但上面的例子也应该有效使用QtCreator时。只需将'CONFIG + = console'添加到项目设置中。


I have not tested it but the above example should also work when using QtCreator. Just add 'CONFIG += console' to the project settings.


这篇关于在NON gui应用程序中浏览数据日志文件(fileio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆