convert binary file to txt file

W

Walter Roberson

Can I fullfill this task? Using fred and fwrite?

Yes, if you know the format of the binary file. Typically,
though, the output portion would be easier if you used
fprintf() or one of its close relatives rather than fwrite():
with fwrite() for the output, you will have to -somehow- do
all of the binary to text string formatting and output buffer
management; fprintf() is designed to do that kind of formatting
and buffer management.
 
W

Walter Roberson

xiao said:
haha, I made a mistake here, I mean fread....but it seems fread does
not working....

Are you opening the file with the 'b' modifier? If not then the
assumption will be that the file is text and binary characters
that -happen- to match the line terminators would be translated
upon reading.

If you are taking care to open with the 'b' modifier, then I suggest
you post a stand-alone code sample of something that "doesn't work",
explaining what you expect it to do and explaining what you see it
doing instead.
 
X

xiao

Are you opening the file with the 'b' modifier? If not then the
assumption will be that the file is text and binary characters
that -happen- to match the line terminators would be translated
upon reading.

If you are taking care to open with the 'b' modifier, then I suggest
you post a stand-alone code sample of something that "doesn't work",
explaining what you expect it to do and explaining what you see it
doing instead.

--
"If there were no falsehood in the world, there would be no
doubt; if there were no doubt, there would be no inquiry; if no
inquiry, no wisdom, no knowledge, no genius."
-- Walter Savage Landor

Here is my program: I just want to converted the input file to see
the content of it. But seems something wrong with opnning the file.


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_PATH_LENGTH 100000000

/* Define the data structure for the file header information */
typedef struct
{
short num;

} HEADER;

typedef struct
{

short x;

} DATA;

int main(void)
{

short xx,numn;
char disp[MAX_PATH_LENGTH];
int i;
FILE *in, *out;
HEADER nscrdh;
DATA nscrd;

/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","r");
out = fopen("newstats.txt","wt");

/* Read necessary header data into local variables & confirm file
format */
for (i=0; i<84; i++)
{
fread(&nscrdh,sizeof(short),1,in);
numn=nscrdh.num;
sprintf(disp, "%d \n", numn);
/* Write header, ray information into output file */
fputs(disp, out);
}
for (i=0; i <8960000; i++)
{
fread(&nscrd,1,sizeof(short),in);
xx= nscrd.x;
sprintf(disp, "%d \n", xx);
fputs(disp, out);
}
fclose(in);
fclose(out);

return 0;
}
 
K

Keith Thompson

The subject is "convert binary file to txt file". Please include the
full question in the body of the article. Not all newsreaders display
the subject header clearly enough to guarantee that it will be seen.

xiao said:
Can I fullfill this task? Using fred and fwrite?

Not unless you define the problem more precisely. What kind of
conversion do you want to do?

The answer will almost certainly be yes (though fwrite may not be the
best tool for writing the text file).
 
X

xiao

The subject is "convert binary file to txt file". Please include the
full question in the body of the article. Not all newsreaders display
the subject header clearly enough to guarantee that it will be seen.



Not unless you define the problem more precisely. What kind of
conversion do you want to do?

The answer will almost certainly be yes (though fwrite may not be the
best tool for writing the text file).

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

I just want to see the contents of the binary file (i mean the numbers
in it) :) The original file is ldTotalStats_8.dat and the new file
is newdata.txt.
 
K

Keith Thompson

Richard Heathfield said:
xiao said:

Here is my program: I just want to converted the input file to see
the content of it. But seems something wrong with opnning the file.

I'll give you six.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_PATH_LENGTH 100000000 [...]
char disp[MAX_PATH_LENGTH];

