Subject: Re: Problem writting output to a file

Re: Problem writting output to a file

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Sun, 18 Dec 2011 13:07:05 -0800

On Sun, Dec 18, 2011 at 06:58:22PM +0100, vicent roca daniel wrote:
> I'm trying to write the output of a command exectution using libssh2 to a file:
> Some code:
>
>
> /*** definitions, etc...***/
>
> sprintf(filename, "/tmp/.aom/%d-%s.json", epoch, host);
> filefinal = fopen(filename, "w");
>
> /* execute the command...*/
>
> char buffer2[1024*4];
>   
>     while( (read = libssh2_channel_read( channel, buffer2, sizeof(buffer2))) !=
> 0 )
>     {
>       
>         //buffer2[read-1] = '\0';
>         fwrite(buffer2, sizeof(char), read, filefinal);
>        
>     }
>
> /* closing the file ,etc...*/
>
>
> The problem here is tha if this loop iterates only one time, I get some extra
> data at the end of the file.
> I've check that I read and write the same amount of bytes.+
>
> I'm totally lost... any pointers?
> Thank you :)

Note that libssh2_channel_read() can return something other than a size,
namely an error code. I'm not sure what fwrite will do when presented with
a negative size, but it could very well write garbage.

>>> Dan
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2011-12-18