|
Torsten suggests a bean which may well be a good answer. However, it is difficult to suggest a better alternative without knowing some more about what you are trying to achieve.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Actually I have I programme which write in file with some fixed header in files . which is in form of structure like
struct _somename
{
int somenuml;
int somenum2;
int somenum3;
char databuffer1[100];
// very long struct
};
// initialize struct
struct _somename mystruct;
mystruct.somename = 1;
//------
// ans finaly open some file and write to file
fwrite(&mystruct,1,sizeof(struct _somename));
Now I want to convert same c programme in java due to some technical reason
Trioum
|
|
|
|
|
trioum wrote: Now I want to convert same c programme in java due to some technical reason
Then you have some work to do.
The original implementation made assumptions about the layout of the data in memory. Or maybe they just completely ignored it because they didn't understand it at all.
And you must determine what that layout is and then match it exactly.
For starters you will need to know the size of 'int' and whether it is big endian or little endian. You need to determine the exact binary format that the 'int' takes in memory so you can determine how it would be written to a file.
And you must do the same for every other data item in the struct.
Finally you must account for the fact that the struct might be padded at the end. So there might be one or more random bytes which are not explicitly represented in the structure.
|
|
|
|
|
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]);
}
public static void test()
{
_somename mystruct = new _somename();
mystruct.somename = 1;
}
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
still not able to write in the file
Trioum
|
|
|
|
|
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
|
|
|
|
|
i want to write this object field values in file
Trioum
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
can you send me the piece of code for writing class content to the file
Trioum
|
|
|
|
|
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...
|
|
|
|
|
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;
}
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
|
|
|
|
|
That won't compile. os is out of scope in the finally block.
|
|
|
|
|
|
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
|
|
|
|
|
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.)
|
|
|
|
|
check it again it is producing wrong result
Trioum
|
|
|
|
|
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
|
|
|
|
|
I'm having a problem writing the switch part for my alarm clock...I'm just not sure how to write the code. Thank you for all of you help
• The alarm clock can be constructed using the classes in the clock example, namely ClockDisplay and NumberDisplay. Just as a ClockDisplay object can be considered as two NumberDisplay objects, An AlarmClock object can be considered a ClockDisplay object
• an alarm that can be set to go off at a particular time. The alarm has a switch that allows it to be set for the alarm to go off at the appropriate time. If the switch is not set the alarm does not ring at the appropriate time.
• Note that the ClockDisplay class only returns the time as a String, so you will need to make a comparison of two String objects to determine when to ring the alarm. The == operator is not a valid way to do this. It does not compare Strings the way we would expect. But the String class contains a method that is appropriate. Its signature is
boolean equals (String)
This method returns true when the string executing the method is equal to (has exactly that same characters in the same order) the string that is passed as a parameter, and false otherwise. For example
"hello".equals("hello") returns true
"hello".equals("goodbye") returns false
• It is also possible to build the alarm from other classes. Consider the characteristics:
o it contains a time that the alarm is set to
o that time can be changed
o it contains a switch to turn the alarm setting on or off.
|
|
|
|
|
If you are not sure how to write your code then you may like to take a look here[^] for some useful suggestions. The description above reads very much like a college assignment so you may also want to discuss your problem with your instructor.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
...that's not an typical beginners-app. You must have been in class before and have done some other apps.
The only help i want to give is this:
- make a plan. Don't just try to type code. Make a plan:
- what's supposed to happen? When will it happen (task B starts when task A is ready, ...)?
- which recources will you need (pics, special jar-Files)?
- which parts can i just throw in and which do i need to take care of?
regards,
Torsten
I never finish anyth...
|
|
|
|
|
Hi
Please excuse me if my question sound trivial as I am a novice:
Why do we need the key word 'this'? What does it do?
|
|
|
|
|
You don't need it, but it refers to member variables or methods of a class:
class Foo
{
private int bar;
void int setBar(int bar)
{
this.bar = bar;
}
}
this.bar is the memeber variable, bar is the message attribute.
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
|
|
|
|
|
Nagy Vilmos wrote: You don't need it
in fact, you may need this , e.g. to pass "yourself" to another object or class method, say when you want to subscribe to an event or a service.
FWIW: I do like VB.NET's Me keyword, it seems more appropriate than this .
|
|
|
|
|
The keyword 'this' refers to the current class you are in.
If you write 'this.' while in a class, you can access all members
of that class by IntelliSense.
'this' just means 'the current class'. Sounds logical, yes?
public class MyClass
{
private int age = 18;
public MyClass
{
this.age = 20;
}
}
However, 'this' can be omitted.
'this.age' is the same as 'age'
while in the same class.
Regards,
CSTreval
|
|
|
|