From benjamin.leblanc at igh.cnrs.fr  Thu Jan 29 12:26:03 2009
From: benjamin.leblanc at igh.cnrs.fr (Benjamin Leblanc)
Date: Thu, 29 Jan 2009 12:26:03 +0100
Subject: [Rd] File and console output does not work in C code
In-Reply-To: <3225F02A-9599-4386-90F0-E4F2088C6C57@r-project.org>
References: <49808BD5.2090105@igh.cnrs.fr>
	<alpine.LFD.2.00.0901281738030.600@gannet.stats.ox.ac.uk>
	<4980A7A5.50309@igh.cnrs.fr>
	<3225F02A-9599-4386-90F0-E4F2088C6C57@r-project.org>
Message-ID: <4981924B.7000509@igh.cnrs.fr>

Hi Simon,
Yes, absolutely right, I just did this (...) mistake using copy/paste 
and forgetting to rename the called function.
Thank you for the help and the reference to graphic packages.

Benjamin


Simon Urbanek a ?crit :
> Ben,
>
> just FYI - GDD package uses libgd to create R graphics images so you 
> can have a look at that -- and even better you can use Cairo which has 
> much better rendering than libgd ... You can look at the sources if 
> you want to customize that ...
>
> Cheers,
> S
>
> PS: I don't think your c_bricks code gets called at all - that's 
> probably why you don't see any output.
>
>
> On Jan 28, 2009, at 13:44 , Benjamin Leblanc wrote:
>
>> Here are more specific details: I use GNU/Linux Ubuntu 8.04 on an 
>> x86_64 workstation. R environnement was compiled from 2.8.0 sources.
>>
>> Below is a part of my C code to test the file I/O.
>> When compiled as a standalone executable it prints "Test    1" on the 
>> console and generates "test.txt" and "test.png" file outputs as 
>> expected. When compiled as a shared library and invoked from R 
>> environment via .C() interface, it does nothing (to avoid any file 
>> access issue, file permissions were set to 777 in the "/test" output 
>> folder).
>>
>> Am I missing a configuration option?
>> Thanks for your help,
>>
>>
>> ----
>>
>>
>> #include <R.h>
>> #include <Rmath.h>
>> #include <stdio.h>
>> #include <gd.h>
>>
>> // 
>> ------------------------------------------------------------------------------ 
>>
>> void c_bricks(double *x, int *nx, int *nw, double *gamma, double 
>> *Piw, double *V, double *W)  {
>>
>>   fflush(stdout);
>>   printf("Test %10d\n", 1);
>>   Rprintf("Test %10d\n", 2);
>>   REprintf("Test %10d\n", 3);
>>
>>   FILE *txtlog;
>>   txtlog = fopen("/test/test.txt", "w");
>>   fprintf(txtlog, "Test %10d\n", 1);
>>   fclose(txtlog);
>>
>>
>>   // *** Code imported from the GD2 tutorial, 
>> http://www.libgd.org/Manual ***
>>   // Declare the image
>>   gdImagePtr im;
>>   // Declare output files
>>   FILE *pngout;
>>   // Declare color indexes
>>   int black;
>>   int white;
>>   // Allocate the image: 64 pixels across by 64 pixels tall
>>   im = gdImageCreate(64, 64);
>>   /* Allocate the color black (red, green and blue all minimum).
>>   Since this is the first color in a new image, it will
>>   be the background color. */
>>   black = gdImageColorAllocate(im, 0, 0, 0);
>>   /* Allocate the color white (red, green and blue all maximum). */
>>   white = gdImageColorAllocate(im, 255, 255, 255);
>>   /* Draw a line from the upper left to the lower right,
>>   using white color index. */
>>   gdImageLine(im, 0, 0, 63, 63, white);
>>   /* Open a file for writing. "wb" means "write binary", important
>>   under MSDOS, harmless under Unix. */
>>   pngout = fopen("/test/test.png", "wb");
>>
>>   /* Output the image to the disk file in PNG format. */
>>   gdImagePng(im, pngout);
>>   /* Close the files. */
>>   fclose(pngout);
>>   /* Destroy the image in memory. */
>>   gdImageDestroy(im);
>>
>> }
>>
>>
>>
>> Prof Brian Ripley a ?crit :
>>> On Wed, 28 Jan 2009, Benjamin Leblanc wrote:
>>>
>>>> Hello all,
>>>> I am getting into trouble when trying to do standard I/O from a C 
>>>> function called within R environment.
>>>>
>>>> This function is written in a small C library that is loaded whith 
>>>> dyn.load() and called in R via the .C() interface. I need to debug 
>>>> the C code using a text file for some logs, and also to speed up 
>>>> its result visualization by creating images on the fly using the GD 
>>>> library.
>>>> Both of these outputs require to write into files from this C 
>>>> function.
>>>> Allthough everything goes smoothly from compilation and linking to 
>>>> execution into R without errors, no file is created. I have also 
>>>> tried to do some simple printf(), Rprintf() and REprintf() but even 
>>>> that didn't gave me any visible output.
>>>>
>>>> Am I missing something trivial? Could anyone give me a hint on why 
>>>> it does not work?
>>>
>>> It works in many other packages.  What OS are you using?  (There 
>>> known issues with using stdout and stderr from GUIs, but those are 
>>> documented in the R-exts manual.)
>>>
>>>> Best regards,
>>>>
>>>> Benjamin
>>>>
>>>> -- 
>>>> Chromatin and Cell Biology Lab.
>>>> Institute for Human Genetics
>>>> 141, rue de la Cardonille
>>>> F-34396 Montpellier
>>>> France
>>>> Phone  +33-(0)4 99 61 99 51
>>>> FAX      +33-(0)4 99 61 99 01
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>>
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
>



[Rd] File and console output does not work in C code
Benjamin Leblanc