473,700 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

differance between binary file and ascii file

vim
hello everybody
Plz tell the differance between binary file and ascii
file........... ....
Thanks
in advance
vim

May 13 '06 #1
68 5232
vim said:
hello everybody
Plz tell the differance between binary file and ascii
file........... ....


Well, that's really the wrong question.

The right question is: "what is the difference between a stream opened in
binary mode, and a stream opened in text mode?"

Let's deal with the easy one first. When you associate a binary stream with
a file, the data flows in from the file, through the stream, unmodified
(or, if you're writing, it flows out, through the stream, to the file,
unmodified). It's just a raw stream of bytes, to do with as you will.

Okay, now the hard one. When you associate a /text/ stream with a file, you
are assuming the convention that the data comprises zero or more lines,
where each line is composed of 0 or more bytes followed by a newline marker
of some kind.

The newline marker defined by C is '\n'.

On Unix, this agrees well with the marker actually used by longstanding
convention on that system.

On CP/M and derivatives (such as Q-DOS and that point-and-click adventure
game it spawned), the marker is '\r' and '\n', in that order.

On the Mac, it's just plain '\r'.

On the mainframe - well, you /really/ don't want to know.

All this is a bit of a nuisance, and it would be nice if we didn't have to
bother with such niceties when processing plain ol' text. And so, when you
are reading from a text stream, the standard library performs any necessary
conversions on incoming data, to force-map the newline marker into a nice
simple '\n'. And when you are writing to the stream, the standard library
looks for '\n' characters and replaces them with the byte or bytes used for
marking newlines on the particular system on which the program is running.

So, when you are writing your code, you can just pretend that the newline
marker is '\n', and - to all intents and purposes - so it is! So you don't
have to mess about with detecting whether you're running on a Mac or a mini
or a mainframe - you can just assume a '\n' delimiter and let the standard
library worry about the underlying representation.

If you don't /want/ the system to do this, open the file in binary mode. But
then managing the newline stuff all falls to you instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 13 '06 #2
"vim" wrote:
Plz tell the differance between binary file and ascii
file........... ....


Here is a long and recent discussion on the subject.

http://tinyurl.com/s737d
May 13 '06 #3
vim
thanks a lot

May 13 '06 #4

vim wrote:
hello everybody
Plz tell the differance between binary file and ascii
Mate, you are in big trouble now. You just used 'silly' 'Plz'.
And this question is off-topic for someone.

If we come to answer. Every file is binary but when you open ASCII
file, you will see bytes representing elements of ASCII set. Every
element of ASCII set can be stored in char in C programming language.
And ASCII char is one byte and one byte is eight bits. And one bit only
can be 0 or 1. You will find more if you google for "binary
arithmetic".
file........... ....
Thanks
in advance
vim


May 13 '06 #5
Parahat Melayev said:

vim wrote:
hello everybody
Plz tell the differance between binary file and ascii
Mate, you are in big trouble now.


No, he isn't.
You just used 'silly' 'Plz'.
Yes, he did.
And this question is off-topic for someone.
No, it isn't. But in some ways, your answer is.
If we come to answer. Every file is binary
The C Standard does not guarantee this.
but when you open ASCII file,
The C Standard does not specify the concept "ASCII file".
you will see bytes representing elements of ASCII set. Every
element of ASCII set can be stored in char in C programming language.
And ASCII char is one byte and one byte is eight bits.


C does not specify that one byte is eight bits wide - although it does
specify that one byte is *at least* eight bits wide.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 13 '06 #6
Parahat Melayev wrote:
vim wrote:
hello everybody
Plz tell the differance between binary file and ascii
Mate, you are in big trouble now. You just used 'silly' 'Plz'.


It *is* a silly abbreviation. While nobody is going to be in big
trouble, consistent use of such incomprehensibl e English will simply
result the poster being ignored.
And this question is off-topic for someone.
Who said it was off-topic? Certainly it perfectly within topic as far
as I'm aware.
And ASCII char is one byte and one byte is eight bits.
A C byte is not always eight bits is size. Though 8-bit bytes are
common on PCs, other sizes are possible, and indeed are prevalent, on
Mainframes, DSPs etc. A char is garunteed by the standard to be atleast
8 bits but it could be more.
You will find more if you google for "binary arithmetic".


What has binary arithmetic got to do with C streams?

May 13 '06 #7

Richard Heathfield wrote:
[When] you
are reading from a text stream, the standard library performs any necessary
conversions on incoming data, to force-map the newline marker into a nice
simple '\n'. And when you are writing to the stream, the standard library
looks for '\n' characters and replaces them with the byte or bytes used for
marking newlines on the particular system on which the program is running. From this explanation, I am under the impression that stdin is

therefore opened in binary mode, since I find I have to explicitly deal
with '\r's to ensure that redirected input from text files works. For
example, I once wrote an rtrim function to remove trailing whitespace
from a input line assumed to have come from stdin (being part of a K&R
exercise, I didn't give myself the luxury of using things like
isspace(); the code quality is in any event not the focus of my
curiosity :-)

#include <stdio.h>

/* rtrim: removes trailing whitespace from s, re-attaches '\n' if
necessary*/
void rtrim(char s[], int len)
{
int i, newline;

newline = 0;
i = len - 1;
while(s[i] == '\t' || s[i] == ' ' || s[i] == '\n' || s[i] ==
'\r') {
if(s[i] == '\n')
newline = 1;
--i;
}

if(newline && i > 0)
s[++i] = '\n';
s[++i] = '\0';
}

