473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

convert binary file to txt file

Can I fullfill this task? Using fred and fwrite?
Jul 23 '08 #1
22 11702
In article <f1**********************************@k13g2000hse. googlegroups.com>,
xiao <li*********@gmail.comwrote:
>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.
--
"To the modern spirt nothing is, or can be rightly known,
except relatively and under conditions." -- Walter Pater
Jul 23 '08 #2
xiao wrote:
>
Can I fullfill this task? Using fred and fwrite?
If you can find fred.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Jul 23 '08 #3
On Jul 23, 11:20 am, CBFalconer <cbfalco...@yahoo.comwrote:
xiao wrote:
Can I fullfill this task? Using fred and fwrite?

If you can find fred.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
haha, I made a mistake here, I mean fread....but it seems fread does
not working....
Jul 23 '08 #4
In article <84**********************************@k37g2000hsf. googlegroups.com>,
xiao <li*********@gmail.comwrote:
>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.

--
"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
Jul 23 '08 #5
On Jul 23, 12:07 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <84de7aca-4fb9-469c-afc0-6e7b38aa7...@k37g2000hsf.googlegroups.com>,

xiao <littledd...@gmail.comwrote:
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.

--
"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;
}

Jul 23 '08 #6
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 <li*********@gmail.comwrites:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jul 23 '08 #7
xiao said:

<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.
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);

--
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
Jul 23 '08 #8
On Jul 23, 2:38 pm, Keith Thompson <ks...@mib.orgwrote:
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 <littledd...@gmail.comwrites:
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).

--
Keith Thompson (The_Other_Keith) ks...@mib.org <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.
Jul 23 '08 #9
Richard Heathfield <rj*@see.sig.invalidwrites:
xiao said:

<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.

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]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jul 23 '08 #10
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.

--
Er*********@sun.com
Jul 23 '08 #11
On Jul 23, 3:37 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
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.

--
Eric.Sos...@sun.com
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....
Jul 23 '08 #12
On Jul 23, 2:40 pm, Richard Heathfield <r...@see.sig.invalidwrote:
xiao said:

<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.

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);

--
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~
Jul 23 '08 #13
xiao wrote:
On Jul 23, 3:37 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>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.

--
Er*********@sun.com

Jul 23 '08 #14
On Jul 23, 4:21 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
xiao wrote:
On Jul 23, 3:37 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
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.

--
Eric.Sos...@sun.com
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[i]);}

and
fread(&nscrd,sizeof(nscrd),14,in);
for(a=0; a<800 ;a++){
for(b=0; b<800 ;b++){
xx[a][b]=nscrd.x[a][b];
}
}
fprintf(out,"%hd", xx);
And both of them remind me that :

warning: int format, pointer arg (arg 3)
Jul 24 '08 #15
xiao said:
On Jul 23, 4:21 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
<snip>
> 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];
....then you are still working way too hard.

Look, this is really, really, really easy.

Your spec appears to be an input binary file comprising short ints, which
you want to output in decimal form with no spaces between them or anything
like that, no newlines, just raw data.

Fine.

The following has full error-checking, but no error-reporting. Feel free to
add it.

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

int main(int argc, char **argv)
{
int rc = EXIT_FAILURE;
if(argc 2)
{
FILE *fin = fopen(argv[1], "rb");
if(fin != NULL)
{
FILE *fout = fopen(argv[2], "w");
if(fout != NULL)
{
short sh = 0;
while(fread(&sh, sizeof sh, 1, fin) 0)
{
fprintf(fout, "%hd", sh);
}
if(!ferror(fin) && !ferror(fout))
{
rc = EXIT_SUCCESS;
}
fclose(fout);
}
fclose(fin);
}
}
return rc;
}

--
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
Jul 24 '08 #16
xiao <li*********@gmail.comwrote:
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
Jul 24 '08 #17
please include the subject in the body of your text
Subject: convert binary file to txt file

On 23 Jul, 17:46, xiao <littledd...@gmail.comwrote:
On Jul 23, 11:20 am, CBFalconer <cbfalco...@yahoo.comwrote:
xiao wrote:
Can I fullfill this task? Using fred and fwrite?
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.
--
*[mail]: Chuck F (cbfalconer at maineline dot net)
*[page]: <http://cbfalconer.home.att.net>
* * * * * * Try the download section.
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
Jul 24 '08 #18
On Jul 24, 3:41 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
please include the subject in the body of your text
Subject: convert binary file to txt file

On 23 Jul, 17:46, xiao <littledd...@gmail.comwrote:
On Jul 23, 11:20 am, CBFalconer <cbfalco...@yahoo.comwrote:
xiao wrote:
Can I fullfill this task? Using fred and fwrite?

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.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

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

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[i]);
}*/

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][b]=nscrd.x[a][b];
}
}
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;

}
Jul 24 '08 #19
xiao <li*********@gmail.comwrites:

<snip>
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[i]);
}*/
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][b]=nscrd.x[a][b];
}
}
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.
}
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;

}

--
Ben.
Jul 24 '08 #20
On Wed, 23 Jul 2008 19:40:54 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>xiao said:

<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.

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
Jul 25 '08 #21
On Wed, 23 Jul 2008 12:32:55 -0700 (PDT), xiao <li*********@gmail.com>
wrote:

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
Jul 25 '08 #22
On Wed, 23 Jul 2008 18:10:41 -0700 (PDT), xiao <li*********@gmail.com>
wrote:

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[i]);}
What is the type of the expression xx[i]? 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][b]=nscrd.x[a][b];
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
Jul 25 '08 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
2
by: Joey Lee | last post by:
Hi, Does anyone know how I am able to write a utf-8 encoded binary string into binary file? Currently I am given a UTF-8 string which was read from a gif image. Here are my functions... ...
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
0
by: Andy | last post by:
Hi, I have a MS Word binary data file that is sent from my .NET webservice in response to an XMLHTTP request from an Internet Explorer client. This data has to be base64 encoded to tunnel through...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
5
by: sweeet_addiction16 | last post by:
im coding in c....i need to accept an integer value(decimal) and then after converting it into hexadecimal value i need to write it into a file.i do not need to print it..so using fprintf along...
4
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
1
by: Man4ish | last post by:
I have sent one thread about the use of binary file for file indexing and got some very good reply and which helped me a lot. Now I am proceeding ahead. But facing one pblm of converting text file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.