Python论坛  - 讨论区

标题:[python-chinese] 第一帖.请教怎么实现像C语言里的结构?

2007年06月19日 星期二 20:25

橙子 eyeonme在gmail.com
星期二 六月 19 20:25:02 HKT 2007

CÊÇÕâÑùµÄ.

struct  name_info
{
     char  _name[64];
     int    _idx;
}

//´ò¿ªÒ»¸öÎļþ  _file;
struct  name_info  _name;
for(char i='a'; i<'z'; i++)
{
    _name._name[0] = i;
    _name._idx = 0;

    fwrite(_file, (char*)&_name, sizeof(struct  name_info));
}
//¹Ø±ÕÎļþ  _file;

³õѧPython, Õæ²»ÖªµÀÕâ¶Î´úÂëÓà pythonÔõôд£¬Çë´Í½Ì.
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070619/6ae27f06/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月19日 星期二 20:32

Ranger.Lee uraka.lee在gmail.com
星期二 六月 19 20:32:29 HKT 2007

CÖеĽṹÔÚC++ÖпÉÒÔÓÃÀàÀ´ÃèÊö£¬PythonÖÐ×ÔȻҲ¿ÉÒÔ¡£

On 6/19/07, ³È×Ó <eyeonme在gmail.com> wrote:
>
> CÊÇÕâÑùµÄ.
>
> struct  name_info
> {
>      char  _name[64];
>      int    _idx;
> }
>
> //´ò¿ªÒ»¸öÎļþ  _file;
> struct  name_info  _name;
> for(char i='a'; i<'z'; i++)
> {
>     _name._name[0] = i;
>     _name._idx = 0;
>
>     fwrite(_file, (char*)&_name, sizeof(struct  name_info));
> }
> //¹Ø±ÕÎļþ  _file;
>
> ³õѧPython, Õæ²»ÖªµÀÕâ¶Î´úÂëÓà pythonÔõôд£¬Çë´Í½Ì.
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Uraka.Lee @ School of Software and Micro-electronic, Peking University,
China
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070619/45c1005b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月19日 星期二 21:09

huanghao.c huanghao.c在gmail.com
星期二 六月 19 21:09:11 HKT 2007

你的 fwrite() 错了吧

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE
*stream);

python 的话, 你可以试试 file object, 但我不确定它是否能用于 r/w binary file.


橙子 写道:
> C是这样的.
>
> struct name_info
> {
> char _name[64];
> int _idx;
> }
>
> //打开一个文件 _file;
> struct name_info _name;
> for(char i='a'; i<'z'; i++)
> {
> _name._name[0] = i;
> _name._idx = 0;
>
> fwrite(_file, (char*)&_name, sizeof(struct name_info));
> }
> //关闭文件 _file;
>
> 初学Python, 真不知道这段代码用 python怎么写,请赐教.
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月19日 星期二 21:13

大郎 iexper在gmail.com
星期二 六月 19 21:13:04 HKT 2007

file("...", "rw")
it is ok


On 6/19/07, huanghao.c <huanghao.c在gmail.com> wrote:
>
> ÄãµÄ fwrite() ´íÁË°É
>
> size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE
> *stream);
>
> python µÄ»°, Äã¿ÉÒÔÊÔÊÔ file object, µ«ÎÒ²»È·¶¨ËüÊÇ·ñÄÜÓÃÓÚ r/w binary file.
>
>
> ³È×Ó Ð´µÀ:
> > CÊÇÕâÑùµÄ.
> >
> > struct name_info
> > {
> > char _name[64];
> > int _idx;
> > }
> >
> > //´ò¿ªÒ»¸öÎļþ _file;
> > struct name_info _name;
> > for(char i='a'; i<'z'; i++)
> > {
> > _name._name[0] = i;
> > _name._idx = 0;
> >
> > fwrite(_file, (char*)&_name, sizeof(struct name_info));
> > }
> > //¹Ø±ÕÎļþ _file;
> >
> > ³õѧPython, Õæ²»ÖªµÀÕâ¶Î´úÂëÓà pythonÔõôд£¬Çë´Í½Ì.
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
ÎÒ×ßµ½Ò»¸öÄ°ÉúµÄµØ·½, ¸æËß±ðÈË ÎÒҪȥÁ÷ÀË
Ŷ£¬ÎÒҪȥÁÆÉË¡­¡­

