From stream/file to a string array problem
Hi,
I am having a problem with retrieving data from a socket stream.
I want to pass some encrypted data, which includes special characters over a TCP socket, this part is working OK - the number of bytes sent coresponds to the number of bytes received.
The problem is when I try and read the contents of the data into a string array (array of char). The strcpy fn I want to use to copy the buffer only copies part of the data.
The code is as follows:
while((rc=sockRead(isock_fd,szbuf,sizeof(szbuf))))
{
sprintf(received, "\n%d\n", rc);
fprintf(log, "\nNumber of bytes received by sockRead %s\n", received);
write(STDOUT_FILENO, szbuf, rc);
szcontent = (unsigned char *) malloc((rc+1) * sizeof(unsigned char));
strcpy(szcontent, szbuf);
sprintf(written, "\n%d\n", strlen(szcontent));
fprintf(log, "\nWritten to file: %s\n", written);
(void) fprintf(log,"content in loop=%s\n",szcontent);
}
I know the data arrives correctly, because when I write to stdout, it is all present and correct. Also, I can write it to a file. However, this is no use, as when I write to a file, then read back from the file, the same problem is encountered.
The data transferred is:
/export/home/dradsws/tmpcoe/0660838006.1001928501cadccdhlkfdjbedchicfcfe.0tttt tt
::s0128NSw`;3Yȯ
.c]9;fK&ˋpTǣm/%с;:co5)Nbɏ aoLM$N(m:^Жؗ~%QeqbC2dLR$t8T2 IiٳPm~[ؿ/\.
4]4C+yK8z;MM]?h`wȣOq
9fҀ>=3;շ^ԆiM..@C)f!'l6X%e )Ϙbdv0jO9FZNv?..
Li:+!G^2ưfWZ_}jcAn} `o..M
6)qG7+nkó3
strcpy only copies up to ::s0128 i.e.
/export/home/dradsws/tmpcoe/0660838006.1001928501cadccdhlkfdjbedchicfcfe.0tttt tt::s0128
Anyone got any ideas. I need to pass a filename and content over a stream, but now I can't write the stream to a variable!!
Please help somebody!!
Thanks
Steve:confused:
Re: From stream/file to a string array problem
#include <stdio.h>
size_t fwrite(const void *buf, size_t size,size_t count, FILE *fp); Description
fwrite attempts to copy, starting from the memory location
buf, count elements (each of size size) into the file or
stream identified by fp. fwrite may copy fewer elements than
count if an error intervenes.
fwrite also advances the file position indicator (if any) for
fp by the number of characters actually written.
open log in the binary mode fwrite(szcontent,1,rc,log);
Quote:
Originally posted by dradsws
Hi,
I am having a problem with retrieving data from a socket stream.
I want to pass some encrypted data, which includes special characters over a TCP socket, this part is working OK - the number of bytes sent coresponds to the number of bytes received.
The problem is when I try and read the contents of the data into a string array (array of char). The strcpy fn I want to use to copy the buffer only copies part of the data.
The code is as follows:
while((rc=sockRead(isock_fd,szbuf,sizeof(szbuf))))
{
sprintf(received, "\n%d\n", rc);
fprintf(log, "\nNumber of bytes received by sockRead %s\n", received);
write(STDOUT_FILENO, szbuf, rc);
szcontent = (unsigned char *) malloc((rc+1) * sizeof(unsigned char));
strcpy(szcontent, szbuf);
sprintf(written, "\n%d\n", strlen(szcontent));
fprintf(log, "\nWritten to file: %s\n", written);
(void) fprintf(log,"content in loop=%s\n",szcontent);
}
I know the data arrives correctly, because when I write to stdout, it is all present and correct. Also, I can write it to a file. However, this is no use, as when I write to a file, then read back from the file, the same problem is encountered.
The data transferred is:
/export/home/dradsws/tmpcoe/0660838006.1001928501cadccdhlkfdjbedchicfcfe.0tttt tt
::s0128NSw`;3Yȯ
.c]9;fK&ˋpTǣm/%с;:co5)Nbɏ aoLM$N(m:^Жؗ~%QeqbC2dLR$t8T2 IiٳPm~[ؿ/\.
4]4C+yK8z;MM]?h`wȣOq
9fҀ>=3;շ^ԆiM..@C)f!'l6X%e )Ϙbdv0jO9FZNv?..
Li:+!G^2ưfWZ_}jcAn} `o..M
6)qG7+nkó3
strcpy only copies up to ::s0128 i.e.
/export/home/dradsws/tmpcoe/0660838006.1001928501cadccdhlkfdjbedchicfcfe.0tttt tt::s0128
Anyone got any ideas. I need to pass a filename and content over a stream, but now I can't write the stream to a variable!!
Please help somebody!!
Thanks
Steve:confused: