Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > cannot understand the character handling Program

Reply
Thread Tools

cannot understand the character handling Program

 
 
Jack Klein
Guest
Posts: n/a
 
      01-05-2006
On 4 Jan 2006 09:52:34 -0800, "yogeshmk" <(E-Mail Removed)>
wrote in comp.lang.c:

> wow!
> I must say that this is an eye opener for me. All this time i was under
> the impression that EOF is a special character. Coming from a Cobol
> background sentences like OPEN <FILE> AT END.. verbs, and because of
> MPE/iX intrinsics like FWRITEDIR which 'writes' EOF marker at the end
> of last record in file, I was 'convinced' that EOF is a character like
> CR/LF which is present in the file. I somehow never thought that it
> could be a condition not a character.
>
> Thanx for the nice explanation.
> ~yogesh


On some platforms, under some circumstances, it is. At least for text
files.

Early OS's for small platforms (specifically CP/M 80), did not track
the actual size of a file in octets, only in logical sectors, which
happened to 128 octets. When you read the last sector of a file into
memory, you had no real way of telling how many of the 128 octets were
part of the data, and how many were just left-over junk.

For binary files, you were on your own, but for text files the
convention was that the program that wrote the file appended a 0x1a
after the last actual valid byte.

Early versions of MS-DOS used the same convention.

I am resurrecting some code I wrote in the mid 1980's on MS-DOS. When
I open them in a modern GUI editor, there's a little square box at the
end of each, what Windows produces for characters that don't have a
glyph in the font in use.

But even in the days of CP/M 80 and MS-DOS 2.x, if a file was opened
in text mode, the C stdio library never let the program see the 0x1a,
it returned EOF when that octet was read.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
vishnu
Guest
Posts: n/a
 
      01-05-2006
1. The logic of the program
getchar () have been used here . whenever a cprogram starts three
shandard files are opened named stdin , stdout , and stderr . if u dont
specify any file as input to the program :
c:\program.exe <file1.txt
then it will asuume stdin standard stream attached to the keyboard as
its input and thus getchar gets it's input from stdin i.e. keyborad and
simply outputs it till u enter end of file.
u may rum the program in tow ways.
c:\program.exe
in this case stdin is taken as input stream attached to the program and
program just prints whatever u type on the screen
2.as u have asked about EOF
EOF is defined as
#defined EOF -1
in file stdio.h
it may have different value on your systems. this constants and severl
opthers like it make c programs portable to across different plateforms
value of EOF may be diffeerent for C compilers designed forb different
plateforms like windows and unix.
Gladiator
Jan 4, 10:28 am show options

Newsgroups: comp.lang.c
From: "Gladiator" <(E-Mail Removed)> - Find messages by this author
Date: 3 Jan 2006 21:28:31 -0800
Local: Wed, Jan 4 2006 10:28 am
Subject: cannot understand the character handling Program
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

When I am trying to execute a program from "The C Programming Language"

by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.


#include <stdio.h>
main()
{
long nc;


nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc); // I think it should be %ld in
//in place of %1d
}
and there is no need for long , simple int will work since
EOF value may be out of range of charectyers and also getchar() outputs
an integer type u may have used int in place of long nc;
hope this may be of some help to you.
Go on Programming.
Bye

 
Reply With Quote
 
 
 
 
vishnu
Guest
Posts: n/a
 
      01-05-2006
1. The logic of the program
getchar () have been used here . whenever a cprogram starts three
shandard files are opened named stdin , stdout , and stderr . if u dont
specify any file as input to the program :
c:\program.exe <file1.txt
then it will asuume stdin standard stream attached to the keyboard as
its input and thus getchar gets it's input from stdin i.e. keyborad and
simply outputs it till u enter end of file.
u may rum the program in tow ways.
c:\program.exe
in this case stdin is taken as input stream attached to the program and
program just prints whatever u type on the screen
2.as u have asked about EOF
EOF is defined as
#defined EOF -1
in file stdio.h
it may have different value on your systems. this constants and severl
opthers like it make c programs portable to across different plateforms
value of EOF may be diffeerent for C compilers designed forb different
plateforms like windows and unix.
Gladiator
Jan 4, 10:28 am show options

