LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2005, 02:11 PM   #1
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
How to output a C stream into a binary file readable via Fortran I/O?


Hi,

I need to write numerical data from an already-written C code to a binary formatted file. This data will then be read via another already-written Fortran program. How can I output binary data from C that is readable by Fortran? I need to work with binary data because of the large dataset (considering speed and disk capacity) that we are working with. Thanks for any insight or links that you all can provide :-)

I've looked into using the C-Fortran interface to pass the data to a Fortran routine that would write the binary file, but I couldn't figure out how to pass a file pointer from C to Fortran. If anyone has any ideas on this route, it would also be helpful. Thanks so much.
 
Old 02-07-2005, 04:14 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
C (as its unix-offspring suggests) knows nothing about
binary data ... you just output a stream of characters to
a file - done. There is no distinction between ASCII and
Binary from the OS or the programming languages per-
spective ... if you're using DOS/WinDOHs ... read the
compiler manuals ;)


Cheers,
Tink
 
Old 02-07-2005, 06:28 PM   #3
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
Ok, that's all fine and good. But, what I want to know is how to structure a C 'fwrite' or 'fput' or whatever that will, for example, put integer data in binary (as in non-text) format that Fortran code can handle with a 'read'.

The difference between binary and text output is 'fwrite' and 'fprintf'.

I'm using the gcc and g77 compilers. I can read the man (and have read up quite a bit), but I need a link to a site that discusses this question in particular. Thanks.

edit: my terminological use of stream may have be incorrect. i meant to say that i have an array of numerical data (integers and reals), not characters. i'm not a programmer (just an engineer who hacks together whatever works), so i didn't know that stream refers exclusively to characters.

Last edited by zero79; 02-07-2005 at 06:40 PM.
 
Old 02-07-2005, 06:35 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I don't understand what your question is. Is the man page unclear?
Code:
itsme@dreams:~/C$ cat bindata.c
#include <stdio.h>

int main(void)
{
  int num = 86;
  FILE *fp;

  fp = fopen("slarf", "w");
  fwrite(&num, sizeof(int), 1, fp);
  fclose(fp);

  return 0;
}
itsme@dreams:~/C$ ./bindata
itsme@dreams:~/C$ ls -l slarf
-rw-r--r--    1 itsme    users           4 Feb  7 16:35 slarf
itsme@dreams:~/C$ hexdump slarf
0000000 0056 0000
0000004
itsme@dreams:~/C$

Last edited by itsme86; 02-07-2005 at 06:36 PM.
 
Old 02-07-2005, 06:45 PM   #5
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
i can write the data to the file via c, i just can't retrieve it via a fortran routine. give me a minute to recreate what i have at work.
 
Old 02-07-2005, 06:54 PM   #6
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
basically my Fortran code doesn't want to properly read the binary file as can be seen below.

Code:
zero79@glasstux:~/test$ gcc -o writedata writedata.c
zero79@glasstux:~/test$ ./writedata
zero79@glasstux:~/test$ hexdump slarf
0000000 0056 0000
0000004
zero79@glasstux:~/test$ cat writedata.c
#include <stdio.h>

int main(void)
{
  int num = 86;
  FILE *fp;

  fp = fopen("slarf", "w");
  fwrite(&num, sizeof(int), 1, fp);
  fclose(fp);

  return 0;
}
zero79@glasstux:~/test$ g77 -o readdata readdata.f
zero79@glasstux:~/test$ ./readdata
do_us: end of file
apparent state: unit 10 named slarf
lately reading sequential unformatted external IO
Aborted
zero79@glasstux:~/test$ cat readdata.f
        program readdata
        implicit none
        integer n

        open(10, file="slarf", form="unformatted", access="direct")
        read(10) n
        close(10)

        write(*,*) n

        end program readdata
 
Old 02-07-2005, 07:47 PM   #7
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
ok, it appears that Fortran expects differently formatted data. i wrote a slightly different code:

Code:
zero79@glasstux:~/test$ cat readdata.f
        program readdata
        implicit none
        integer*4 num, n

        num = 86

        open(11, file="test", form="unformatted", access="direct")
        write(11) num
        close(11)

        open(10, file="test", form="unformatted", access="direct")
        read(10) n
        close(10)

        write(*,*) n

        end program readdata
zero79@glasstux:~/test$ g77 -o readdata readdata.f && ./readdata
 86
zero79@glasstux:~/test$ hexdump test
0000000 0004 0000 0056 0000 0004 0000
000000c
as can be seen, the fortran file write appends '0004 0000' between the records and closes the file with '000000c' instead of '0000004'. so what can i do from here?
 
Old 02-07-2005, 08:00 PM   #8
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
i think the intermediate record ('0004 0000') is an indicator that the following value is a 4 byte integer. if i declare my integers with 'integer*8', the intermediate record becomes '0008 0000' or with 'integer*2' it becomes '0002 0000'.

ok, got it, basically i just needed to create an array with those extra integers in the C code. now my slarf output is in the proper format that Fortran expects. the following works perfectly. data is generated in C and read via Fortran:

Code:
zero79@glasstux:~/test$ cat writedata.c
#include <stdio.h>

int main(void)
{
  int num[3] = {4, 86, 4};
  FILE *fp;

  fp = fopen("slarf", "w");
  fwrite(&num, sizeof(int), 3, fp);
  fclose(fp);

  return 0;
}
zero79@glasstux:~/test$ cat readdata.f
        program readdata
        implicit none
        integer*4 num, n

        open(10, file="slarf", form="unformatted", access="direct")
        read(10) n
        close(10)

        write(*,*) n

        end program readdata
zero79@glasstux:~/test$ gcc -o writedata writedata.c && ./writedata
zero79@glasstux:~/test$ hexdump slarf
0000000 0004 0000 0056 0000 0004 0000
000000c
zero79@glasstux:~/test$ g77 -o readdata readdata.f && ./readdata
 86
Thanks for all your help! I really mean it; I wouldn't have figured this out if you hadn't thrown out that 'hexdump' routine. Nice bit of trickery. Good to know. Anyway, thanks! Peace

Last edited by zero79; 02-07-2005 at 08:04 PM.
 
Old 02-07-2005, 08:51 PM   #9
initialdrifteg6
Member
 
Registered: Sep 2004
Location: France, Kentucky
Distribution: debian
Posts: 173

Rep: Reputation: 38
dude... what are you trying to do in c? and why didn't you call and ask me first?!? also... if you need a super compiler... apt-get moo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran: compressing output files on the fly zero79 Programming 1 03-23-2005 06:39 PM
how to redirect stdout [binary stream] to multiple processes vtaminh Linux - General 2 08-19-2004 01:05 PM
mplayer to output stream eantoranz Linux - Software 0 05-17-2004 02:47 PM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM
Need help defining Fortran 77 input/output streams using g77! Gethyn Programming 1 08-14-2003 12:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration