我需要转换为Java的C代码是:
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;
}
}
我写的Java代码是:
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;
}
}
我是否正确完成了转换,或者此处出现了问题。我不确定sscanf功能。请帮忙。感谢。
答案 0 :(得分:1)
The scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer stream, and
sscanf() 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 or
unsigned short int (rather than int).
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextShort()
sscanf和nextShort都将下一个值转换为short(在c版本中你有unsigned short)。如果你想拥有相同的精度 - 将short更改为int。在总是签名的java中简短。