473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with EOF when using pipes and stdin

Hello all,

I have maybe a trivial question, but I cannot think out what is wrong :(
How do i detect EOF correctly when i read from stdin? Am I doing it
wrong?

<pipetest.c>
#include <stdio.h>
int main(void) {
char ch;
while (!feof(stdin)) { // <-- EOF appears to be true when character 0x1A is on stdin
ch=getchar(); // <-- variable ch has value of 0xFF when 0x1A was on stdin
putchar(ch);
}
return(0);
}

<256.tmp>
contains values from 0 to 255

I compiled and run from cmd prompt: pipetest.exe < 256.tmp > out.tmp

<out.tmp>
00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0E 0F
00000010 10 11 12 13 14 15 16 17 18 19 FF

Why the program ended on 0x1A character and outputted FF instead?

How do I correctly write program which can be used with pipes? I use
free borland turbo c++ 1.01 compiler on windows xp.

Thanks anyone for kind help,
Y.
Sep 14 '05 #1
7 6110
Now I know I need to read and write files in binary mode. I know how to open file on disk in binary mode,
but how do i open stdin and stdout in binary? (AFAIK these files are yet opened)

Thanks anyone for kind help,
Yandos
Sep 14 '05 #2
"Yandos" <fa******@fakeisp.cz> wrote in message
news:43**********@x-privat.org...
How do i detect EOF correctly when i read from stdin? Am I doing it
wrong?
As apparent from another post of yours, I think you are learning C, not C++.
You may find better answers at comp.lang.c and/or alt.comp.lang.learn.c-c++
<pipetest.c>
#include <stdio.h>
int main(void) {
char ch;
while (!feof(stdin)) { // <-- EOF appears to be true when
character 0x1A is on stdin
ch=getchar(); // <-- variable ch has value of 0xFF when
0x1A was on stdin
putchar(ch);
getchar actually returns int. What you are doing is truncating that value
and outputting something else.
}
return(0);
}

<256.tmp>
contains values from 0 to 255

I compiled and run from cmd prompt: pipetest.exe < 256.tmp > out.tmp
<out.tmp>
00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0E 0F
00000010 10 11 12 13 14 15 16 17 18 19 FF

Why the program ended on 0x1A character and outputted FF instead?
I don't know... :(
How do I correctly write program which can be used with pipes? I use
free borland turbo c++ 1.01 compiler on windows xp.


You are using a compiler that was released in 1991! :)

You should consider getting a more modern compiler. For the Windows
environment; there is a free one from Microsoft, a free one from Borland,
and a very nice free development environment named Dev-C++ that which uses
gcc as the compiler.

Ali

Sep 14 '05 #3
Yandos wrote:
Hello all,

