Results 1 to 5 of 5

Thread: Read from a file

  1. #1
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Read from a file

    Hello.
    I want to read data from file to the multi-dimensional array.
    For example, I have data in file:

    |1 2 3 4 5|
    |2 3 4 5 1|
    |3 2 1 5 2|
    |2 6 3 1 2|

    and want to assign this to the table. In c++ i do this that:

    Qt Code:
    1. ifstream file("name");
    2.  
    3. for(int i=0; i<4; i++)
    4. for(int n=0; n<4; n++)
    5. file>>Data[i][n];
    To copy to clipboard, switch view to plain text mode 

    but here this not work.

    I try to use QFile and QTextStream but I still dont know how to do it.

  2. #2
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read from a file

    Check for failbits. If your file format is the same as the example you gave, then there will be errors reading the data from standard input.

  3. #3
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Read from a file

    Ok, but I asking about another way to read from file (the same as the example), becouse ifstream call me:
    Qt Code:
    1. (...)mainwindow.cpp:16: error: ‘ifstream’ was not declared in this scope
    To copy to clipboard, switch view to plain text mode 
    though I include fstream at the start of file..

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read from a file

    Qt is not a language, it's a framework. Whilst it's preferably to use QFile, you don't have to.

  5. #5
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read from a file

    Mole is right. You're using C++ (the language) to build an application. Qt (the framework) enhances and provides pre-built functionality for your application.

    For the C++ side of this question (which is really beyond the scope of these forums), you may have included fstream, but what namespace are you using? If you didn't tell the compiler you're pulling this functionality from the standard namespace ("using namespace std;" or "using std::ifstream;"--the latter being preferred), you'll need to declare "ifstream" as "std::ifstream".

    These all do the same thing:
    Qt Code:
    1. #include <fstream>
    2.  
    3. int main()
    4. {
    5. std::ifstream test("test.txt");
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <fstream>
    2. using namespace std; // not preferred
    3.  
    4. int main()
    5. {
    6. ifstream test("test.txt");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <fstream>
    2. using std::ifstream;
    3.  
    4. int main()
    5. {
    6. ifstream test("test.txt");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Like I said, explaining this further is well beyond the scope of these forums.

    From a Qt and QFile perspective, look at the docs for QFile. It's pretty easy to read a simple text file.
    Last edited by TheJim01; 26th April 2010 at 19:09. Reason: updated contents

Similar Threads

  1. Read the file into
    By offline in forum Qt Programming
    Replies: 7
    Last Post: 12th April 2010, 12:11
  2. How to read a XML file that uses UTF-8?
    By PaladinKnight in forum Newbie
    Replies: 2
    Last Post: 10th April 2010, 13:52
  3. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  4. Replies: 1
    Last Post: 20th June 2008, 18:43
  5. Read An Xml File
    By Alienxs in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2007, 00:28

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.