Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. structure equivalant in java

structure equivalant in java

Scheduled Pinned Locked Moved Java
javatutorial
20 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dave Doknjas

    It's not that bad - C/C++ structs and classes are virtually identical - convert these to classes in Java and make adjustments if you're placing them on the stack in C++ - there is no automatic copy construction or assignment in Java:

    public class _somename
    {
    public int somenuml;
    public int somenum2;
    public int somenum3;
    public String databuffer1 = new String(new char[100]);
    // very long struct
    }

    public static void test()
    {
    // initialize struct
    _somename mystruct = new _somename();
    mystruct.somename = 1;
    }

    David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

    T Offline
    T Offline
    trioum
    wrote on last edited by
    #7

    still not able to write in the file

    Trioum

    L 1 Reply Last reply
    0
    • T trioum

      still not able to write in the file

      Trioum

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #8

      trioum wrote:

      still not able to write in the file

      What does this mean? Please give a proper explanation of your problem so poeple can try and help you.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      T 1 Reply Last reply
      0
      • L Lost User

        trioum wrote:

        still not able to write in the file

        What does this mean? Please give a proper explanation of your problem so poeple can try and help you.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        T Offline
        T Offline
        trioum
        wrote on last edited by
        #9

        i want to write this object field values in file

        Trioum

        L N 2 Replies Last reply
        0
        • T trioum

          i want to write this object field values in file

          Trioum

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #10

          Saying "I want to do ..." is neither a question nor an explanation; please try and be specific about your problem.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          1 Reply Last reply
          0
          • T trioum

            i want to write this object field values in file

            Trioum

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #11

            Make your class serialisable and write it to a binary buffer. Sorted.


            Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

            T 1 Reply Last reply
            0
            • N Nagy Vilmos

              Make your class serialisable and write it to a binary buffer. Sorted.


              Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

              T Offline
              T Offline
              trioum
              wrote on last edited by
              #12

              can you send me the piece of code for writing class content to the file

              Trioum

              T N 2 Replies Last reply
              0
              • T trioum

                can you send me the piece of code for writing class content to the file

                Trioum

                T Offline
                T Offline
                TorstenH
                wrote on last edited by
                #13

                what kind of file? do you want to have an output to some textfile or did you get stuck on how to assume values to the fields? regards Torsten

                I never finish anyth...

                1 Reply Last reply
                0
                • T trioum

                  can you send me the piece of code for writing class content to the file

                  Trioum

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #14

                  trioum wrote:

                  can you send me the piece of code for writing class content to the file

                  0. Define the class as implements Serializable:

                  class Foo implements Serializable
                  {
                  private int bar;
                  // stuff

                  // other stuff
                  }

                  1. Write it to a binary buffer:

                  Foo foo = new Foo(42);

                  try
                  {
                  java.io.ObjectOutputStream os =
                  new java.io.ObjectOutputStream(
                  new java.io.FileOutputStream("foobar.dat"));
                  os.writeObject(foo);
                  } catch (java.io.IOException e)
                  {
                  e.printStackTrace();
                  } finaly
                  {
                  os.close();
                  }

                  That's hard how?


                  Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                  D T 2 Replies Last reply
                  0
                  • N Nagy Vilmos

                    trioum wrote:

                    can you send me the piece of code for writing class content to the file

                    0. Define the class as implements Serializable:

                    class Foo implements Serializable
                    {
                    private int bar;
                    // stuff

                    // other stuff
                    }

                    1. Write it to a binary buffer:

                    Foo foo = new Foo(42);

                    try
                    {
                    java.io.ObjectOutputStream os =
                    new java.io.ObjectOutputStream(
                    new java.io.FileOutputStream("foobar.dat"));
                    os.writeObject(foo);
                    } catch (java.io.IOException e)
                    {
                    e.printStackTrace();
                    } finaly
                    {
                    os.close();
                    }

                    That's hard how?


                    Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #15

                    That won't compile. os is out of scope in the finally block.

                    T 1 Reply Last reply
                    0
                    • D David Skelly

                      That won't compile. os is out of scope in the finally block.

                      T Offline
                      T Offline
                      trioum
                      wrote on last edited by
                      #16

                      wont compile

                      Trioum

                      1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        trioum wrote:

                        can you send me the piece of code for writing class content to the file

                        0. Define the class as implements Serializable:

                        class Foo implements Serializable
                        {
                        private int bar;
                        // stuff

                        // other stuff
                        }

                        1. Write it to a binary buffer:

                        Foo foo = new Foo(42);

                        try
                        {
                        java.io.ObjectOutputStream os =
                        new java.io.ObjectOutputStream(
                        new java.io.FileOutputStream("foobar.dat"));
                        os.writeObject(foo);
                        } catch (java.io.IOException e)
                        {
                        e.printStackTrace();
                        } finaly
                        {
                        os.close();
                        }

                        That's hard how?


                        Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                        T Offline
                        T Offline
                        trioum
                        wrote on last edited by
                        #17

                        there is difference i getting when writing using c and java following lines shows the difference when I use the c code struct _Foo { int one; int two; char buffer[50]; } ; struct _Foo foo; memset(&foo,'0/',sizeof(_Foo)); foo.one = 2; foo.two = 3; strcpy(foo.buffer,"my name is trioum"); FILE *fp; fp = fopen("cfile.txt","wb"); if(fp) { fwrite(&foo,sizeof(struct _Foo),1,fp); fclose(fp); } result in the file I get   my name is trioum ////////////////////////////////// but when I write the same code in java public static class _Foo implements Serializable { public int one; public int two; public String buffer = new String(new char[50]); } ObjectOutputStream outputStream = null; try { outputStream = new ObjectOutputStream(new FileOutputStream("javafile.txt")); _Foo Foo = new _Foo(); Foo.one = 2; Foo.two = 3; Foo.buffer = "my name is trioum"; outputStream.writeObject(Foo); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } I get the result in javafile.txt ¬í sr Main$_FooDpÕGT¯_ I oneI twoL buffert Ljava/lang/String;xp  t my name is trioum But I want the result as seen in cfile.txt by c code How can I solve the problem

                        Trioum

                        D 1 Reply Last reply
                        0
                        • T trioum

                          there is difference i getting when writing using c and java following lines shows the difference when I use the c code struct _Foo { int one; int two; char buffer[50]; } ; struct _Foo foo; memset(&foo,'0/',sizeof(_Foo)); foo.one = 2; foo.two = 3; strcpy(foo.buffer,"my name is trioum"); FILE *fp; fp = fopen("cfile.txt","wb"); if(fp) { fwrite(&foo,sizeof(struct _Foo),1,fp); fclose(fp); } result in the file I get   my name is trioum ////////////////////////////////// but when I write the same code in java public static class _Foo implements Serializable { public int one; public int two; public String buffer = new String(new char[50]); } ObjectOutputStream outputStream = null; try { outputStream = new ObjectOutputStream(new FileOutputStream("javafile.txt")); _Foo Foo = new _Foo(); Foo.one = 2; Foo.two = 3; Foo.buffer = "my name is trioum"; outputStream.writeObject(Foo); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } I get the result in javafile.txt ¬í sr Main$_FooDpÕGT¯_ I oneI twoL buffert Ljava/lang/String;xp  t my name is trioum But I want the result as seen in cfile.txt by c code How can I solve the problem

                          Trioum

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #18

                          That's because you are serializing the whole Foo object, which is not what you want. I'm not going to write the whole thing for you but I think I would make Foo look something like this:

                          public class Foo {

                          public char one;
                          public char two;
                          public String buffer;
                          ... etc ...

                          public String toExternalForm() {
                          StringBuilder sb = new StringBuilder();
                          sb.append(one);
                          sb.append(two);
                          sb.append(buffer);
                          ... etc ...
                          return sb.toString();
                          }

                          }

                          Then to use it:

                          foo.one = 2;
                          foo.two = 3;
                          foo.buffer = "something";
                          bw = new BufferedWriter(...etc...);
                          bw.write(foo.toExternalForm());
                          bw.flush();

                          That's basically it, you can look up BufferedWriter and whatever else you need and you might want to be a bit more intelligent about handling nulls. (I am using char instead of int in the class Foo because if we use int then StringBuilder would give us 23something which is not what you want, if I have understood correctly.)

                          T 1 Reply Last reply
                          0
                          • D David Skelly

                            That's because you are serializing the whole Foo object, which is not what you want. I'm not going to write the whole thing for you but I think I would make Foo look something like this:

                            public class Foo {

                            public char one;
                            public char two;
                            public String buffer;
                            ... etc ...

                            public String toExternalForm() {
                            StringBuilder sb = new StringBuilder();
                            sb.append(one);
                            sb.append(two);
                            sb.append(buffer);
                            ... etc ...
                            return sb.toString();
                            }

                            }

                            Then to use it:

                            foo.one = 2;
                            foo.two = 3;
                            foo.buffer = "something";
                            bw = new BufferedWriter(...etc...);
                            bw.write(foo.toExternalForm());
                            bw.flush();

                            That's basically it, you can look up BufferedWriter and whatever else you need and you might want to be a bit more intelligent about handling nulls. (I am using char instead of int in the class Foo because if we use int then StringBuilder would give us 23something which is not what you want, if I have understood correctly.)

                            T Offline
                            T Offline
                            trioum
                            wrote on last edited by
                            #19

                            check it again it is producing wrong result

                            Trioum

                            1 Reply Last reply
                            0
                            • T trioum

                              is there any C structure equivalant in java how to write following struct { int data char buffer[100]; }; equivalent in java

                              Trioum

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #20

                              Why not use a class?

                              class xyz
                              {
                              public int data;
                              public string buffer;
                              };

                              "One man's wage rise is another man's price increase." - Harold Wilson

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              "Man who follows car will be exhausted." - Confucius

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups