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. ofstream::put() writes 2 bytes instead of 1?!

ofstream::put() writes 2 bytes instead of 1?!

Scheduled Pinned Locked Moved C / C++ / MFC
questionlounge
2 Posts 2 Posters 0 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
    Sprudling
    wrote on last edited by
    #1

    I'm trying to create a function which just creates a file and fills it with random bytes. I want to be able to specify the size. I do this like this...

    bool CreateRandomFile(const char* pszDest, unsigned int nLen)
    {
    if (!nLen) return false;
    static const nByteSize = sizeof(byte) * 256;
    ofstream fDest(pszDest);
    if (!fDest) return false;
    for (int i = 0; i < nLen; i++)
    fDest.put(byte(::rand() % nByteSize));
    fDest.close();
    return true;
    }

    This doesn't work because when (::rand() % nByteSize) becomes 10, 2 bytes are written instead of just the one. The 2 bytes are the standard "\r\n" thing and I guess it's supposed to be this way. But what should I do to get it to work properly? byte is typedef'ed like this:

    typedef unsigned char byte;

    And btw.: fDest.put(10) writes 2 bytes too, and fDest.put(9) writes 1. I'm using the header "fstream" and namespace std. Sprudling :confused:

    J 1 Reply Last reply
    0
    • S Sprudling

      I'm trying to create a function which just creates a file and fills it with random bytes. I want to be able to specify the size. I do this like this...

      bool CreateRandomFile(const char* pszDest, unsigned int nLen)
      {
      if (!nLen) return false;
      static const nByteSize = sizeof(byte) * 256;
      ofstream fDest(pszDest);
      if (!fDest) return false;
      for (int i = 0; i < nLen; i++)
      fDest.put(byte(::rand() % nByteSize));
      fDest.close();
      return true;
      }

      This doesn't work because when (::rand() % nByteSize) becomes 10, 2 bytes are written instead of just the one. The 2 bytes are the standard "\r\n" thing and I guess it's supposed to be this way. But what should I do to get it to work properly? byte is typedef'ed like this:

      typedef unsigned char byte;

      And btw.: fDest.put(10) writes 2 bytes too, and fDest.put(9) writes 1. I'm using the header "fstream" and namespace std. Sprudling :confused:

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Open the file in binary mode. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      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