On Tue, 22 Jul 2008 22:59:49 -0700, rudra wrote:
> dear friend, i have a C++ routine(badly written):
>
>
> main()
> {
> ofstream myfile;
> /********* PART 1: Checking machine bit *********/
>
> FILE *stream;
> char sys[6],*sysptr;
> int bit;
> sysptr = &sys[0];
> sys[6] = 0x0;
should be sys[5]=0x0;
> stream=popen("/bin/uname -m","r");
> fread(sysptr,1,6,stream);
> pclose(stream);
> //printf("%s\n",sysptr);
>
> if ((strncmp(sysptr, "i686",4)==0)||
> (strncmp(sysptr, "i386",4)==0))
> {
> bit=32;
> }
> else {
> bit=64;
> }
> myfile.open ("machine");
> myfile << bit<<"\n";
> myfile.close();
> printf("This is a %d bit machine \n",bit);
> }
>
Are you just looking for the information uname gives or are you
experimenting
with popen? If yes to the former, do something like this instead:
#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <string.h>
#include <sys/utsname.h>
int my_bit_count_(void)
{
struct utsname INFO;
int bits=-1;
if(uname(&INFO) < 0)
perror("uname");
else
{
/*
printf("sysname\t%s\n",INFO.sysname);
printf("release\t%s\n",INFO.release);
printf("version\t%s\n",INFO.version);
printf("machine\t%s\n",INFO.machine);
printf("nodename\t%s\n",INFO.nodename);
// printf("domainname%s\n",INFO.domainname);
*/
if(strcmp(INFO.machine,"i686")==0)
bits=64;
else
bits=32;
}
return bits;
}
> wose output is written in "machine" file. a f90 program is supposed to
> read it as
>
>
>
> open(2,file="machine",status="old")
> read(2,*) bit
> close(2)
>
> I want to use the C code as a function of the f90 program. can anybody
> tell me how can i do that?
And then call the c function something like this:
program main
implicit none
integer my_bit_count
integer bitcount
bitcount = my_bit_count()
write(*,*)bitcount
stop
end
compile with gfortran a.c b.f -W -Wall if yuo have a recent gcc installed.
Jörgen


|