Gtalk: iexper(at)gmail.com
ÓòÃû¹ýÆÚÁË
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070619/841cdcbc/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月19日 星期二 21:57

橙子 eyeonme在gmail.com
星期二 六月 19 21:57:12 HKT 2007

α´úÂë

ÔÚ07-6-19£¬´óÀÉ <iexper在gmail.com> дµÀ£º
>
> file("...", "rw")
> it is ok
>
>
> On 6/19/07, huanghao.c <huanghao.c在gmail.com> wrote:
> >
> > ÄãµÄ fwrite() ´íÁË°É
> >
> > size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE
> > *stream);
> >
> > python µÄ»°, Äã¿ÉÒÔÊÔÊÔ file object, µ«ÎÒ²»È·¶¨ËüÊÇ·ñÄÜÓÃÓÚ r/w binary file.
> >
> >
> > ³È×Ó Ð´µÀ:
> > > CÊÇÕâÑùµÄ.
> > >
> > > struct name_info
> > > {
> > > char _name[64];
> > > int _idx;
> > > }
> > >
> > > //´ò¿ªÒ»¸öÎļþ _file;
> > > struct name_info _name;
> > > for(char i='a'; i<'z'; i++)
> > > {
> > > _name._name[0] = i;
> > > _name._idx = 0;
> > >
> > > fwrite(_file, (char*)&_name, sizeof(struct name_info));
> > > }
> > > //¹Ø±ÕÎļþ _file;
> > >
> > > ³õѧPython, Õæ²»ÖªµÀÕâ¶Î´úÂëÓà pythonÔõôд£¬Çë´Í½Ì.
> > >
> > >
> > >
> > ------------------------------------------------------------------------
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> ÎÒ×ßµ½Ò»¸öÄ°ÉúµÄµØ·½, ¸æËß±ðÈË ÎÒҪȥÁ÷ÀË
> Ŷ£¬ÎÒҪȥÁÆÉË¡­¡­
>
> Gtalk: iexper(at)gmail.com
> ÓòÃû¹ýÆÚÁË
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070619/bdd108ae/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月24日 星期日 21:12

Shuning Hong hongshuning在gmail.com
星期日 六月 24 21:12:19 HKT 2007

Try this:

class name_info:
  pass

f=open(FILENAME,'wb')
f._name = 'a'
f._idx=0
f.write(name_info)
f.close()


On 6/19/07, 橙子 <eyeonme at gmail.com> wrote:
> C是这样的.
>
> struct  name_info
> {
>      char  _name[64];
>      int    _idx;
> }
>
> //打开一个文件  _file;
> struct  name_info  _name;
> for(char i='a'; i<'z'; i++)
> {
>     _name._name[0] = i;
>     _name._idx = 0;
>
>     fwrite(_file, (char*)&_name, sizeof(struct  name_info));
> }
> //关闭文件  _file;
>
> 初学Python, 真不知道这段代码用 python怎么写,请赐教.
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月26日 星期二 09:27

橙子 eyeonme在gmail.com
星期二 六月 26 09:27:43 HKT 2007

лл Shuning Hong .

»¹ÊǸоõpythonµÄÎļþ²Ù×÷²»ÊǺܷ½±ã.

Èç¹û class name_info ÊÇÒ»Ìõ¼Ç¼,ÎÒÔÚÎļþÀïÃæÒѾ­Ð´ÁË 10Ìõ¼Ç¼ÁË. ÏÖÔÚÒª¸ÄдµÚÎåÌõµÄ¼Ç¼,ÈçºÎ²Ù×÷?



