Results 1 to 8 of 8

Thread: Read&Writee in file

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Read&Writee in file

    i want to copy the contents of one file to another file.But using the following code only first line is copied;

    Qt Code:
    1. QFile f("/root/Desktop/n.txt");
    2. f.open(QIODevice::ReadOnly | QIODevice::Text);
    3. QFile f1("nn.txt");
    4. f1.open(QIODevice::WriteOnly | QIODevice::Text);
    5.  
    6. QTextStream in(&f);
    7. QTextStream out(&f1);
    8.  
    9. while(!f.atEnd())
    10. {
    11. in>>q;
    12. out<<q;
    13.  
    14. }
    15. f.close();
    16. f1.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 7th February 2011 at 11:54. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    Why don't you use QFile::copy?

    how is 'q' defined?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    16
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    q is of type QString.i want to copy line by line,so that i can get some details seperated from the file.

  4. #4
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    q is of type QString.i want to copy line by line,so that i can get some details seperated from the file.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    1. Operator >> reads word not line.
    2. Use QTextStream::readline.

  6. #6
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    @Leisok: thanks... i also wanted to know how to write the data line by line..

  7. #7
    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&Writee in file

    Did you try using operator << ?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read&Writee in file

    Quote Originally Posted by cebin View Post
    @Leisok: thanks... i also wanted to know how to write the data line by line..
    Qt Code:
    1. out << q << '\n';
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Read from a file
    By matulik in forum Newbie
    Replies: 4
    Last Post: 26th April 2010, 19:02
  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
  •  
Qt is a trademark of The Qt Company.