Thread: From stream/file to a string array problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > which includes special characters
    The special character which is the problem is \0

    Any use of strcpy or any printf command using "%s" as the conversion will come unstuck.

    If you want to pass off your encrypted text as ascii data, then you need to convert it to printable characters (like uuencode or mime does). Then you'll be able to use normal string functions to deal with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    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);

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another String Problem
    By w2look in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 12:00 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM