How to store the data in buffer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jhamb
    New Member
    • Feb 2008
    • 18

    How to store the data in buffer

    Hello,

    I want to read a file and to pass the values in a buffer but the values are of different datatype. So I cant put the values in the char type buffer.
    What to do?

    I found this but dint worked for me...
    Code:
    void print ( char *Buffer, usigned long bufferSize, int chunkSize );
    
    /*This fn will accept file buffer and the buffer size and the chunk size
    Send the buffer in chunks which is provided by the parameter.
    */
    
    FILE *fp23 = fopen( "c:\\file.txt", "rb" );
    
    fseek (fp23 , 0 , SEEK_END);
    int m_jobSize = ftell (fp23);
    rewind (fp23);
    
    char* m_buffer = new char[m_jobSize+1];
    int m_result = fread( m_buffer, 1, m_jobSize, fp23 );
    int chunkSize = 1;
    void print ( m_buffer, m_jobSize, chunkSize);
    
    
    void print ( char *m_buffer, usigned long m_jobSize, int chunkSize)
    {
        int BytesIndex = 0;
        while( bufsize > 0) 
        {
              if ( bufsize < (unsigned long)iSendStatus ) // Send some bytes
              {
                  iSendStatus = send(*sock, &m_buffer[BytesIndex], bufsize, 0); 
                                                              // Call Back Function
                  set += iSendStatus;
                  ptr(jobid,set);
                  if ( iSendStatus != bufsize )
                  {
                       cpError = SocketErrorHandler();
                       return cpError;
                  }
    
             }
             else
             {
                 iSendStatus = send(*sock, &m_buffer[BytesIndex], chunkSize, 0); 
                                                             // Call Back Function
                 set += iSendStatus;
                 ptr(jobid,set);
    
                 if ( iSendStatus != chunkSize )
                 {
                     cpError = SocketErrorHandler();
                     return cpError;
                 }
    	
            } 
                                                   // Update buffer and counter
           if ( bufsize < (unsigned long)iSendStatus )
           {
              bufsize -= bufsize;
              BytesIndex += bufsize;
           }
          else
          { 
             bufsize -= iSendStatus;
             BytesIndex +=iSendStatus;
          }
    }

    Anyone can help..!
    Thanks :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    fread() doesn't care what the buffer type is. That argument is a void*. Just define your buffer of the correct type and pass the address to fread().

    Comment

    • jhamb
      New Member
      • Feb 2008
      • 18

      #3
      Couldn't make out with the work, still.
      Dint understood, how to do..!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is fread() prototype:
        [code=c]
        size_t fread(
        void *buffer,
        size_t size,
        size_t count,
        FILE *stream
        );
        [/code]

        To read 1000 ints you would:
        [code=c]
        int buffer[1000]
        fread(buffer, sizeof(int), 1000, myFile);
        [/code]

        Comment

        Working...