2007/6/24, Shuning Hong <hongshuning在gmail.com>:
>
> Try this:
>
> class name_info:
> pass
>
> f=open(FILENAME,'wb')
> f._name = 'a'
> f._idx=0
> f.write(name_info)
> f.close()
>
>
> On 6/19/07, ³È×Ó <eyeonme在gmail.com> wrote:
> > CÊÇÕâÑùµÄ.
> >
> > struct  name_info
> > {
> >      char  _name[64];
> >      int    _idx;
> > }
> >
> > //´ò¿ªÒ»¸öÎļþ  _file;
> > struct  name_info  _name;
> > for(char i='a'; i<'z'; i++)
> > {
> >     _name._name[0] = i;
> >     _name._idx = 0;
> >
> >     fwrite(_file, (char*)&_name, sizeof(struct  name_info));
> > }
> > //¹Ø±ÕÎļþ  _file;
> >
> > ³õѧPython, Õæ²»ÖªµÀÕâ¶Î´úÂëÓà pythonÔõôд£¬Çë´Í½Ì.
> >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070626/833cc820/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月26日 星期二 09:33

limodou limodou在gmail.com
星期二 六月 26 09:33:28 HKT 2007

On 6/26/07, 橙子 <eyeonme在gmail.com> wrote:
> 谢谢 Shuning Hong .
>
> 还是感觉python的文件操作不是很方便.
>
> 如果 class name_info 是一条记录,我在文件里面已经写了 10条记录了. 现在要改写第五条的记录,如何操作?
>
python的写文件是随机存取的,而你要求的是按记录来处理的,这样一般要求每次记录是定长的,然后可以移动文件指针到指定的记录开始的位置,然后写入整条记录。

python的文件处理与C/C++是相同的。

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月26日 星期二 09:52

橙子 eyeonme在gmail.com
星期二 六月 26 09:52:46 HKT 2007

ºÇºÇ. ÊÇÎÒ¸ã´íÁË.

¸Õ²Å×Ðϸ¿´ÁËһϠopen.__doc__  .
Ô­À´ÒªÒÔ   ("r+")  µÄ·½Ê½´ò¿ªÎļþ.   "w" »áÇå¿ÕÎļþ.  "a"  Ö»»áÌí¼ÓÊý¾Ýµ½Î²²¿. ÔÚ"a"ģʽÏÂ, seek. ºÃÏñûÓÃ.

·Ç³£¸Ðл´ó¼ÒµÄ»Ø¸´. лл!


ÔÚ07-6-26£¬limodou <limodou在gmail.com> дµÀ£º
>
> On 6/26/07, ³È×Ó <eyeonme在gmail.com> wrote:
> > лл Shuning Hong .
> >
> > »¹ÊǸоõpythonµÄÎļþ²Ù×÷²»ÊǺܷ½±ã.
> >
> > Èç¹û class name_info ÊÇÒ»Ìõ¼Ç¼,ÎÒÔÚÎļþÀïÃæÒѾ­Ð´ÁË 10Ìõ¼Ç¼ÁË. ÏÖÔÚÒª¸ÄдµÚÎåÌõµÄ¼Ç¼,ÈçºÎ²Ù×÷?
> >
>
> pythonµÄдÎļþÊÇËæ»ú´æÈ¡µÄ£¬¶øÄãÒªÇóµÄÊÇ°´¼Ç¼À´´¦ÀíµÄ£¬ÕâÑùÒ»°ãÒªÇóÿ´Î¼Ç¼ÊǶ¨³¤µÄ£¬È»ºó¿ÉÒÔÒƶ¯ÎļþÖ¸Õëµ½Ö¸¶¨µÄ¼Ç¼¿ªÊ¼µÄλÖã¬È»ºóдÈëÕûÌõ¼Ç¼¡£
>
> pythonµÄÎļþ´¦ÀíÓëC/C++ÊÇÏàͬµÄ¡£
>
> --
> I like python!
> UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070626/2323d843/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号