1) You're asking for an object 100,000,000 bytes in size. C only guarantees
you an object 32,767 bytes in size (65,535 if you have a C99
implementation, which you probably don't).

Yes, but that's not necessarily a fatal flaw in the program. If you
really need an object that big, then that's how to declare it. If the
system doesn't support objects that big, then it should reject your
program, which is just what you want.

However, on some systems you may have a better chance of successfully
allocating your huge object using malloc() rather than a file-scope
declaration. This also has the advantage of well-defined behavior if
the allocation fails.

But it's a good idea to read the file in smaller chunks if you can.

The name MAX_PATH_LENGTH implies that it's an upper bound for the
length of a file name (the term "path" is often used, on some systems,
to refer to a full file name, including the directory name). You're
using it as the size of a buffer to contain data read from a file. I
suggest you pick a more meaningful name.

Finally, you only store information in disp, your huge array, in two places:

sprintf(disp, "%d \n", numn);
...
sprintf(disp, "%d \n", xx);

In both cases, you're unlikely to write more than a dozen or so
characters to your buffer. 100 million seems like overkill.

[snip]
 
X

xiao

Keith said:
[...]
Finally, you only store information in disp, your huge array, in two places:
sprintf(disp, "%d \n", numn);
...
sprintf(disp, "%d \n", xx);
In both cases, you're unlikely to write more than a dozen or so
characters to your buffer. 100 million seems like overkill.

Even that many won't be enough if his system's `short' is
wider than 332,192,796 bits. Memory's cheap, but safety is
priceless.

Yup , I tried to change the 100 million to 100000 and it works....but
i am not sure if the result is right....Because i did not changed
antthing else.... Sign....
 
X

xiao

xiao said:

Here is my program: I just want to converted the input file to see
the content of it. But seems something wrong with opnning the file.

I'll give you six.


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_PATH_LENGTH 100000000
/* Define the data structure for the file header information */
typedef struct
{
short num;
} HEADER;
typedef struct
{
int main(void)
{
short xx,numn;
char disp[MAX_PATH_LENGTH];

1) You're asking for an object 100,000,000 bytes in size. C only guarantees
you an object 32,767 bytes in size (65,535 if you have a C99
implementation, which you probably don't).
int i;
FILE *in, *out;
HEADER nscrdh;
DATA nscrd;
/* Determine the file names to be read from and written to */
/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","r");

2) You don't check that this call succeeded. If it fails, the behaviour of
your program is undefined.

3) If you wanted to open this file in binary mode, you should specify "rb"
as the mode, not just "r".
out = fopen("newstats.txt","wt");

4) You don't check that this call succeeded. If it fails, the behaviour of
your program is undefined.

5) The fopen function has no "wt" mode. If you mean you want a text file,
specify "w".


/* Read necessary header data into local variables & confirm file
format */
for (i=0; i<84; i++)
{
fread(&nscrdh,sizeof(short),1,in);

6) Here, you show confusion about whether you're reading a HEADER (you're
passing a pointer to a HEADER object) or a short (you use sizeof(short) in
the call). Clearer: fread(&nscrdh, sizeof nscrdh, 1, in);

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


:)Thank you for your grading :) haha~
 
X

xiao

xiao said:
Keith Thompson wrote:
[...]
Finally, you only store information in disp, your huge array, in two places:
sprintf(disp, "%d \n", numn);
...
sprintf(disp, "%d \n", xx);
In both cases, you're unlikely to write more than a dozen or so
characters to your buffer. 100 million seems like overkill.
Even that many won't be enough if his system's `short' is
wider than 332,192,796 bits. Memory's cheap, but safety is
priceless.
Yup , I tried to change the 100 million to 100000 and it works....but
i am not sure if the result is right....Because i did not changed
antthing else.... Sign....

Xiao, Xiao, you're working too hard. Step back and think a moment:
What's the very next thing you do after each of the sprintf(disp,...)
calls? fputs(disp,out), that's what. And is the disp array used for
any other purpose? No, it's not: You don't actually need it for
anything except to hold some characters before writing them to the
output. Xiao, I suggest you crack open your C reference and look up
the fprintf() function; using it, you can get rid of disp altogether
and stop worrying about its size.

Similar remarks apply to the two struct types you define. Each
contains just one thing, a short, and as soon as you read a value
into the struct's short the very next thing you do is pluck it out
into a free-standing short variable. Why bother with the struct
types at all?

Pointless complications do not improve programs, unless they're
IOCCC entries.

haha .thank you~ i changed my mind here , If I define it like this:

short xx[800][800];


or


typedef struct
{
short x[800][800];
} NSC_RAY_DATA


how can I access the data in fread and fprintf?
I tried this:

fread(xx,sizeof(xx),14,in);
for(i=0; i<14 ;i++)
{ fprintf(out,"%hd", xx);}



and
fread(&nscrd,sizeof(nscrd),14,in);
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a]=nscrd.x[a];
}
}
fprintf(out,"%hd", xx);


