Results 1 to 6 of 6

Thread: break a file

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default break a file

    Hello,
    I have a text file with eg. 600 lines. I have to break it into 3 and create 3 new file each with 200 lines. What's the better way?. Furthermore: 600 lines is a lucky case because it could be 599. In this case I have to spilt it into 3 parts more equal as possbile.....

    Thanks-
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: break a file

    INT = 599 / 3 = 199
    FRAC = 599 % 3 = 2

    if FRAC !=0 then INT = INT + 1

    First two files get INT lines each and the last one gets the rest.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: break a file

    yes but what I was wondering was how retrieve the number of lines....what's the most quick method. Simpling counting the lines before split?
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: break a file

    The most straightforward way is not to retrieve the number of lines at all. If the way lines are divided doesn't matter then you can split files the same way as you deal cards - one line to file 1, next to file 2, next to file 3, next to file 1, etc. If you need first 200 lines to go to file 1, next 200 to file 2 and the remaining 199 to file 3, then you'll have to count lines either by reading all lines into a list and counting it (if the file is small) or scan the file for new line characters and count them (and add 1).

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: break a file

    EDIT: I have to put a percentuge to file1, to file2 and to file3 (eg. I have int p1=0.5, p2=0.4, p3=0.1), but important the choice of the lines must be at random......
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: break a file

    Two choices then:
    1. read all lines into memory, shuffle and write back to files at given percentage
    2. read lines one by one and write to a random file but always check that you don't go over the desired percentage limit (you'll have to keep the number of lines already stored in each file). In the end you should reach the desired percentages.

Similar Threads

  1. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  2. Replies: 2
    Last Post: 8th November 2007, 20:15
  3. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

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.