Thanks a lot Matt, Just if someone like to see how I called a C function from R with files as argumets. For simplicity, this example is copying the content of input file into output file
----------------------------------------- My main program is : source("parse.R") parseGBest('gbest40.seq', 'gbest40.out'); // .seq is input file, .out is output file --------------------------------------- I wrote a wrapper function (parse.R) as follows: dyn.load("parse.so"); parseGBest= function(infile, outfile) { # Do some whatever you like here .C("parse", infile, outfile) } -------------------------------------- parse.c File looks like follows: #include<R.h> #include <stdio.h> #include <stdlib.h> #include<string.h> void adel(char **infile, char **outfile) { char line[81]; FILE *fr, *of; if (!(fr = fopen(*infile, "r"))) { fprintf(stdout, "Error while opening input file\n"); return ; } if (!(of = fopen(*outfile, "w"))) { fprintf(stdout, "Error while opening output file\n"); return ; } while(fgets(line, 81, fr) != NULL) { fputs(line, of); } fclose(fr); fclose(of); } -------------------------------------- Thats it!!!!!!!!!!!!! I saved almost a week in parsing all those genbank est files(almost 413 files). Thanks R&C interface team. --Fahim On Wed, Jul 14, 2010 at 10:18 AM, Matt Shotwell <shotw...@musc.edu> wrote: > Fahim, > > Please see the Writing R Extensions manual > http://cran.r-project.org/doc/manuals/R-exts.pdf > > There are simple instructions in this document under the heading "System > and foreign language interfaces." > > -Matt > > > On Wed, 2010-07-14 at 01:21 -0400, Fahim Md wrote: > > Hi, > > I am trying to call a C function, that I wrote to parse a flat file, > into > > R. The argument that will go into this function is an input file that I > need > > to parse and write the desired output in an output file. I used some hit > > and trial approach but i keep on getting the "file not found" or > > "segmentation fault" error. I know that the error is in passing the > argument > > but I could not solve it. > > > > After reading some of the tutorials, I understood how to do this if the > > arguments are integers or floats. I am stuck when i am trying to send the > > files. I am attaching stub of each file. > > Help appreciated. > > Thanks > > > > --------------------------------------- > > My function call would be: > > source("parse.R") > > parseGBest('./gbest/inFile.seq', './gbest/outFile.out'); > > --------------------------------------- > > I wrote a wrapper function (parse.R) as follows: > > > > dyn.load("parse.so"); > > parseGBest = function(inFile, outFile) > > { > > .C( "parse" , inFile , outFile); > > } > > > > How to write receive the filenames in function( , ) above. and how to > call > > .C > > > > ---------------------------------------- > > parse.c file is as below: How to receive the argument in funcion and how > to > > make it compatible with my argv[ ]. > > > > > > void parse( int argc, char *argv[] ) //This is working as standalone C > > program. How to receive > > // the above files so > that > > it become compatible with my argv[ ] > > { > > > > FILE *fr, *of; > > char line[81]; > > > > > > if ( *argc == 3 )*/ > > { > > if ( ( fr = fopen( argv[0], "r" )) == NULL ) > > { > > puts( "Can't open input file.\n" ); > > exit( 0 ); > > } > > if ( ( of = fopen( argv[1], "w" )) == NULL ) > > { > > puts( "Output file not given.\n" ); > > } > > } > > else > > {printf("wrong usage: Try Agay!!! correct usage is:= > functionName > > inputfileToParse outFileToWriteInto\n"); > > } > > while(fgets(line, 81, fr) != NULL) > > > > -- > > --- > > -- > > } > > > > > > > > Thanks again > > Fahim > > > -- > Matthew S. Shotwell > Graduate Student > Division of Biostatistics and Epidemiology > Medical University of South Carolina > http://biostatmatt.com > > -- Fahim Mohammad Bioinforformatics Lab University of Louisville Louisville, KY, USA Ph: +1-502-409-1167 [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.