CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 32 of 32
  1. #31
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateFile Failed problem

    Quote Originally Posted by wklove2003 View Post
    Here is where the declaration is. So 16 characters.
    Code:
    #ifdef __APPLE_CC__
      char mDevice[64];
    #else
      char mDevice[16];
    #endif
    What if the name is more than 16 characters? How are you going to stuff 10 pounds of potatos into a 5 pound bag?
    Code:
     strncpy(mDevice, aDevice, sizeof(mDevice) - 1);
    This effectively truncates that name to 15 characters.
    Code:
    CreateFile on \\.\C:\Documents an=failed with error code: 2"
    That is the message you got back. Look at the name -- it should be much more than 15 characters. How much more? That's what string classes such as CString and std::string are for -- unlimited length strings.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 13th, 2010 at 02:38 PM.

  2. #32
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateFile Failed problem

    For example, this is the StringBuffer code, simplified.
    Code:
    #ifndef STRING_BUFFER_HPP
    #define STRING_BUFFER_HPP
    
    #include <string>
    
    class StringBuffer 
    {
    protected:
      std::string mBuffer; /* A resizable character buffer */
      char mTimestamp[64];
      
    public:
      StringBuffer(const char *aString = 0);
    
      operator const char *() { return m_Buffer.c_str(); }
      const char *append(const char *aString)
      const char* operator<<(const char *aString) { return append(aString); }
      void reset();
      void timestamp();
      size_t  length() { m_Buffer.size(); }
    };
    
    #endif
    Code:
    #include "internal.hpp"
    #include "string_buffer.hpp"
    
    StringBuffer::StringBuffer(const char *aString)
    {
        if ( aString )
              m_Buffer = aString;
    }
    
    const char *StringBuffer::append(const char* aString)
    {
          m_Buffer += mTimestamp;
          m_Buffer += aString;
          return mBuffer.c_str() ;
    }
    
    void StringBuffer::reset()
    {
        m_Buffer.clear();
    }
    
    void StringBuffer::timestamp()
    {
    #ifdef WIN32
      SYSTEMTIME st;
      GetSystemTime(&st);
      sprintf(mTimestamp, "%4d-%02d-%02dT%02d:%02d:%02d.%04d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
    #else
      struct timeval tv;
      struct timezone tz;
      
      gettimeofday(&tv, &tz);
      
      strftime(mTimestamp, 64, "%Y-%m-%dT%H:%M:%S", gmtime(&tv.tv_sec));
      sprintf(mTimestamp + strlen(mTimestamp), ".%06d", tv.tv_usec);
    #endif
    }
    Regards,

    Paul McKenzie

Page 3 of 3 FirstFirst 123

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured