473,578 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python equivalent for fputc

I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

So what kind of call can I make to do it in Python?

Aug 31 '06 #1
4 3126
Putty wrote:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.
So what kind of call can I make to do it in Python?
There may be some ways. Here is the data:
from random import randint
vals = [randint(0, 255) for i in xrange(10)]

This is probably the most common way:
print "".join(chr (c) for c in vals)

If you need to access the chars one after the other, this may be a
(quite slower, but less memory consuming) alternative:

import sys
write = sys.stdout.writ e
for c in vals:
write(chr(c))
print

In some cases a solution like this one can be useful too:

import array
print array.array("B" , vals).tostring( )

Bye,
bearophile

Aug 31 '06 #2

Putty wrote:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.
C is a low-level language -- a character *is* a number :-)
>
So what kind of call can I make to do it in Python?
*Guessing* that you mean e.g. fputc(97, f) writes the character 'a' to
the file whose handle is f ...

In Python the more-or-less literal translation (ignoring the fputc
return value) would be f.write(chr(97) )

Note:
|>>ord('a')
97
|>>chr(97)
'a'

HTH -- if not, show us (relevant parts of) the actual source that you
are porting.

Cheers,
John

Aug 31 '06 #3
Thanks, John. That's exactly what I was looking for!

John Machin wrote:
Putty wrote:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

C is a low-level language -- a character *is* a number :-)

So what kind of call can I make to do it in Python?

*Guessing* that you mean e.g. fputc(97, f) writes the character 'a' to
the file whose handle is f ...

In Python the more-or-less literal translation (ignoring the fputc
return value) would be f.write(chr(97) )

Note:
|>>ord('a')
97
|>>chr(97)
'a'

HTH -- if not, show us (relevant parts of) the actual source that you
are porting.

Cheers,
John
Aug 31 '06 #4
Putty wrote:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.
that's what fputc does, of course -- it takes an integer and writes
it as a character to the given stream.

f = fopen(filename, "w");
fputc(c, f);
fclose(f)

becomes

f = open(filename, "w")
f.write(chr(val ue))
f.close()

</F>

Aug 31 '06 #5

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

Similar topics

54
6535
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
699
33646
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
145
6206
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
4
4319
by: Summu82 | last post by:
Hi i have to write some data in a binary file fputc(0x22,f2); this works fine also int data =0x22; fputc(0x22,f2);
26
3210
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I just came across a funny example in which the opposite is the case. The following is a binary search algorithm in Java. It searches a value in an...
20
7270
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output stream so that the tape driver will write a filemark on the tape. Otherwise multiple clumps of data saved to tape would all appear to be one big...
852
28223
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
3
4309
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
When I execute the following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *bmp; bmp = fopen("deneme.bmp","w"); fputc(10,bmp);
3
4640
by: ocli5568 | last post by:
My program is an 'attempt' at steganography, and i've written what i hope to be correct for the most part, although i'm having a lot of trouble with copying the picture input to picture output verbatim. while (bytes > 0) { int c = fgetc(fin); fputc(c, fout);b bytes--; } it's, quite obviously, a simple while loop that occurs...
0
7850
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...
0
7779
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...
0
8126
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. ...
1
7874
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...
1
5664
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...
0
3786
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...
0
3801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2293
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
1
1391
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.