Newsgroups: comp.lang.c
From: "Gladiator" <(E-Mail Removed)> - Find messages by this author
Date: 3 Jan 2006 21:28:31 -0800
Local: Wed, Jan 4 2006 10:28 am
Subject: cannot understand the character handling Program
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

When I am trying to execute a program from "The C Programming Language"

by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.


#include <stdio.h>
main()
{
long nc;


nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc); // I think it should be %ld in
//in place of %1d
}
and there is no need for long , simple int will work since
EOF value may be out of range of charectyers and also getchar() outputs
an integer type u may have used int in place of long nc;
hope this may be of some help to you.
Go on Programming.
Bye

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-05-2006
vishnu said:

> 1. The logic of the program
> getchar () have been used here . whenever a cprogram starts three
> shandard files are opened named stdin , stdout , and stderr . if u dont
> specify any file as input to the program :
> c:\program.exe <file1.txt


The C Standard does not guarantee that this is the right way to specify
input from a file. Indeed, your command line shows severe signs of
non-portability. For example, it certainly doesn't work on my machine.

> then it will asuume stdin standard stream attached to the keyboard as
> its input and thus getchar gets it's input from stdin i.e. keyborad and
> simply outputs it till u enter end of file.


The C Standard does not guarantee that a keyboard is attached to the system.
Nor does it dictate which device is attached to stdin.

> u may rum the program in tow ways.
> c:\program.exe


This doesn't work on my machine. What makes you think it'll work on the OP's
machine?

> in this case stdin is taken as input stream attached to the program and
> program just prints whatever u type on the screen
> 2.as u have asked about EOF
> EOF is defined as
> #defined EOF -1


No, it's not. Firstly, the above is a syntax error. Secondly, the true
definition of EOF in language terms is: "a negative integral constant
expression that is returned by several functions to indicate end-of-file
,that is, no more input from a stream;"

So it might, or might not, be -1.

> it may have different value on your systems.


Right. So there wasn't much point in giving a value of -1, was there? Still,
full marks for pointing this out. We can, however, state categorically that
its value is /negative/.

> long nc;
>
>
> nc = 0;
> while(getchar() != EOF)
> ++nc;
> printf("%1d\n",nc); // I think it should be %ld in
> //in place of %1d


Please don't use // when you mean /* */

Yes, you are correct; it should be %ld rather than %1d ("ell" rather than
"one") - better still would be to change nc to unsigned long and use %lu.

> and there is no need for long , simple int will work since


You cannot guarantee that.

> EOF value may be out of range of charectyers and also getchar() outputs
> an integer type u may have used int in place of long nc;


You have misunderstood the reason long int was used. It is not storing the
result of getchar(). It is being used as a counter.

--
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)
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-05-2006
"Old Wolf" writes:

> osmium wrote:
>
>> "Keith Thompson" writes:
>>
>>> "Chuck F. " <(E-Mail Removed)> writes:
>>> [...]
>>>> EOF is a macro, which you loaded when you #included <stdio.h>. You
>>>> don't care what the value is, just that it is unique, and different
>>>> from any possible char value.
>>>>
>>>> Notice that the return from getchar() is only tested, never stored
>>>> (in this program). It is of type int, which allows it to take on
>>>> values outside the range of char, such as EOF.
>>>
>>> A quibble: EOF commonly is in the range of char on systems where plain
>>> char is signed. getchar() returns the next character as an unsigned
>>> char converted to an int; EOF, since it's negative, is guaranteed to
>>> be outside the range of unsigned char.

>>
>> And what exactly, is the quibble? He said it was an int. Are you saying
>> he
>> was wrong?

>
> CBF said "values outside the range of char, such as EOF".
> Keith's quibble was that EOF is actually inside the range of char
> on many (most?) systems, including the OP's system.


He said *allows* it to take on. Allowing something and doing something are
two different things. But I think you found the point he was trying to
make, thanks.


 
Reply With Quote
 
pank
Guest
Posts: n/a
 
      01-05-2006