I have maybe a trivial question, but I cannot think out what is wrong :(
How do i detect EOF correctly when i read from stdin? Am I doing it
wrong?

<pipetest.c>
#include <stdio.h>
int main(void) {
char ch; Bzzzt. make that:
int ch; /* since EOF, by definition is a value *not* representable
by char (this is why getchar returns int) */
while (!feof(stdin)) { // <-- EOF appears to be true when character 0x1A is on stdin
Bzzzt. feof() will return true only *after* a read attempt returns EOF
(this ain't Pascal).
ch=getchar(); // <-- variable ch has value of 0xFF when 0x1A was on stdin
putchar(ch);
}
return(0);
}

<256.tmp>
contains values from 0 to 255

I compiled and run from cmd prompt: pipetest.exe < 256.tmp > out.tmp

<out.tmp>
00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0E 0F
00000010 10 11 12 13 14 15 16 17 18 19 FF

Why the program ended on 0x1A character and outputted FF instead?

How do I correctly write program which can be used with pipes? I use
free borland turbo c++ 1.01 compiler on windows xp.
*That* is off topic here; pipes are not part of standard C++.

I recommend that you see the C FAQ, as these are functions (getchar(),
etc) from the C library inherited into C++.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Sep 14 '05 #4
On 14 Sep 2005 21:50:22 +0200, Yandos <fa******@fakeisp.cz> wrote:
Hello all,

I have maybe a trivial question, but I cannot think out what is wrong :(
How do i detect EOF correctly when i read from stdin? Am I doing it
wrong?

<pipetest.c>
#include <stdio.h>
int main(void) {
char ch;
while (!feof(stdin)) { // <-- EOF appears to be true when character 0x1A is on stdin
ch=getchar(); // <-- variable ch has value of 0xFF when 0x1A was on stdin
putchar(ch);
}
return(0);
}

<256.tmp>
contains values from 0 to 255

I compiled and run from cmd prompt: pipetest.exe < 256.tmp > out.tmp

<out.tmp>
00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0E 0F
00000010 10 11 12 13 14 15 16 17 18 19 FF

Why the program ended on 0x1A character and outputted FF instead?

How do I correctly write program which can be used with pipes? I use
free borland turbo c++ 1.01 compiler on windows xp.

Thanks anyone for kind help,
Y.


First of all, both questions you posted were centered on the
(mis)behavior of a C, (not C++,) program. If you have C questions
you'll get a better responses and more patience in a C related group.

Now to your question: Borland's Turbo C++ 1.0 is an old compiler,
(I believe I bought it in 1990?) written at a time were compatibility
with MS-DOS (plain DOS, not Windows) was important. Many details of
the run time library ensure compatibility with data files created by
other DOS programs, many of which tried to be compatible with files
created under DOS, CPM-86 and CPM-80.

All these last three operating systems use a CONTROL-Z character
(0x1A) as an end-of-file marker in text files. The (historic) reason
being that CPM-80 kept track of file sizes only as the number of 128
byte blocks. To know exactly where a text file ended in the last
block, the convention of using CTRL-Z (a non-printable character) was
established.

So your program stops reading when it receives an indication it
reached the end of file. The 0xFF following it is an error code
supplied when you try to read from a file beyond the logical
end-of-file.

Also note that before reaching CTRL-Z, the newline char (0x0A) was
expanded into carriage return - newline (0x0D 0x0A) and that the
carriage return following it was discarded. Opening the file/pipe in
binary mode would correct all these problems

I am sure there must be a way of modifying your program to tell BC++
1.0 to use binary mode, but it would be a waste of time. You can get
other (more modern) free compilers, and your time would be better
spent learning to do this on LCC-Win32, Digital Mars C/C++, GCC, or
even Microsoft compilers. (They are free, not Visual Studio, but the
underlying compilers.)
Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
Sep 14 '05 #5
Roberto Waltman <us****@rwaltman.net> wrote in
news:op********************************@4ax.com:
First of all, both questions you posted were centered on the
(mis)behavior of a C, (not C++,) program. If you have C questions
you'll get a better responses and more patience in a C related group.

Right. I don't know what's the difference :( I thought i have c++ compiler, so best place is to ask here :)
Simple and stupid, sorry :( I will read more about that difference and try to figure right group next time.
Now to your question: Borland's Turbo C++ 1.0 is an old compiler,
(I believe I bought it in 1990?) written at a time were compatibility
with MS-DOS (plain DOS, not Windows) was important. Many details of
the run time library ensure compatibility with data files created by
other DOS programs, many of which tried to be compatible with files
created under DOS, CPM-86 and CPM-80.

All these last three operating systems use a CONTROL-Z character
(0x1A) as an end-of-file marker in text files. The (historic) reason
being that CPM-80 kept track of file sizes only as the number of 128
byte blocks. To know exactly where a text file ended in the last
block, the convention of using CTRL-Z (a non-printable character) was
established.

So your program stops reading when it receives an indication it
reached the end of file. The 0xFF following it is an error code
supplied when you try to read from a file beyond the logical
end-of-file.

Also note that before reaching CTRL-Z, the newline char (0x0A) was
expanded into carriage return - newline (0x0D 0x0A) and that the
carriage return following it was discarded. Opening the file/pipe in
binary mode would correct all these problems

Damn, how do you know all this? :) This is very interesting. Now I unerstand...
I am sure there must be a way of modifying your program to tell BC++
1.0 to use binary mode, but it would be a waste of time. You can get
other (more modern) free compilers, and your time would be better
spent learning to do this on LCC-Win32, Digital Mars C/C++, GCC, or
even Microsoft compilers. (They are free, not Visual Studio, but the
underlying compilers.)
Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]


Roberto, thank you again for very useful reply. I glad you realized I'm absoulte newbie and handled me
with care :) I have learned a lot from your words. The reason why i'm starting with old borland compiler
is that i knew it is free and i don't yet know the syntax of c and compiler is helping me a lot to correct
"grammar" :) (I have learned pascal before) One big thank you! ;)

