Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 20-Jun-16 3:47am

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:

C++
fileName = QFileDialog::getOpenFileName(this,
    tr("Open Image"), "/home/jana", tr("Image Files (*.txt)"));
 
Share this answer
 
Comments
Member 12587659 20-Jun-16 4:46am    
QFileDialog::getOpenFileName() used in GUI applications. I am looking for getting (browsing)file from Command line interface.
Leo Chapiro 20-Jun-16 4:54am    
Never minds, you can write a console application that USES the GUI elements/dialogs, it's possible! For example, you can call a Message Box from console and so on ...
Please take a look: http://doc.qt.io/qt-5/qapplication.html#details
Member 12587659 20-Jun-16 5:23am    
When I am in Non_GUI section i cant use GUI classes. getting error of QFileDialoge:: No such file or directory
Leo Chapiro 20-Jun-16 7:25am    
To use any of the widget classes, including the file dialog, you need to have an instance of QApplication, although that instance doesn't have to have its exec() method called: http://stackoverflow.com/questions/21568251/qfiledialog-in-a-non-qt-app
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:
C++
#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>

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900