hye
i have read your mail the coding which you have given insted of
this if you use following
code it will run properly.
#include <stdio.h>
main()
{
char ch;
long nc = 0;
while((ch=getchar())!= EOF)
++nc;
printf("%ld\n",nc);
}

#include <stdio.h>
main()
{
char ch;
long nc = 0;
while((ch=getchar())!= EOF)
++nc;
printf("%ld\n",nc);
}

now i will try to explain EOF first
EOF is a special macro define in stdio.h it is used to represent the
end of file.
its value is ^z which can be obtain on output screen by pressing (ctrl
Z).
EOF is the special character used in file handelling.Using file
handelling we can read any file
saved on our system.Now while reading the file how we will come to
know that we have reached to the end of file? for that purpose EOF is
used
I think this not much detail
i will reply again.

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      01-05-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pank wrote:
> hye
> i have read your mail the coding which you have given insted of
> this if you use following
> code it will run properly.
> #include <stdio.h>
> main()
> {
> char ch;
> long nc = 0;
> while((ch=getchar())!= EOF)
> ++nc;
> printf("%ld\n",nc);
> }
>
> #include <stdio.h>
> main()
> {
> char ch;
> long nc = 0;
> while((ch=getchar())!= EOF)
> ++nc;
> printf("%ld\n",nc);
> }
>
> now i will try to explain EOF first
> EOF is a special macro define in stdio.h it is used to represent the
> end of file.
> its value is ^z which can be obtain on output screen by pressing (ctrl
> Z).


Bzzzt. Wrong.

EOF is a condition, not a value.

The /condition/ is that there is no more data to be retrieved from the input stream.

The /condition/ can be initiated by various means, all of which are platform
dependant.

On MSDOSish systems, the input of a <ctrl>Z often tells the system to signal the
/condition/ of no more data (EOF) to programs reading the file or interactive
stream on which the <ctrl>Z is entered. This is not the only way the condition
is initiated; when reading a binary stream from file, MSDOSish systems signal
the End_Of_File condition when there is no more physical data to be read (the
read position exceeds the number of bytes in the file).

On Unixish systems, the input of the 'eof' character (user defined) on an
interactive device causes the OS to signal to readers that there is no more
data. OTOH, there is no corresponding characters for file streams (text or
binary); end-of-file is established by the exhaustion of data on the media, and
the OS signals End_Of_File to the readers.

In all cases, the C runtime is responsible for interpreting the reported
End_Of_File condition and presenting the EOF to the program code.

So, lets recap.

EOF represents a condition which is detected by the OS (by whatever OS dependant
means) and reported to the program by way of the C runtime.

There are various means to invoke the condition, but they are platform
dependant. Some means include entering <ctrl>Z <enter> as the first characters
of a new line (MSDOSish systems) or entering <ctrl>Z as a byte in a text stream
(MSDOSish systems), or by entering a user definable character as a character in
an interactive stream (Unixish systems), or by physically exhausting the data
source (MSDOSish systems, Unixish systems).

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDvU4xagVFX4UWr64RAhb8AJ9PnRP1QvIeJLZbTGxGiE Zx1xlzCwCg4C5X
DWedZSJ7F01Lm/TaX5A0dJc=
=GeFQ
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
Lew Pitcher <(E-Mail Removed)> writes:
> pank wrote:

[...]
>> EOF is a special macro define in stdio.h it is used to represent the
>> end of file.
>> its value is ^z which can be obtain on output screen by pressing (ctrl
>> Z).

>
> Bzzzt. Wrong.
>
> EOF is a condition, not a value.


Um, not quite. End-of-file is a condition. EOF is a macro which
expands to an expression whose value is used to indicate the
end-of-file condition (it can also indicate an error condition).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I cannot understand why an object is created this way Shawn Java 1 09-28-2006 06:57 PM
Trying to understand character arrays. drM C Programming 20 04-06-2006 10:10 AM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Don't understand wxPython event handling Robert Python 3 04-01-2004 10:46 PM
cannot understand strrev implementation tuchka C Programming 7 11-10-2003 12:41 AM



Advertisments