And both of them remind me that :

warning: int format, pointer arg (arg 3)
 
R

Richard Bos

xiao said:
Can I fullfill this task?

"This" task? What is "this" task you speak of?

*Looks up*

Oh. Don't do that; put your complete _question_ in your post, and a
_summary_ in the subject.
Using fred and fwrite?

You can do it much simpler using frhed, if you know where to find it.

But yes, you can, and guess what? It's quite straightforward. You do it
the way you would think you do: open binary file for reading, open text
file for writing, read byte from binary file, write text representation
of byte to text file (easier done using fprintf() than fwrite(), btw),
repeat until you run out of bytes. Improvements involving reading more
than one byte at a time are left as an exercise for the homework
cheater.

Richard
 
N

Nick Keighley

please include the subject in the body of your text
Subject: convert binary file to txt file


what format is the binary file? What do you want the text to look
like.
For instance the binary could be an executable and the text file
might be a hex dump or the binary might be a word processor
document and the text an ASCII dump of the text of the document.
The possibilities are endless. But the answer is yes in princple.
If you can find fred.

don't quote sigs (the bit after --)

haha, I made a mistake here, I mean fread....but it seems fread does
not working....

fread() will almost certainly work on any reasonable
implementation. Please post your code and explain
why you think fread() is "not working".


--
Nick Keighley

A man's most valuable trait is a judicious sense of
what not to believe.
-- Euripides
 
X

xiao

please include the subject in the body of your text
Subject: convert binary file to txt file



what format is the binary file? What do you want the text to look
like.
For instance the binary could be an executable and the text file
might be a hex dump or the binary might be a word processor
document and the text an ASCII dump of the text of the document.
The possibilities are endless. But the answer is yes in princple.


don't quote sigs (the bit after --)


fread() will almost certainly work on any reasonable
implementation. Please post your code and explain
why you think fread() is "not working".

--
Nick Keighley

A man's most valuable trait is a judicious sense of
what not to believe.
-- Euripides




Thank you guys~ I modified the program like the below but the new
generated txt file is a little less then the original dat file. Is
that reasonalble? I think it is not right Thank you ~~~



#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
double CentLat;
double CentLon;
double DeltaX;
double DeltaY;
long ProjectionID;
double ProjectionParameters1;
double ProjectionParameters2;
double ProjectionParameters3;
double ProjectionParameters4;
double ProjectionParameters5;
double ProjectionParameters6;
double ProjectionParameters7;
double ProjectionParameters8;
double ProjectionParameters9;
double ProjectionParameters10;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;
long MapZone;
int NumX;
int NumY;
// The number of rays in
} NSC_RAY_DATA_HEADER;

/* Define the data structure for the ray information */

