Re: JNI Problem =?iso-8859-1?Q?=A1=AA=A1=AA?= File Write Fail

Discussion in 'Java' started by Gordon Beaton, Sep 20, 2003.

  1. On Sat, 20 Sep 2003 13:43:16 +0800, StevenZhao wrote:
    > I create data in Java programm and pass them to a C programm named
    > WriteFile.c . I printed those data in C and get right results.
    > However, when I use writeToFile() in C to write those data to a
    > binary file, I failed, the file was created but no data in it.


    There are numerous problems with the code you've posted:

    - you invoke fopen() in the argument list to fwrite(), which leaves
    you no opportunity to check for success before proceeding. Open the
    file first, save the FILE*, and check that it is not NULL before
    continuing.

    - why do you open the file once for each record? Move fopen() before
    the loop, write all your data, then...

    - *close* the file when you've finished writing. Not closing the file
    could be the reason your data doesn't appear. It is also the source
    of a memory and descriptor leak. To close it, you need to have saved
    the handle returned by fopen() (see my first comment).

    - in the native method you pass variables "file", "cl" and "cled" to
    writeToFile(), but these variables are uninitialized and probably
    contain junk. This could cause erratic or undefined behaviour.

    - remove the three calls to ReleaseStringUTFChars() unless you've also
    used GetStringUTFChars(). This error could cause your program to
    crash, depending on what the 3 uninitialized variables contain.

    - if you in fact *are* using GetStringUTFChars(), note that it returns
    "const char *" not "const jbyte *". On the other hand, if this isn't
    your real code then I am wasting my time helping you debug something
    that may contain a completely different set of errors. Always post
    real code when asking for help.

    Finally, don't multipost! If you think it's necessary to post the same
    question to several groups (and it usually isn't), at least learn to
    *crosspost*.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 20, 2003
    #1
    1. Advertising

  2. Gordon Beaton

    StevenZhao Guest

    Re: JNI Problem ¡ª¡ª File Write Fail

    Gordon, Thanks for your help.
    I don't close the file, that results in incorrect file.Now, it is ok.
    Your advice are very helpful to me. My C programm need more
    refactoring,yeah? Actually, I seldom write C programm and not familiar with
    that. This time, I programm a data creator to simulate a device which can
    produce we call CDR file.
    When I used Java to create that kinds of file, the C programm will meet
    error to parse it.you know, the file formats writtend by Java and C are
    different. So, I decided to assign the task¡ª¡ªcreating file¡ªto a C
    programm named WriteFile.c.
    It's my first time to use JNI, I am reading a book named ¡¶Java Native
    Interface:programmer's Guide and Specification¡·, do you have some material
    to suggest to me ?

    Thank you again, I help me a lot!
    By the way, you refered crosspost. I am using OE, does it support this
    function? Could you introduce crosspost to me or give me some web info about
    it?


    "Gordon Beaton" <> дÈëÓʼþ news:3f6c0384$...
    >
    > On Sat, 20 Sep 2003 13:43:16 +0800, StevenZhao wrote:
    > > I create data in Java programm and pass them to a C programm named
    > > WriteFile.c . I printed those data in C and get right results.
    > > However, when I use writeToFile() in C to write those data to a
    > > binary file, I failed, the file was created but no data in it.

    >
    > There are numerous problems with the code you've posted:
    >
    > - you invoke fopen() in the argument list to fwrite(), which leaves
    > you no opportunity to check for success before proceeding. Open the
    > file first, save the FILE*, and check that it is not NULL before
    > continuing.
    >
    > - why do you open the file once for each record? Move fopen() before
    > the loop, write all your data, then...
    >
    > - *close* the file when you've finished writing. Not closing the file
    > could be the reason your data doesn't appear. It is also the source
    > of a memory and descriptor leak. To close it, you need to have saved
    > the handle returned by fopen() (see my first comment).
    >
    > - in the native method you pass variables "file", "cl" and "cled" to
    > writeToFile(), but these variables are uninitialized and probably
    > contain junk. This could cause erratic or undefined behaviour.
    >
    > - remove the three calls to ReleaseStringUTFChars() unless you've also
    > used GetStringUTFChars(). This error could cause your program to
    > crash, depending on what the 3 uninitialized variables contain.
    >
    > - if you in fact *are* using GetStringUTFChars(), note that it returns
    > "const char *" not "const jbyte *". On the other hand, if this isn't
    > your real code then I am wasting my time helping you debug something
    > that may contain a completely different set of errors. Always post
    > real code when asking for help.
    >
    > Finally, don't multipost! If you think it's necessary to post the same
    > question to several groups (and it usually isn't), at least learn to
    > *crosspost*.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    StevenZhao, Sep 20, 2003
    #2
    1. Advertising

  3. On Sat, 20 Sep 2003 19:35:32 +0800, StevenZhao wrote:
    > When I used Java to create that kinds of file, the C programm will
    > meet error to parse it.you know, the file formats writtend by Java
    > and C are different. So, I decided to assign the task¡ª¡ªcreating
    > file¡ªto a C programm named WriteFile.c.


    It's rarely a good idea to base your storage format on the exact
    contents of the C struct as you've done, because the format is
    dependent on things like byte order and word size of your platform, as
    well as the order of the struct fields and any padding your C compiler
    might choose to add.

    To make your format portable you should specify a byte order and size
    for each field, then write the fields one at a time according to that.
    Whether your format is text-based or binary makes no difference, both
    are common and have their own advantages and disadvantages.

    You *can* use standard Java libraries to write a file that C can
    parse, but you need to design the format with that in mind.

    > It's my first time to use JNI, I am reading a book named ¡¶Java
    > Native Interface:programmer's Guide and Specification¡·, do you have
    > some material to suggest to me ?


    My book (now a few years old) is Rob Gordon: Java Native Interface. I
    think both are ok. There is also som information online:

    http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html
    http://java.sun.com/docs/books/tutorial/native1.1/index.html

    There are also a handful of articles and other webpages on JNI (try
    Google) but I haven't seen anything that adds much to the above sites.

    > By the way, you refered crosspost. I am using OE, does it support
    > this function? Could you introduce crosspost to me or give me some
    > web info about it?


    I don't use OE or anything else on windows so I have no idea, but I
    think all newsreaders support it. You probably just need to tell it
    both newsgroups when you post, probably by typing them together
    separated by a comma.

    I crossposted my answer to your original post by specifying both
    newsgroups. Your reply and this one are automatically crossposted as a
    result, so now it doesn't matter which of the two groups you are
    reading or replying to.

    Read more here:
    http://www.dickalba.demon.co.uk/usenet/guide/faq_cros.html
    http://www.uwasa.fi/~ts/http/crospost.html

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SHIRE
    Replies:
    4
    Views:
    2,731
    SHIRE
    Feb 4, 2004
  2. Franck DARRAS
    Replies:
    12
    Views:
    647
    Jim Higson
    Aug 23, 2004
  3. Harobed
    Replies:
    1
    Views:
    371
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Sep 1, 2003
  4. Harobed
    Replies:
    0
    Views:
    339
    Harobed
    Sep 1, 2003
  5. Peter Jacobi
    Replies:
    13
    Views:
    850
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Aug 3, 2004
Loading...

Share This Page