The C code i need to convert into Java is:
typedef struct
{
short ih;
....
} lsettings;
int ldc_read_parameters(char *param_fnm, lsettings settings, short *image_height)
{
FILE *fp_param;
char line[256];
fp_param = fopen(param_fnm, "r");
if (fp_param)
{
do fgets(line, 256, fp_param);
while (line[0] == '#');
sscanf(line, "%hd", &settings.ih);
*image_height = settings.ih;
}
}
The Java code i have written is:
class lsettings
{
public short ih;
.....
}
int ldc_read_parameters(String param_fnm, lsettings settings, short image_height[])
{
Scanner fp_param;
String line;
fp_param = new Scanner(new BufferedReader(new FileReader(param_fnm)));
if (fp_param.hasNext())
{
do
{
line=fp_param.nextLine();
} while (line.charAt(0) == '#');
Scanner s=new Scanner(line);
settings.ih=s.nextShort();
image_height[0] = settings.ih;
}
}
Have i done the conversion correctly or something is wrong here. I am not sure about the sscanf function. Please help. Thanks.
man sscanf
The scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer stream, andsscanf() reads its input from the character string pointed to by str.
h Indicates that the conversion will be one of d, i, o, u, x, X, or n and the next pointer is a pointer to a short int orunsigned short int (rather than int).
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextShort()
Both sscanf and nextShort converts next value to short (in c version you have unsigned short). If you want to have the same precision – change short to int. short in java in always signed.