int main( )
{

double CentLat1;
double CentLon1;
double DeltaX1;
double DeltaY1;
long ProjectionID1;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;
double ProjectionParameters16;
double ProjectionParameters17;
double ProjectionParameters18;
double ProjectionParameters19;
double ProjectionParameters110;
double ProjectionParameters111;
double ProjectionParameters112;
double ProjectionParameters113;
double ProjectionParameters114;
double ProjectionParameters115;

long MapZone1;
int NumX1;
int NumY1;
int rc = EXIT_FAILURE;
FILE *in, *out;
NSC_RAY_DATA_HEADER nscrdh;


/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","rb");
out = fopen("newstats.txt","w");



if ( in != NULL )

{ printf ("Opened the file successfully\n");}

/* Read necessary header data into local variables & confirm file
format */

fread(&nscrdh,sizeof(nscrdh),1,in);

CentLat1=nscrdh.CentLat;
CentLon1=nscrdh.CentLon;
DeltaX1=nscrdh.DeltaX;
DeltaY1=nscrdh.DeltaY;
ProjectionID1=nscrdh.ProjectionID;
ProjectionParameters11= nscrdh.ProjectionParameters1;
ProjectionParameters12= nscrdh.ProjectionParameters2;
ProjectionParameters13= nscrdh.ProjectionParameters3;
ProjectionParameters14= nscrdh.ProjectionParameters4;
ProjectionParameters15= nscrdh.ProjectionParameters5;
ProjectionParameters16= nscrdh.ProjectionParameters6;
ProjectionParameters17= nscrdh.ProjectionParameters7;
ProjectionParameters18= nscrdh.ProjectionParameters8;
ProjectionParameters19= nscrdh.ProjectionParameters9;
ProjectionParameters110= nscrdh.ProjectionParameters10;
ProjectionParameters111= nscrdh.ProjectionParameters11;
ProjectionParameters112= nscrdh.ProjectionParameters12;
ProjectionParameters113= nscrdh.ProjectionParameters13;
ProjectionParameters114= nscrdh.ProjectionParameters14;
ProjectionParameters115= nscrdh.ProjectionParameters15;
MapZone1=nscrdh.MapZone;
NumX1=nscrdh.NumX;
NumY1=nscrdh.NumY;

fprintf(out, "%lf %lf %lf %lf %ld %ld %d %d",CentLat1,
CentLon1,DeltaX1, DeltaY1,ProjectionID1,MapZone1,NumX1,NumY1);

/* for(i=0; i<14 ;i++){
fprintf(out,"%ld",ProjectionID1);
}*/

fprintf(out,"%ld",ProjectionID1);
/* Write header, ray information into output file */

printf("The center latitude and cent longtitute are %lf %lf
\n",CentLat1,CentLon1);
printf("The deltax and deltay are %lf %lf \n",DeltaX1, DeltaY1);
printf("The projection ID is %ld \n",ProjectionID1);
printf("The MapZone is %ld \n",MapZone1);

printf("The projection parameter are %lf
\n",ProjectionParameters11);
printf("The projection parameter are %lf
\n",ProjectionParameters12);
printf("The projection parameter are %lf
\n",ProjectionParameters13);
printf("The projection parameter are %lf \n",ProjectionParameters14);
printf("The projection parameter are %lf \n",ProjectionParameters15);
printf("The projection parameter are %lf \n",ProjectionParameters16);
printf("The projection parameter are %lf \n",ProjectionParameters17);
printf("The projection parameter are %lf \n",ProjectionParameters18);
printf("The projection parameter are %lf \n",ProjectionParameters19);
printf("The projection parameter are %lf
\n",ProjectionParameters110);
printf("The projection parameter are %lf
\n",ProjectionParameters111);
printf("The projection parameter are %lf
\n",ProjectionParameters112);
printf("The projection parameter are %lf
\n",ProjectionParameters113);
printf("The projection parameter are %lf
\n",ProjectionParameters114);

printf("The projection parameter are %lf
\n",ProjectionParameters115);

printf("The numy and numx are %d %d \n",NumX1,NumY1);
/* fread(&nscrd,sizeof(nscrd),14,in);
xx= nscrd.x;
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a]=nscrd.x[a];
}
}
fprintf(out,"%hd", xx);
printf("The numbers are %d \n",xx);
}*/

/* Close files and exit the program */
if(out != NULL)
{
short sh = 0;
while(fread(&sh, sizeof( sh), 1, in) > 0)
{
fprintf(out, "%hd", sh);
}
printf("The itams read in the file are %d\n",fread(&sh,
sizeof( sh),1,in));
if(!ferror(in) && !ferror(out))
{
rc = EXIT_SUCCESS;
}
fclose(out);
}
fclose(in);
return rc;

}
 
B

Ben Bacarisse

Thank you guys~ I modified the program like the below but the new
generated txt file is a little less then the original dat file. Is
that reasonalble?

It might be. I don't know the data so I can't really say. Writing
loads of "0 0 0 0 0" is shorter than the equivalent binary floating
point, for example. For 800x800 shorts it is much less likely,
although since you have no separators in you output you are in with a
chance that the output will be smaller than the input.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
double CentLat;
double CentLon;
double DeltaX;
double DeltaY;
long ProjectionID;
double ProjectionParameters1;
double ProjectionParameters2;
double ProjectionParameters3;
double ProjectionParameters4;
double ProjectionParameters5;
double ProjectionParameters6;
double ProjectionParameters7;
double ProjectionParameters8;
double ProjectionParameters9;
double ProjectionParameters10;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;

<fx: comedy screeching of brakes>

Hold on. This is what arrays are for.
long MapZone;
int NumX;
int NumY;
// The number of rays in
} NSC_RAY_DATA_HEADER;

/* Define the data structure for the ray information */

int main( )
{

double CentLat1;
double CentLon1;
double DeltaX1;
double DeltaY1;
long ProjectionID1;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;
double ProjectionParameters16;
double ProjectionParameters17;
double ProjectionParameters18;
double ProjectionParameters19;
double ProjectionParameters110;
double ProjectionParameters111;
double ProjectionParameters112;
double ProjectionParameters113;
double ProjectionParameters114;
double ProjectionParameters115;

I see point to all these variables.
long MapZone1;
int NumX1;
int NumY1;
int rc = EXIT_FAILURE;
FILE *in, *out;
NSC_RAY_DATA_HEADER nscrdh;


/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","rb");
out = fopen("newstats.txt","w");



if ( in != NULL )

{ printf ("Opened the file successfully\n");}

/* Read necessary header data into local variables & confirm file
format */

fread(&nscrdh,sizeof(nscrdh),1,in);

CentLat1=nscrdh.CentLat;
CentLon1=nscrdh.CentLon;
DeltaX1=nscrdh.DeltaX;
DeltaY1=nscrdh.DeltaY;
ProjectionID1=nscrdh.ProjectionID;
ProjectionParameters11= nscrdh.ProjectionParameters1;
ProjectionParameters12= nscrdh.ProjectionParameters2;
ProjectionParameters13= nscrdh.ProjectionParameters3;
ProjectionParameters14= nscrdh.ProjectionParameters4;
ProjectionParameters15= nscrdh.ProjectionParameters5;
ProjectionParameters16= nscrdh.ProjectionParameters6;
ProjectionParameters17= nscrdh.ProjectionParameters7;
ProjectionParameters18= nscrdh.ProjectionParameters8;
ProjectionParameters19= nscrdh.ProjectionParameters9;
ProjectionParameters110= nscrdh.ProjectionParameters10;
ProjectionParameters111= nscrdh.ProjectionParameters11;
ProjectionParameters112= nscrdh.ProjectionParameters12;
ProjectionParameters113= nscrdh.ProjectionParameters13;
ProjectionParameters114= nscrdh.ProjectionParameters14;
ProjectionParameters115= nscrdh.ProjectionParameters15;

What is the point of copying all these values from one convenient
structure into a whole load of variables?
MapZone1=nscrdh.MapZone;
NumX1=nscrdh.NumX;
NumY1=nscrdh.NumY;

fprintf(out, "%lf %lf %lf %lf %ld %ld %d %d",CentLat1,
CentLon1,DeltaX1, DeltaY1,ProjectionID1,MapZone1,NumX1,NumY1);

/* for(i=0; i<14 ;i++){
fprintf(out,"%ld",ProjectionID1);
}*/


Ah, there was an array once...
fprintf(out,"%ld",ProjectionID1);

This is a problem. See later...
/* Write header, ray information into output file */

printf("The center latitude and cent longtitute are %lf %lf
\n",CentLat1,CentLon1);
printf("The deltax and deltay are %lf %lf \n",DeltaX1, DeltaY1);
printf("The projection ID is %ld \n",ProjectionID1);
printf("The MapZone is %ld \n",MapZone1);

printf("The projection parameter are %lf
\n",ProjectionParameters11);
printf("The projection parameter are %lf
\n",ProjectionParameters12);
printf("The projection parameter are %lf
\n",ProjectionParameters13);
printf("The projection parameter are %lf \n",ProjectionParameters14);
printf("The projection parameter are %lf \n",ProjectionParameters15);
printf("The projection parameter are %lf \n",ProjectionParameters16);
printf("The projection parameter are %lf \n",ProjectionParameters17);
printf("The projection parameter are %lf \n",ProjectionParameters18);
printf("The projection parameter are %lf \n",ProjectionParameters19);
printf("The projection parameter are %lf
\n",ProjectionParameters110);
printf("The projection parameter are %lf
\n",ProjectionParameters111);
printf("The projection parameter are %lf
\n",ProjectionParameters112);
printf("The projection parameter are %lf
\n",ProjectionParameters113);
printf("The projection parameter are %lf
\n",ProjectionParameters114);

printf("The projection parameter are %lf
\n",ProjectionParameters115);

printf("The numy and numx are %d %d \n",NumX1,NumY1);
/* fread(&nscrd,sizeof(nscrd),14,in);
xx= nscrd.x;
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a]=nscrd.x[a];
}
}
fprintf(out,"%hd", xx);
printf("The numbers are %d \n",xx);
}*/

/* Close files and exit the program */
if(out != NULL)
{
short sh = 0;
while(fread(&sh, sizeof( sh), 1, in) > 0)
{
fprintf(out, "%hd", sh);


This is a very unusual format. The two numbers 12 and 3 are
represented in the same way as 1 and 23. I think you need to re-think
this part.
 
B

Barry Schwarz

xiao said:

Here is my program: I just want to converted the input file to see
the content of it. But seems something wrong with opnning the file.

I'll give you six.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_PATH_LENGTH 100000000

/* Define the data structure for the file header information */
typedef struct
{
short num;

} HEADER;

typedef struct
{

short x;

} DATA;

int main(void)
{

short xx,numn;
char disp[MAX_PATH_LENGTH];

1) You're asking for an object 100,000,000 bytes in size. C only guarantees
you an object 32,767 bytes in size (65,535 if you have a C99
implementation, which you probably don't).
int i;
FILE *in, *out;
HEADER nscrdh;
DATA nscrd;

/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","r");

2) You don't check that this call succeeded. If it fails, the behaviour of
your program is undefined.

3) If you wanted to open this file in binary mode, you should specify "rb"
as the mode, not just "r".
out = fopen("newstats.txt","wt");

4) You don't check that this call succeeded. If it fails, the behaviour of
your program is undefined.

5) The fopen function has no "wt" mode. If you mean you want a text file,
specify "w".
/* Read necessary header data into local variables & confirm file
format */
for (i=0; i<84; i++)
{
fread(&nscrdh,sizeof(short),1,in);

6) Here, you show confusion about whether you're reading a HEADER (you're
passing a pointer to a HEADER object) or a short (you use sizeof(short) in
the call). Clearer: fread(&nscrdh, sizeof nscrdh, 1, in);

In this case, using sizeof nscrdh can cause data to be skipped if the
compiler was perverse enough to put padding after the last (only)
member of the structure.

To me the real question is why bother with a struct if you only have
one scalar member?


Remove del for email
 
B

Barry Schwarz

snip
Here is my program: I just want to converted the input file to see
the content of it. But seems something wrong with opnning the file.


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_PATH_LENGTH 100000000

/* Define the data structure for the file header information */
typedef struct
{
short num;

} HEADER;

typedef struct
{

short x;

} DATA;

int main(void)
{

short xx,numn;
char disp[MAX_PATH_LENGTH];
int i;
FILE *in, *out;
HEADER nscrdh;
DATA nscrd;

/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","r");
out = fopen("newstats.txt","wt");

/* Read necessary header data into local variables & confirm file
format */
for (i=0; i<84; i++)
{
fread(&nscrdh,sizeof(short),1,in);

Does the header contain 84 shorts or 84 char?
numn=nscrdh.num;

Why not read directly in numn?
sprintf(disp, "%d \n", numn);

Here you almost invoke undefined behavior. %d expects an int; you are
attempting to pass a short. Fortunately, for a variadic function, an
"unspecified" short will be promoted to int. Did you really plan on
this or were you just lucky?

Do you really intend to memorize the binary representation of at least
65,536 different possible short values. Most people try to decode
binary data in 8 bit chunks which only requires memorizing 256
representations.

In either case, wouldn't it be easier to print the data in hex?
/* Write header, ray information into output file */
fputs(disp, out);
}
for (i=0; i <8960000; i++)

Are you sure your file contains this much data.
{
fread(&nscrd,1,sizeof(short),in);
xx= nscrd.x;
sprintf(disp, "%d \n", xx);
fputs(disp, out);
}
fclose(in);
fclose(out);

return 0;
}


Remove del for email
 
B

Barry Schwarz

snip 44 lines of obsolete discussion
haha .thank you~ i changed my mind here , If I define it like this:

short xx[800][800];


or


typedef struct
{
short x[800][800];
} NSC_RAY_DATA


how can I access the data in fread and fprintf?
I tried this:

fread(xx,sizeof(xx),14,in);

What is the size of xx? Literally, how many bytes does it occupy? How
many objects of that size did you define? How many did you request
fread to process? What happens when you try to read more data than
your buffer will hold?
for(i=0; i<14 ;i++)
{ fprintf(out,"%hd", xx);}


What is the type of the expression xx? What happens automatically
to expressions of this type when they are used in expressions where
they are evaluated? What is the type of the expression fprintf
receives? What type of expression did %hd promise fprintf? What
happens when you lie to fprint like this (hint - same as above)?
and
fread(&nscrd,sizeof(nscrd),14,in);

You introduced a new structure but never told us what nscrd is. Is it
the same as in your earlier post? If so, you have exactly the same
problem here as you do in the fread call using xx.
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a]=nscrd.x[a];


Nowhere in any of your code was nscrd.x an array. It was always a
short.
}
}
fprintf(out,"%hd", xx);

This fprintf call has the same problem as the one above.
And both of them remind me that :

warning: int format, pointer arg (arg 3)

As well they should. What type does %hd promise to provide? What
type do you actually provide? Are the two in any way compatible?


Remove del for email
 
B

Barry Schwarz

Thank you guys~ I modified the program like the below but the new
generated txt file is a little less then the original dat file. Is
that reasonalble? I think it is not right Thank you ~~~



#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
double CentLat;
double CentLon;
double DeltaX;
double DeltaY;
long ProjectionID;
double ProjectionParameters1;
double ProjectionParameters2;
double ProjectionParameters3;
double ProjectionParameters4;
double ProjectionParameters5;
double ProjectionParameters6;
double ProjectionParameters7;
double ProjectionParameters8;
double ProjectionParameters9;
double ProjectionParameters10;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;
long MapZone;
int NumX;
int NumY;
// The number of rays in
} NSC_RAY_DATA_HEADER;

/* Define the data structure for the ray information */

int main( )
{

double CentLat1;
double CentLon1;
double DeltaX1;
double DeltaY1;
long ProjectionID1;
double ProjectionParameters11;
double ProjectionParameters12;
double ProjectionParameters13;
double ProjectionParameters14;
double ProjectionParameters15;
double ProjectionParameters16;
double ProjectionParameters17;
double ProjectionParameters18;
double ProjectionParameters19;
double ProjectionParameters110;
double ProjectionParameters111;
double ProjectionParameters112;
double ProjectionParameters113;
double ProjectionParameters114;
double ProjectionParameters115;

long MapZone1;
int NumX1;
int NumY1;
int rc = EXIT_FAILURE;
FILE *in, *out;
NSC_RAY_DATA_HEADER nscrdh;


/* Determine the file names to be read from and written to */

/* Open the files to be read from and written to*/
in = fopen("CldTotalStats_8.dat","rb");
out = fopen("newstats.txt","w");



if ( in != NULL )

{ printf ("Opened the file successfully\n");}

What you really need is code to handle the case where in is NULL.
/* Read necessary header data into local variables & confirm file
format */

fread(&nscrdh,sizeof(nscrdh),1,in);

CentLat1=nscrdh.CentLat;

Why copy all the members of the structure to scalar variables? You
can work with them in the structure just as easily.
CentLon1=nscrdh.CentLon;
DeltaX1=nscrdh.DeltaX;
DeltaY1=nscrdh.DeltaY;
ProjectionID1=nscrdh.ProjectionID;
ProjectionParameters11= nscrdh.ProjectionParameters1;
ProjectionParameters12= nscrdh.ProjectionParameters2;
ProjectionParameters13= nscrdh.ProjectionParameters3;
ProjectionParameters14= nscrdh.ProjectionParameters4;
ProjectionParameters15= nscrdh.ProjectionParameters5;
ProjectionParameters16= nscrdh.ProjectionParameters6;
ProjectionParameters17= nscrdh.ProjectionParameters7;
ProjectionParameters18= nscrdh.ProjectionParameters8;
ProjectionParameters19= nscrdh.ProjectionParameters9;
ProjectionParameters110= nscrdh.ProjectionParameters10;
ProjectionParameters111= nscrdh.ProjectionParameters11;
ProjectionParameters112= nscrdh.ProjectionParameters12;
ProjectionParameters113= nscrdh.ProjectionParameters13;
ProjectionParameters114= nscrdh.ProjectionParameters14;
ProjectionParameters115= nscrdh.ProjectionParameters15;
MapZone1=nscrdh.MapZone;
NumX1=nscrdh.NumX;
NumY1=nscrdh.NumY;

fprintf(out, "%lf %lf %lf %lf %ld %ld %d %d",CentLat1,
CentLon1,DeltaX1, DeltaY1,ProjectionID1,MapZone1,NumX1,NumY1);

Don't you think it would pay to label the values. Since you
immediately print these out again, did you mean to comment out this
statement also?
/* for(i=0; i<14 ;i++){
fprintf(out,"%ld",ProjectionID1);
}*/

fprintf(out,"%ld",ProjectionID1);


Do you really want this variable and the previous 8 all on the same
line? Without a space between NumY1 and ProjectionID1?
/* Write header, ray information into output file */

printf("The center latitude and cent longtitute are %lf %lf
\n",CentLat1,CentLon1);

Do you really want this on the same line with the previous 9?
printf("The deltax and deltay are %lf %lf \n",DeltaX1, DeltaY1);
printf("The projection ID is %ld \n",ProjectionID1);
printf("The MapZone is %ld \n",MapZone1);

printf("The projection parameter are %lf
\n",ProjectionParameters11);

Don't you think the text should say which parameter? As it is now,
they say the same thing.
printf("The projection parameter are %lf
\n",ProjectionParameters12);
printf("The projection parameter are %lf
\n",ProjectionParameters13);
printf("The projection parameter are %lf \n",ProjectionParameters14);
printf("The projection parameter are %lf \n",ProjectionParameters15);
printf("The projection parameter are %lf \n",ProjectionParameters16);
printf("The projection parameter are %lf \n",ProjectionParameters17);
printf("The projection parameter are %lf \n",ProjectionParameters18);
printf("The projection parameter are %lf \n",ProjectionParameters19);
printf("The projection parameter are %lf
\n",ProjectionParameters110);
printf("The projection parameter are %lf
\n",ProjectionParameters111);
printf("The projection parameter are %lf
\n",ProjectionParameters112);
printf("The projection parameter are %lf
\n",ProjectionParameters113);
printf("The projection parameter are %lf
\n",ProjectionParameters114);

printf("The projection parameter are %lf
\n",ProjectionParameters115);

printf("The numy and numx are %d %d \n",NumX1,NumY1);

Either the text or the arguments are in the wrong order.
/* fread(&nscrd,sizeof(nscrd),14,in);
xx= nscrd.x;
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a]=nscrd.x[a];
}
}
fprintf(out,"%hd", xx);
printf("The numbers are %d \n",xx);
}*/

/* Close files and exit the program */
if(out != NULL)


Since you have already written several lines to out, this is a little
late.
{
short sh = 0;
while(fread(&sh, sizeof( sh), 1, in) > 0)
{
fprintf(out, "%hd", sh);
}
printf("The itams read in the file are %d\n",fread(&sh,
sizeof( sh),1,in));

Are you sure there is another short left to read in the file? The
only two possible outputs from this are 0 and 1. Is this what you
really want to print?

fread returns a size_t. This need not be an int. Regardless of its
size, it will be unsigned. Unless you have a format specification
that matches size_t, you need to cast the return from fread to match
whatever specification you use.
if(!ferror(in) && !ferror(out))
{
rc = EXIT_SUCCESS;
}
fclose(out);
}
fclose(in);
return rc;

}


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top