473,699 Members | 2,701 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
22 11765
On Wed, 23 Jul 2008 19:40:54 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>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, 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*********@gm ail.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("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, 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
"unspecifie d" 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),i n);
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*********@gm ail.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,"%h d", 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,si zeof(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,"%h d", 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
6108
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
11207
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
79643
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
0
1584
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 the server. How can I decode the data using jscript that's running on the browser?
7
19216
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
5081
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. Is there any way to do this in Python? Thanks!
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 value(eg 3c) and then write it into a file
4
22238
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
3581
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 1 + A/G Here file should not only be tab separated but each value in column should take...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.