Y.
Sep 14 '05 #6
=?utf-8?Q?Ali_=C3=87ehreli?= <ac******@yahoo.com> wrote in
news:dg**********@domitilla.aioe.org:
As apparent from another post of yours, I think you are learning C,
not C++. You may find better answers at comp.lang.c and/or
alt.comp.lang.learn.c-c++
Right, thank you for hint. I will read more about the difference and next time try to figure the correct
group ;)
getchar actually returns int. What you are doing is truncating that
value and outputting something else.
IC, thanks. Anyway, the result is still the same, no matter if i use int or char. But I will keep that in mind.
You are using a compiler that was released in 1991! :)

You should consider getting a more modern compiler. For the Windows
environment; there is a free one from Microsoft, a free one from
Borland, and a very nice free development environment named Dev-C++
that which uses gcc as the compiler.

Ali


Thank you again for hint, i will try it. I have started with borland c++ because i have learned turbo
pascal before, and knew that borland had released c++ 1.01 for free use - and i needed some "gui"
because i need a lot of help with syntax.

Y.
Sep 14 '05 #7
Artie Gold <ar*******@austin.rr.com> wrote in
news:3o************@individual.net:
Yandos wrote:
Hello all,

I have maybe a trivial question, but I cannot think out what is wrong
:( How do i detect EOF correctly when i read from stdin? Am I doing
it wrong?

<pipetest.c>
#include <stdio.h>
int main(void) {
char ch; Bzzzt. make that:
int ch; /* since EOF, by definition is a value *not*
representable
by char (this is why getchar returns int) */


IC, thanks :) But on the result there's no difference, but i will now be more careful on data types.
Pascal would have warned me...
while (!feof(stdin)) { // <-- EOF appears to be true when
character 0x1A is on stdin
Bzzzt. feof() will return true only *after* a read attempt returns EOF
(this ain't Pascal).


Not really. Also when 0x1A is received, because stdin is opened in text mode by default in turbo c++
1.01 because of compatibility reasons (Roberto right explained that in different thread)
How do I correctly write program which can be used with pipes? I use
free borland turbo c++ 1.01 compiler on windows xp.


*That* is off topic here; pipes are not part of standard C++.


IC, sorry.

I recommend that you see the C FAQ, as these are functions (getchar(),
etc) from the C library inherited into C++.

HTH,
--ag


Thank you for help, I have a lot to learn yet :) And apologies for off topic...
Y.
Sep 14 '05 #8

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

Similar topics

1
by: | last post by:
Does anyone know of a way I can make a program read/write from stdin/stdout so that I can chain it with other commands and interact with it from python? Unfortunately it's third party software and...
1
by: Chris S. | last post by:
A wrote a small class to handle IO through pipes, but the connection only seems to work in one direction. The following code defines connection.py, engine.py, and controller.py. Once connected, the...
6
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
6
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input...
5
by: Dara Durum | last post by:
Hi ! I want to create a Process Pool Object. I can hold started processes, and can communicate with them. I tryed with many ipc methods, but every of them have bug or other problem. Sockets...
3
by: ZhukovL | last post by:
I'm having some trouble implementing the handling of multiple pipes in a shell I'm writing. I was hoping someone could point me in the right direction because I really cant see where I'm going...
0
by: Daniel Klein | last post by:
Without getting into a lot of unnecessary detail, I'm working on a project to allow PHP to communicate with a proprietary database. The IPC mechanism I'm using is 'proc_open'; there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.