468,496 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

How to store the data in buffer

18
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...
Expand|Select|Wrap|Line Numbers
  1. void print ( char *Buffer, usigned long bufferSize, int chunkSize );
  2.  
  3. /*This fn will accept file buffer and the buffer size and the chunk size
  4. Send the buffer in chunks which is provided by the parameter.
  5. */
  6.  
  7. FILE *fp23 = fopen( "c:\\file.txt", "rb" );
  8.  
  9. fseek (fp23 , 0 , SEEK_END);
  10. int m_jobSize = ftell (fp23);
  11. rewind (fp23);
  12.  
  13. char* m_buffer = new char[m_jobSize+1];
  14. int m_result = fread( m_buffer, 1, m_jobSize, fp23 );
  15. int chunkSize = 1;
  16. void print ( m_buffer, m_jobSize, chunkSize);
  17.  
  18.  
  19. void print ( char *m_buffer, usigned long m_jobSize, int chunkSize)
  20. {
  21.     int BytesIndex = 0;
  22.     while( bufsize > 0) 
  23.     {
  24.           if ( bufsize < (unsigned long)iSendStatus ) // Send some bytes
  25.           {
  26.               iSendStatus = send(*sock, &m_buffer[BytesIndex], bufsize, 0); 
  27.                                                           // Call Back Function
  28.               set += iSendStatus;
  29.               ptr(jobid,set);
  30.               if ( iSendStatus != bufsize )
  31.               {
  32.                    cpError = SocketErrorHandler();
  33.                    return cpError;
  34.               }
  35.  
  36.          }
  37.          else
  38.          {
  39.              iSendStatus = send(*sock, &m_buffer[BytesIndex], chunkSize, 0); 
  40.                                                          // Call Back Function
  41.              set += iSendStatus;
  42.              ptr(jobid,set);
  43.  
  44.              if ( iSendStatus != chunkSize )
  45.              {
  46.                  cpError = SocketErrorHandler();
  47.                  return cpError;
  48.              }
  49.  
  50.         } 
  51.                                                // Update buffer and counter
  52.        if ( bufsize < (unsigned long)iSendStatus )
  53.        {
  54.           bufsize -= bufsize;
  55.           BytesIndex += bufsize;
  56.        }
  57.       else
  58.       { 
  59.          bufsize -= iSendStatus;
  60.          BytesIndex +=iSendStatus;
  61.       }
  62. }
  63.  

Anyone can help..!
Thanks :)
Mar 20 '08 #1
3 7241
weaknessforcats
9,207 Expert Mod 8TB
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().
Mar 20 '08 #2
jhamb
18
Couldn't make out with the work, still.
Dint understood, how to do..!
Mar 28 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
This is fread() prototype:
Expand|Select|Wrap|Line Numbers
  1. size_t fread( 
  2.    void *buffer,
  3.    size_t size,
  4.    size_t count,
  5.    FILE *stream 
  6. );
  7.  
To read 1000 ints you would:
Expand|Select|Wrap|Line Numbers
  1. int buffer[1000]
  2. fread(buffer, sizeof(int), 1000, myFile);
  3.  
Mar 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by clwoods | last post: by
3 posts views Thread by Forconi Boris via .NET 247 | last post: by
2 posts views Thread by jmensch | last post: by
36 posts views Thread by Crirus | last post: by
3 posts views Thread by piotrek | last post: by
5 posts views Thread by res7cxbi | last post: by
7 posts views Thread by TYF | last post: by
nurulshidanoni
3 posts views Thread by nurulshidanoni | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.