473,806 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6160
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******@fakei sp.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.l earn.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******@fakei sp.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****@rwaltma n.net> wrote in
news:op******** *************** *********@4ax.c om:
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=87e hreli?= <ac******@yahoo .com> wrote in
news:dg******** **@domitilla.ai oe.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.l earn.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*******@aust in.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
1383
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 thus can't be changed to do so. The program has to be invoked 'program input.file output.file'. I would like to do something like 'otherprog | program stdin stdout | anotherprog' to take advantage of of the multiprocessor environment I'm on. ...
1
2303
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 engine is only able to send and the controller recieve. Also, this only works when using popen2.popen3. Using os.popen3 doesn't work at all and seems to behave completely differently. What could be causing these problems? Any help is greatly...
6
2522
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 write the python commands right onto the command line. It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
2
4549
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 the function due to its
7
3710
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 filestream. If ANYONE could offer ANY suggestion as to why this is happening it would be much appreciated. Thanks in advance!
6
5281
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 characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
5
2424
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 are unavailabe (because Windows Firewall hold them). I think I will use pipe.
3
16004
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 wrong. Its supposed to work as follows: main shell forks off a child shell, the child shell sets up the pipes and forks off n-1 other shells (where n is the total number of commands). Each child shell sets its pipe file descriptors then execs to...
0
1362
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 server-side component (written in C) that 'listens' for PHP requests (via its stdin) and spits out the appropriate response (via its stdout). Conversely, the PHP side uses fwrite() to send to the component's stdin and fread() to read from the component's...
0
9597
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
10620
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
10369
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
10372
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
10110
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
6877
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
5546
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...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
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.