Results 1 to 3 of 3

Thread: Input a text file

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Input a text file

    Hi,

    I am using Qt Creator on a Macbook Pro runing 10.6.2.

    I've been successful with the following code and resource file:

    #include <QTextStream>
    #include <QFile>

    int main()
    {
    QFile data(":/input.txt");

    QString line;

    if (data.open(QFile::ReadOnly)) {
    QTextStream in(&data);
    QTextStream out(stdout);

    out.setCodec("UTF-8");
    in.setCodec("UTF-8");

    do {
    line = in.readLine();
    out << line << endl;
    } while (!line.isNull());
    } else {
    QTextStream out(stdout);
    out << "File not found" << endl;
    }

    }

    Resource file:

    <RCC>
    <qresource prefix="/">
    <file>input.txt</file>
    </qresource>
    </RCC>

    Project file:

    SOURCES += file.cpp
    RESOURCES += file.qrc

    I was trying to do this without a resource file with input.txt in the same directory as all the other project files, but no luck. Here was my attempt, which did not work:

    #include <QTextStream>
    #include <QFile>

    int main()
    {
    QFile data("input.txt");

    QString line;

    if (data.open(QFile::ReadOnly)) {
    QTextStream in(&data);
    QTextStream out(stdout);

    out.setCodec("UTF-8");
    in.setCodec("UTF-8");

    do {
    line = in.readLine();
    out << line << endl;
    } while (!line.isNull());
    } else {
    QTextStream out(stdout);
    out << "File not found" << endl;
    }

    }

    You'll note the only change was to this line:

    QFile data("input.txt");

    I am wondering why this did not work.

    David

  2. #2
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Input a text file

    At least in Windows the executable is (by default) either in debug/release folder and you need to put your text file into debug/release folder instead of the folder where project files are.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Input a text file

    It is not working because your path is wrong. Obviously. It's a relativ path and the starting point is the folder in your application executable. So use an absolute path or a right relative one. Since the wiki is down and and I don't have a mac, I think it was something like that:
    Qt Code:
    1. QDir dir(QCoreApplication::applicationDirPath());
    2. dir.up();
    3. dir.up();
    To copy to clipboard, switch view to plain text mode 
    And then you should be in the "root" directory of your project.

Similar Threads

  1. How to limit the input text in QPlaintextedit?
    By sudhansu in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 10:42
  2. Continuous read of text from an input file
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 00:09
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. read input string --> text mode
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2007, 18:08
  5. [?] Input text programmatically
    By XuTMAH in forum Newbie
    Replies: 3
    Last Post: 24th September 2007, 16:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.