X
xiao
Can I fullfill this task? Using fred and fwrite?
Can I fullfill this task? Using fred and fwrite?
xiao said:Can I fullfill this task? Using fred and fwrite?
If you can find fred.
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.
--
"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
xiao said:Can I fullfill this task? Using fred and fwrite?
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"
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).
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.
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);
--
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
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.
xiao said:Can I fullfill this task?
Using fred and fwrite?
If you can find fred.
haha, I made a mistake here, I mean fread....but it seems fread does
not working....
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?
#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);
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);
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;
}
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)
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;
}
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.