473,385 Members | 1,742 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,385 software developers and data experts.

write eof without closing

hello

can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

grts,
ruben
Aug 19 '06 #1
14 17046
In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something
What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch
Aug 19 '06 #2
On 2006-08-19, cage <ca********************@gmail.comwrote:
can i write a eof to a file descriptor without closing it?
No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.

--
Grant Edwards
gr****@visi.com

Aug 19 '06 #3
Marc 'BlackJack' Rintsch schreef:
In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
>can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch
ok let me explain this a bit more...
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)

grts,
ruben
Aug 19 '06 #4
>can i write a eof to a file descriptor without closing it?
>
No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.
Common Dos/Window convention also uses ctrl+Z (0x1a) for EOF.

c:\copy con test.txt
hello
^Z
c:\>

*nix usually uses ctrl+D (0x04) as an EOF signal...and OS-X being
Unixish also uses the same.

bash$ cat test.txt
hello
^D
bash$

Don't know about Macs earlier than OS-X.

I don't know if there are problems (triggering an actual EOF and
closing the file) writing either of these ascii characters in
ascii/text mode (may vary between platforms?), but there are no
problems writing either of these characters in binary mode.

-tkc

Aug 19 '06 #5
cage wrote:
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)
You might want to check the pty module in the Standard Library, and/or
Pexpect (http://pexpect.sf.net/)
Aug 19 '06 #6
cage schrieb:
Marc 'BlackJack' Rintsch schreef:
>In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
>>can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch

ok let me explain this a bit more...
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)
According to wikipedia (german version, but I bet you can get that info
using the english one, too) C-d sends EOT - end of transmission. Which
is ascii 0x04.

So I suggest you try writing

"\x04"

to the pipe. Maybe that works.

Diez
Aug 19 '06 #7
Writing the binary value for ^D into the stream will not do anything.
That value signals the shell to close the stream, as such it only has
significance when you're typing something into the shell.

To the OP: writing an EOF to a stream without closing it makes no
sense. EOF means just that--end of file. Once the program reaches an
EOF, it can't read any more values from the stream, so keeping it open
to write more stuff into it wouldn't even work.

Aug 19 '06 #8
Actually, nevermind. It appears that receiving an EOF from a stream
tells it when to stop 'reading', not necessarily that the stream is
closed. What a weird behavior.

Aug 19 '06 #9
cage wrote:
hello

can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

grts,
ruben
No but there is an EOF to the file anyway, even if it is open.

I recall under MS-DOS, you could create a file of size N without writing to
it (some INT21 or 9 ? call to modify the FAT) ... not really possible
anymore.

Philippe
Aug 19 '06 #10
cage wrote:
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
have you tried *flushing* the output stream after each command?

</F>

Aug 20 '06 #11
On 2006-08-19, Tim Chase <py*********@tim.thechases.comwrote:
>>can i write a eof to a file descriptor without closing it?

No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.

Common Dos/Window convention also uses ctrl+Z (0x1a) for EOF.

c:\copy con test.txt
hello
^Z
c:\>
IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.
*nix usually uses ctrl+D (0x04) as an EOF signal...and OS-X
being Unixish also uses the same.

bash$ cat test.txt
hello
^D
bash$
That's just the tty line-discipline layer of the tty driver.
When it sees Ctrl-D as the first thing on a "line", it closes
the file descriptor causing the client to see an EOF. That
feature in the line-discipline layer can be disabled or
configured to use any other character.

I suspect what the OP really needs to do is write a newline and
then flush the stream.

--
Grant Edwards grante Yow! I'm pretending that
at we're all watching PHIL
visi.com SILVERS instead of RICARDO
MONTALBAN!
Aug 21 '06 #12
Grant Edwards <gr****@visi.comwrote:
...
IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.
Easy to test, if you have Windows:
>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file" -- a convention going back
to CP/M (which lacked metadata to represent file length except in
multiples of 256 characters, if I recall correctly) and is still
followed by Windows (and by Python running on Windows).

Nevertheless I doubt it would help the original poster -- I think, like
/F and you, that a line-end and flush may be what he needs.
Alex
Aug 21 '06 #13
Alex Martelli wrote:
>IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.

Easy to test, if you have Windows:
>>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file"
your test doesn't match the OP's example, though, which used control-Z
to signal end of file when reading from the console:
>copy con test.txt
hello
^Z
1 file(s) copied.

that control-Z works in the same way as control-D on Unix, and no EOF
character is copied to the file:
>python -c "print repr(open('test.txt', 'rb').read())"
'hello\r\n'

</F>

Aug 21 '06 #14
On 2006-08-21, Alex Martelli <al***@mac.comwrote:
Grant Edwards <gr****@visi.comwrote:
...
>IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.

Easy to test, if you have Windows:
I might, but I won't admit it in public. :)
>>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file" -- a convention going back
to CP/M (which lacked metadata to represent file length except in
multiples of 256 characters, if I recall correctly)
That's correct.
and is still followed by Windows (and by Python running on
Windows).
Very interesting. I thought that windows had abandoned that. I
remember having problems under DOS/Windows caused by an old
text editor that put a ctrl-Z at the end of the file --
probably a result of the other programs reading the file in
binary mode and seeing the ctrl-Z.
Nevertheless I doubt it would help the original poster -- I
think, like /F and you, that a line-end and flush may be what
he needs.
--
Grant Edwards grante Yow! Am I elected yet?
at
visi.com
Aug 21 '06 #15

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

Similar topics

2
by: Amy G | last post by:
I am looking to make this code a little "nicer"... any suggestions??? I want to do a "read+" where I would be able to first read the contents of the file... then either close the file out or write...
4
by: Dennis M. Marks | last post by:
I generate a SELECT list dynamically by taking items from a table. I have DOCUMENT.WRITE statements that write a combination of literals and variables including < and > characters. The HTML seems...
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
12
by: Sean | last post by:
Hi, I have the following script: ----------------------------------------------------------------------------------- <script type="text/javaScript"> <!-- document.write('<div...
5
by: nick_faye | last post by:
Hi, I am still a newbie to VB and using MS Access 2000. I am currently trying to provide a preview of reports before printing them. My program is simple: AC.DoCmd.OpenReport "MyReport",...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
4
by: Paul | last post by:
Hi all, I've built a custom control that won't contain any inner content. If I derive the control from Control, then the VS editor will always add the closing tag when I use the control. I can...
2
by: Luqman | last post by:
How can I open another WebForm in ASP.Net / VS 2005 without closing the Current Webform. I tried using Server.Transfer("Form2.Aspx",True) on Button_Click of Form1 but it close the current web...
10
by: cjard | last post by:
I have a client and server that enjoy the following simple dialogue: Client connects Client sends request Server sends response Client disconnects This is the way it must be. The response...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.