2012-03-21 6 views
1

내가 자바로 변환해야하는 C 코드 sscanf에서 것은 :자바 코드로 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; 
    } 
} 

내가 쓴 자바 코드는 다음과 같습니다

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 기능에 대해 잘 모르겠습니다. 도와주세요. 감사.

+1

그냥 사용해 보는 게 어떨까요? – Joey

+0

시도 할 때 어떤 오류가 있습니까? 디버거에서 코드를 단계별로 실행 해 보았습니까? –

답변

1

사람 sscanf를

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 짧은 옆에 값을 변환 (C 버전에서 서명되지 않은 짧은 있음). 같은 정밀도를 원하면 - int로 short로 변경하십시오. 항상 서명 된 자바의 짧은 것.

+0

IMHO'% hd'는 서명 된 short int를 참조합니다. 아니면 내가 놓친 게 있니? –

+0

'또는 unsigned short int' -'sscanf (line, "% hd", & variable); 입력 변수가 signed 또는 unsigned short인지 여부에 따라 다릅니다. 자바 짧은 있음 항상 서명 된 (문자 서명되지 않은,하지만 스캐너 해석 방법을 모르겠다) –

+0

'구조체 lsettings' 그것은'short'로 정의되었습니다 –