I added the check for '\r' as a afterthought in order to make the code
more portable (since it broke under Windows) - the input line I was
passing to rtrim was being read from stdin using getchar() and was
simply a line terminated by a sole '\n' character. The '\r's of course
cropped up when I redirected Windows text files to stdin.

So again, it seems stdin is opened in binary mode, and not text mode,
since the newlines don't get converted to a single '\n'. Does the
Standard make any statement about the default mode stdin opens in (and
for that matter, stdout and stderr), and is it possible or worthwhile
to explicitly put stdin into text mode if you know that you are going
to deal with text input exclusively?

Or it may well be the case I am missing something more fundamental
here....

Mike S
---
"[BASIC] programmers...a re mentally mutilated beyond hope of
regeneration. " - Dijkstra

May 14 '06 #8
Mike S said:

Richard Heathfield wrote:
[When] you
are reading from a text stream, the standard library performs any
necessary conversions on incoming data, to force-map the newline marker
into a nice simple '\n'. And when you are writing to the stream, the
standard library looks for '\n' characters and replaces them with the
byte or bytes used for marking newlines on the particular system on which
the program is running.

From this explanation, I am under the impression that stdin is

therefore opened in binary mode, since I find I have to explicitly deal
with '\r's to ensure that redirected input from text files works.


No, that almost certainly means simply that you've got a Windows text file
on a Linux system. Linux doesn't know, bless it, that you've been sullying
its filesystem with foreign muck. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #9
santosh wrote:
Parahat Melayev wrote:
vim wrote:
> hello everybody
> Plz tell the differance between binary file and ascii
Mate, you are in big trouble now. You just used 'silly' 'Plz'.


It *is* a silly abbreviation. While nobody is going to be in big
trouble, consistent use of such incomprehensibl e English will simply
result the poster being ignored.
And this question is off-topic for someone.


Who said it was off-topic? Certainly it perfectly within topic as far
as I'm aware.


This newsgroup is for C language related questions. The OP is asking a more
generalized question.

The OP may find help here -
http://en.wikipedia.org/wiki/Binary_arithmetic
And ASCII char is one byte and one byte is eight bits.
A C byte is not always eight bits is size. Though 8-bit bytes are
common on PCs, other sizes are possible, and indeed are prevalent, on
Mainframes, DSPs etc.


A byte is always 8 bits by definition!!! On older CDC computers, for
example, there was a "character" of 6 bits but it was never referred to as
a "byte".

A char is garunteed by the standard to be atleast
8 bits but it could be more.
A character is not some arbitrary size. A character is either one Byte (ie
8 bits) or in the case of Unicode it is two Bytes (ie 16 bits). BTW - the
word is "guaranteed ".
You will find more if you google for "binary arithmetic".


What has binary arithmetic got to do with C streams?


Don't you know??
Alan

May 14 '06 #10

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

Similar topics

8
3452
by: Bernhard Hidding | last post by:
Hi, my program writes chars to an ascii file via ofstream. You can use "\n" for newline and "\t" for tab there, but is there any command that deletes the last char in the current ofstream? Thanks in advance, Bernhard
2
3294
by: anirudhvr | last post by:
Hi, I needed a basic binary to ascii encoder, so I wrote this piece of code: /* Encoding algo: suppose 11111010 is the byte to be encoded. It is first broken up into two 4-bit parts (1111 and 1010) This 4-bit quantity is then padded between a 01 and a 10, ie, 01xxxx10 will be written out to the output file.
31
3167
by: Claude Yih | last post by:
Hi, everyone. I got a question. How can I identify whether a file is a binary file or an ascii text file? For instance, I wrote a piece of code and saved as "Test.c". I knew it was an ascii text file. Then after compilation, I got a "Test" file and it was a binary executable file. The problem is, I know the type of those two files in my mind because I executed the process of compilation, but how can I make the computer know the type of a...
6
10034
by: SandyMan | last post by:
Hi, I am able to open a binary file for reading but can someone tell me as how to go about converting a Binary file to ASCII file using C. Thanks In Advance SandyMan
5
10711
by: veg_all | last post by:
I have a script where a user can upload their csv file into a mysql database. The problem is sometimes a user will upload the raw excel or access file. How can I perform some simple checks to determine if the file is binary. I could do some regular expressions to check for unusual characters, but how would I read the file line by line?
1
1403
by: rsr | last post by:
Hi everybody, I have a VC++ application to connect two computers with a serial cable. Can anyone of u tell me which scheme of encoding is used to transmit data?..Binary or ASCII.. I guess its Binary as I am able to transmit even special characters like Δ(delta), alpha, beta..But I am not sure.. Thanks in advance
24
2999
by: pbd22 | last post by:
Hi. I want to know the size of a file (prior to FTP) in ASCII Mode. The reason is that I want to know how bit the file is going to be once it reaches the FTP server and there is a size difference between binary and ascii mode (100 or so bytes). So, how do I get the size of the file in ASCII Mode using the usual VB.NET API?
2
3886
by: tedpottel | last post by:
Hi, My program has the following code to transfer a binary file f = open(pathanme+filename,'rb') print "start transfer" self.fthHandle.storbinary('STOR '+filename, f) How can I do an ASCII file transfer?????? -Ted
1
2603
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02) .. ........ Likewise I read the mac entries from the text file using a perl script and do some processing
0
8728
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9217
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
8926
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...
1
6563
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5903
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
4404
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
4659
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2030
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.