Use fopen in binary mode (Windows support)

This commit is contained in:
Shahram Najm 2019-02-28 12:19:28 +00:00
parent 5bac99bc73
commit 974999976e
8 changed files with 76 additions and 73 deletions

View File

@ -90,7 +90,7 @@ int main(int argc, char** argv)
/* Encode the keys back in the data section */
CODES_CHECK(codes_set_long(h, "pack", 1), 0);
fout = fopen(outfilename, "w");
fout = fopen(outfilename, "wb");
if (!fout) {
fprintf(stderr, "Failed to open (create) output file.\n");
return 1;

View File

@ -29,7 +29,7 @@ int main(int argc,char* argv[])
const char* key_name = "#5#brightnessTemperature";
const double tolerance = 1e-6;
in=fopen(infile,"r");
in=fopen(infile,"rb");
assert(in);
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)

View File

@ -46,7 +46,7 @@ int main(int argc,char* argv[])
}
assert(input_filename);
f = fopen(input_filename, "r");
f = fopen(input_filename, "rb");
assert(f);
h = codes_handle_new_from_file(NULL, f, PRODUCT_BUFR, &err);
assert(h);

View File

@ -38,13 +38,13 @@ int main(int argc, char* argv[]) {
infile1=argv[1];
infile2=argv[2];
f1=fopen(infile1,"r");
f1=fopen(infile1,"rb");
if (!f1) {
perror(infile1);
exit(1);
}
f2=fopen(infile2,"r");
f2=fopen(infile2,"rb");
if (!f2) {
perror(infile2);
exit(1);

View File

@ -36,7 +36,7 @@ int main(int argc, char** argv)
if (argc<2) usage(argv[0]);
filename=argv[1];
in = fopen(filename,"r");
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",filename);
return 1;

View File

@ -36,13 +36,13 @@ int main(int argc, char** argv)
if (argc<2) usage(argv[0]);
in = fopen(argv[1],"r");
in = fopen(argv[1],"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",argv[1]);
return 1;
}
/* create new handle from a message in a file*/
/* create new handle */
err = 0;
h = grib_handle_new_from_file(0,in,&err);
if (h == NULL) {

View File

@ -51,7 +51,7 @@ static void test_reduced_gg(int remove_local_def, int edition, const char* packi
grib_util_packing_spec packing_spec={0,};
assert(input_filename);
in = fopen(input_filename,"r"); assert(in);
in = fopen(input_filename,"rb"); assert(in);
handle = grib_handle_new_from_file(0,in,&err); assert(handle);
CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen),0);
@ -60,7 +60,7 @@ static void test_reduced_gg(int remove_local_def, int edition, const char* packi
return;
}
assert(output_filename);
out = fopen(output_filename,"w"); assert(out);
out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(grib_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen);
@ -155,7 +155,7 @@ static void test_regular_ll(int remove_local_def, int edition, const char* packi
grib_util_packing_spec packing_spec={0,};
assert(input_filename);
in = fopen(input_filename,"r"); assert(in);
in = fopen(input_filename,"rb"); assert(in);
handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle);
CODES_CHECK(codes_get_long(handle, "edition", &input_edition), 0);
@ -166,7 +166,7 @@ static void test_regular_ll(int remove_local_def, int edition, const char* packi
return;
}
assert(output_filename);
out = fopen(output_filename,"w"); assert(out);
out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(codes_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen);
@ -255,7 +255,7 @@ static void test_grid_complex_spatial_differencing(int remove_local_def, int edi
grib_util_grid_spec spec={0,};
grib_util_packing_spec packing_spec={0,};
in = fopen(input_filename,"r"); assert(in);
in = fopen(input_filename,"rb"); assert(in);
handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle);
CODES_CHECK(grib_get_string(handle, "packingType", gridType, &slen),0);
@ -263,7 +263,7 @@ static void test_grid_complex_spatial_differencing(int remove_local_def, int edi
grib_handle_delete(handle);
return;
}
out = fopen(output_filename,"w"); assert(out);
out = fopen(output_filename,"wb"); assert(out);
CODES_CHECK(codes_get_size(handle,"values",&inlen), 0);
values = (double*)malloc(sizeof(double)*inlen);

View File

@ -9,71 +9,74 @@
*/
#include "grib_api_internal.h"
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
exit(1);
static void usage (const char *prog)
{
printf ("usage: %s infile\n", prog);
exit (1);
}
static unsigned char buffer[50000000];
int main(int argc,char* argv[]) {
int main (int argc, char *argv[])
{
char *filename;
FILE *f;
grib_handle *h = NULL;
grib_context *c;
size_t size = 0;
int ret = 0;
size_t bufsize = sizeof (buffer);
long count, step, edition, totalLength;
char gridType[50], levelType[50], level[50], shortName[50];
size_t gridTypelen = sizeof (gridType);
size_t levelTypelen = sizeof (levelType);
size_t levellen = sizeof (level);
size_t shortNamelen = sizeof (shortName);
size_t len;
char* filename;
FILE* f;
grib_handle* h=NULL;
grib_context* c;
size_t size=0;
int ret=0;
size_t bufsize=sizeof(buffer);
long count,step,edition,totalLength;
char gridType[50],levelType[50],level[50],shortName[50];
size_t gridTypelen=sizeof(gridType);
size_t levelTypelen=sizeof(levelType);
size_t levellen=sizeof(level);
size_t shortNamelen=sizeof(shortName);
size_t len;
if (argc != 2)
usage (argv[0]);
filename = argv[1];
if (argc!=2) usage(argv[0]);
filename=argv[1];
f = fopen (filename, "rb");
if (!f) {
perror (filename);
exit (1);
}
c = grib_context_get_default ();
f=fopen(filename,"r");
if (!f) {
perror(filename);
exit(1);
}
c=grib_context_get_default();
size=bufsize;
count=1;
while ((ret=grib_read_any_from_file(c,f,buffer,&size))==GRIB_SUCCESS) {
if ((1)) {
h=grib_handle_new_from_message_copy(c,buffer,size);
if (!h) {
printf("unable to new from message\n");
exit(1);
} else {
grib_get_long(h,"edition",&edition);
grib_get_long(h,"step",&step);
grib_get_long(h,"totalLength",&totalLength);
len=gridTypelen;
grib_get_string(h,"gridType",gridType,&len);
len=levelTypelen;
GRIB_CHECK(grib_get_string(h,"levelType",levelType,&len),0);
len=levellen;
grib_get_string(h,"level",level,&len);
len=shortNamelen;
grib_get_string(h,"shortName",shortName,&len);
printf("- %3ld -\t ed=%ld\t size=%8ld totalLength=%8ld \t %s\t %s\t %s\t level=%s\t step=%ld\n",
count, edition, (long)size, totalLength, shortName, gridType, levelType, level, step);
grib_handle_delete(h);
}
} else {
printf("MESSAGE #%ld\n",count);
}
size=bufsize;
count++;
}
return 0;
size = bufsize;
count = 1;
while ((ret = grib_read_any_from_file (c, f, buffer, &size)) == GRIB_SUCCESS) {
if ((1)) {
h = grib_handle_new_from_message_copy (c, buffer, size);
if (!h) {
printf ("unable to new from message\n");
exit (1);
}
else {
grib_get_long (h, "edition", &edition);
grib_get_long (h, "step", &step);
grib_get_long (h, "totalLength", &totalLength);
len = gridTypelen;
grib_get_string (h, "gridType", gridType, &len);
len = levelTypelen;
GRIB_CHECK (grib_get_string (h, "levelType", levelType, &len), 0);
len = levellen;
grib_get_string (h, "level", level, &len);
len = shortNamelen;
grib_get_string (h, "shortName", shortName, &len);
printf ("- %3ld -\t ed=%ld\t size=%8ld totalLength=%8ld \t %s\t %s\t %s\t level=%s\t step=%ld\n",
count, edition, (long) size, totalLength, shortName, gridType, levelType, level, step);
grib_handle_delete (h);
}
}
else {
printf ("MESSAGE #%ld\n", count);
}
size = bufsize;
count++;
}
return 0;
}