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

Benjamin Leblanc benjamin.leblanc at igh.cnrs.fr
Wed Jan 28 19:44:53 CET 2009


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



More information about the R-devel mailing list