473,569 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert binary file to txt file

Can I fullfill this task? Using fred and fwrite?
Jul 23 '08 #1
22 11738
In article <f1************ *************** *******@k13g200 0hse.googlegrou ps.com>,
xiao <li*********@gm ail.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...@yah oo.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************ *************** *******@k37g200 0hsf.googlegrou ps.com>,
xiao <li*********@gm ail.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.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <84de7aca-4fb9-469c-afc0-6e7b38aa7...@k3 7g2000hsf.googl egroups.com>,

xiao <littledd...@gm ail.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("CldTotal Stats_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,s izeof(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),i n);
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*********@gm ail.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_Keit h) 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("CldTotal Stats_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,s izeof(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.orgw rote:
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...@gm ail.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_Keit h) 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.in validwrites:
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_Keit h) 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

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

Similar topics

4
6094
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 like this: $buf = array(0x41, 0x42, 0x43); Anyone know how? I haven't been able to find a way.
2
11175
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... public Byte GetDocument(string DocumentName) { string strdocPath;
4
79608
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
0
1568
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 a firewall. I've used the .NET Convert.ToBase64String() method to encode the data. I understand that the Convert.FromBase64String only runs on...
7
19204
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
29
5062
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 something like ascii text editor. I'd also like to be able to read the binary formed data back into string format so that it shows the original value....
5
1289
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 with %lx would not help me.for eg..if i have a decimal value of 60 to be passed to a function ..i need that function to convert it into hexadecimal...
4
22125
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 = open("test2.pc0", "rb") In : tagData = f.read(4) In : tagData
1
3571
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 into binary file of fixed length. example: input file 34134 43214 1 + A/G 44134 43214 1 + A/G 346134 4323214...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.