J
Jamie
I have a file that was written using Java and the file has unicode
strings. What is the best way to deal with these in C? The file
definition reads:
Data Field Description
CHAR[32] File identifier (64 bytes corresponding to Unicode character
string padded with '0' Unicode characters.
CHAR[16] File format version (32 bytes corresponding to Unicode
character string "x.y.z" where x, y, z are integers
corresponding to major, minor and revision
number of the File format version) padded with '0' Unicode
characters.
INTEGER Main file header length [bytes].
....
The data field defitions are from Java primitives:
CHAR Unicode character. 16-bit Unicode character.
INTEGER Signed integer number. 32-bit two's complement signed integer.
There is absolutely no need for these strings to be in unicode format and
I am at a loss as to how to convert them to a standard C character array.
Moreover, in the example below, I seem to be out in the byte count as my
integer is garbage. Any ideas would be greatly appreciated.
#include <stdlib.h>
#include <stdio.h>
#define ARGC_FAILURE 100
#define OPEN_FAILURE 101
#define CLOSE_FAILURE 102
int main(int argc, char *argv[])
{
FILE *fp;
long n;
char d_id[64];
char d_version[32];
int d_hdrlen;
if (argc !=2)
{
printf("Usage: read_adf filename\n");
return ARGC_FAILURE;
}
// Open the file
if ( (fp = fopen(argv[1], "r")) == NULL)
{
printf("%s: Error opening %s", argv[0], argv[1]);
return OPEN_FAILURE;
}
// Read the contents
n = fread(d_id, sizeof(d_id), 1, fp);
n = fread(d_version, sizeof(d_version), 1, fp);
n = fread(&d_hdrlen, sizeof(d_hdrlen), 1, fp);
// Display the contents
printf(" ID: %s\n", d_id);
printf(" VER: %s\n", d_version);
printf(" HDR Length: %d\n", d_hdrlen);
// Close the file
if (fclose(fp) == EOF)
{
printf("%s: Error closing %s", argv[0], argv[1]);
return CLOSE_FAILURE;
}
return 0;
}
strings. What is the best way to deal with these in C? The file
definition reads:
Data Field Description
CHAR[32] File identifier (64 bytes corresponding to Unicode character
string padded with '0' Unicode characters.
CHAR[16] File format version (32 bytes corresponding to Unicode
character string "x.y.z" where x, y, z are integers
corresponding to major, minor and revision
number of the File format version) padded with '0' Unicode
characters.
INTEGER Main file header length [bytes].
....
The data field defitions are from Java primitives:
CHAR Unicode character. 16-bit Unicode character.
INTEGER Signed integer number. 32-bit two's complement signed integer.
There is absolutely no need for these strings to be in unicode format and
I am at a loss as to how to convert them to a standard C character array.
Moreover, in the example below, I seem to be out in the byte count as my
integer is garbage. Any ideas would be greatly appreciated.
#include <stdlib.h>
#include <stdio.h>
#define ARGC_FAILURE 100
#define OPEN_FAILURE 101
#define CLOSE_FAILURE 102
int main(int argc, char *argv[])
{
FILE *fp;
long n;
char d_id[64];
char d_version[32];
int d_hdrlen;
if (argc !=2)
{
printf("Usage: read_adf filename\n");
return ARGC_FAILURE;
}
// Open the file
if ( (fp = fopen(argv[1], "r")) == NULL)
{
printf("%s: Error opening %s", argv[0], argv[1]);
return OPEN_FAILURE;
}
// Read the contents
n = fread(d_id, sizeof(d_id), 1, fp);
n = fread(d_version, sizeof(d_version), 1, fp);
n = fread(&d_hdrlen, sizeof(d_hdrlen), 1, fp);
// Display the contents
printf(" ID: %s\n", d_id);
printf(" VER: %s\n", d_version);
printf(" HDR Length: %d\n", d_hdrlen);
// Close the file
if (fclose(fp) == EOF)
{
printf("%s: Error closing %s", argv[0], argv[1]);
return CLOSE_FAILURE;
}
return 0;
}