Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. copying a file

copying a file

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 4 Posters 11 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    Can someone tell me if this code works to copy a binary file?

    while ((ch = fgetc(source)) != EOF) {
    fputc(ch, target);
    }

    What if the file contains a byte that is 255? Won't it get converted to -1 and break the loop? Thank you.

    Mircea NeacsuM L K 3 Replies Last reply
    0
    • M mike7411

      Can someone tell me if this code works to copy a binary file?

      while ((ch = fgetc(source)) != EOF) {
      fputc(ch, target);
      }

      What if the file contains a byte that is 255? Won't it get converted to -1 and break the loop? Thank you.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Depends on declaration of ch. If it’s an int (as it should), all is fine as 255 != -1. If it’s char, it fails, with or without compiler warnings.

      Mircea

      1 Reply Last reply
      0
      • M mike7411

        Can someone tell me if this code works to copy a binary file?

        while ((ch = fgetc(source)) != EOF) {
        fputc(ch, target);
        }

        What if the file contains a byte that is 255? Won't it get converted to -1 and break the loop? Thank you.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should not use fgetc, fgets, or any other character based function to process binary files. You should use fread | Microsoft Learn[^] and fwrite | Microsoft Learn[^].

        1 Reply Last reply
        0
        • M mike7411

          Can someone tell me if this code works to copy a binary file?

          while ((ch = fgetc(source)) != EOF) {
          fputc(ch, target);
          }

          What if the file contains a byte that is 255? Won't it get converted to -1 and break the loop? Thank you.

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          While your method may work, assuming that ch is an int, and so does not have the 255/-1 issue, your method is horribly inefficient. Richard's suggestion to use fread/fwrite produces much better performance. For example, given an 8M file, looping through your code 100 times took about 32 seconds. Using a fread/fwrite using a 4K buffer, the same 100 lops took just under 1 second to complete. This was on a PI-3, and successive runs remained stable. I'd expect an 8M file to sit comfortably in the file cache, so the difference is totally down to the difference between extracting a single char at a time, and reading a lot of characters at a time.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups