Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to write 3/4 data of a file to another empty file?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to write 3/4 data of a file to another empty file?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scroverty
    wrote on last edited by
    #1

    Hello!

    I have an empty file called fileout.ogg, and a file (filein.ogg) that have 100 seconds of sound.

    So, how can I write into the empty file just the first 75 seconds of filein.ogg with Qt's Classes?

    Thanks for any reply!

    [Below Solved]
    BTW, a little off-topic. I have coded some line to copy files from a directory to another directory, it's like:

    @ QDir source("original");
    QStringList filesList = directory.entryList(QDir::Files);
    QString fileName;
    foreach(fileName, fileList){
    QFile sourceFile (fileName);
    sourceFile.copy (Path/to/tartget/directory/ + fileName);
    }
    @
    It seem I have done something wrong with this, if anyone can point it out, thanks a million!

    Alvis Ar'Berkeley Andrew.
    Pleased to meet you!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      Hmm, I ussually work with QFileInfoList for such operations.
      It's just a guess but I think QDir::entryList(..) only returns the filenames and not their complete paths, and that may be the reason why QFile sourceFile doesn't work and thus sourceFile.copy(..) also doesn't work.

      At first I would suggest you check the return value of QFile::copy()
      @
      if(sourceFile.copy(Path/to/tartget/directory/ + fileName))
      {
      // success
      }
      else
      {
      // failed
      }
      @

      If this always fails then maybe using QDir::entryInfoList(..) and QFileInfo::absoluteFilePath(..)
      will get you the result you're looking for.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        For the audio, you need to:

        Read raw bytes from filein.ogg

        Decode raw bytes (convert them into audio samples)

        Copy 3/4 of the audio samples

        Encode the copied samples (convert them into raw bytes)

        Write raw bytes into fileout.ogg

        There's no Qt function for that... you need a 3rd-party library for decoding/encoding

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • S Offline
          S Offline
          scroverty
          wrote on last edited by
          #4

          Thanks alot, KA51O! Below are my codes for those interested:

          @
          QDir sourcePath("source");
          QStringList filesList = sourcePath.entryList(QDir::Files);
          QString fileName;
          foreach(fileName, fileList){
          QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));
          sourceFile.copy (QDir().toNativeSeperators (QString ("path/to/tartget/directory/") + fileName);
          }

          @

          @JKSH: I thought Qt should have some funtion which help me analize a file's size, then read till it reach the 3/4 point?

          Alvis Ar'Berkeley Andrew.
          Pleased to meet you!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            You can't just take 3/4 of coded file and save it in another...
            This will work with text or raw file formats, but not with extended formats like music/video/archive/documents etc... They all have strict structure of content, with header where the extended information about content/codec name/lenght/quality/compression method, etc is saved.....

            If you want 100sec of ogg file, you need to read file header, get all info about encoding and codecs, when read so called "pages" of ogg, it's usually 4-8kb, they also have header with information how to decompress this page, crc summ, sequence number etc...

            If you like to copy some seconds of ogg file into another one, you should use libogg for that...

            Ogg Encapsulation Format "RFC3533":http://www.ietf.org/rfc/rfc3533.txt
            "Reading Ogg files using libogg":http://www.bluishcoder.co.nz/2009/06/24/reading-ogg-files-using-libogg.html

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [quote author="A.A.B.A" date="1346148193"]I thought Qt should have some funtion which help me analize a file's size, then read till it reach the 3/4 point?[/quote]Qt can:

              • Analyze file size -- QFile::size()
              • Read raw data -- QFile::read()

              Qt CANNOT:

              • Decode/Encode .ogg data

              Why not? Because hundreds and thousands of file types exist in the world. Qt developers don't have time/energy to write encoders/decoders for all of them.

              An .ogg file contains sound data, mixed with OTHER data. Read AcerExtensa's link for details. If you just chop off the last 1/4 of your file, it will become corrupted.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • S Offline
                S Offline
                scroverty
                wrote on last edited by
                #7

                Wow, I didn't know working with other file types could be so complicated.

                Thanks so much for both of your's info, JKSH and AcerExtensa! I'm really apreciated!

                Alvis Ar'Berkeley Andrew.
                Pleased to meet you!

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="A.A.B.A" date="1346155719"]Wow, I didn't know working with other file types could be so complicated.

                  Thanks so much for both of your's info, JKSH and AcerExtensa! I'm really apreciated![/quote]Yea, they're a a little complicated... but that makes them more powerful :)

                  You're welcome!

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #9

                    A.B.B.A.: This is not necessary:
                    @QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));@

                    QFile wants a string in with / as separators, just like any other file related method in Qt. So there is no need to use toNativeSeparators here! That only ever makes sense when presenting pathes to the user.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      scroverty
                      wrote on last edited by
                      #10

                      [quote author="Tobias Hunger" date="1346231933"]A.B.B.A.: This is not necessary:
                      @QFile sourceFile (QDir().toNativeSeperators (sourcePath.absolutePath() + QString ("/") + fileName));@

                      QFile wants a string in with / as separators, just like any other file related method in Qt. So there is no need to use toNativeSeparators here! That only ever makes sense when presenting pathes to the user.[/quote]

                      Hm, I didn't know it.

                      Thanks for your clearance, Tobias!

                      Alvis Ar'Berkeley Andrew.
                      Pleased to meet you!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved