Benjamin Leblanc
2009-Jan-28 16:46 UTC
[Rd] File and console output does not work in C code
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? 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
Prof Brian Ripley
2009-Jan-28 17:43 UTC
[Rd] File and console output does not work in C code
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 >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Benjamin Leblanc
2009-Jan-29 11:26 UTC
[Rd] File and console output does not work in C code
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 >> >> > >