免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2198 | 回复: 3

向文件中添加记录问题 [复制链接]

论坛徽章:
0
发表于 2007-03-03 17:47 |显示全部楼层
向一个文件添加学生记录,包括id,password
现在遇到的问题是:
如果文件中存在记录的话,不能往里面写空记录;但是如果文件本身就是空白的,这时候往里面添加
空白记录竟然成功了,难道每个新创建的文件开头都存在一个空白符?应该怎么去除啊?


  1. my ( $id, $pwd );    #需要添加的用户id和密码
  2. my $list;
  3. open(AD, "+<userInfo.csv") || die "cannot access file userInfo.csv:$!"
  4. while( <AD> )
  5. {
  6.     if( $id eq '' || $pwd eq '' )
  7.    {
  8.         close(AD);
  9.    }
  10.     $list .= join( "," $id, $pwd )."\n";
  11.     print AD $list;

  12.     close(AD);
  13. }   
复制代码

论坛徽章:
0
发表于 2007-03-03 19:03 |显示全部楼层
你再check下吧.
1) 逻辑有问题;
2) 打开文件的方式不对.

论坛徽章:
0
发表于 2007-03-05 09:19 |显示全部楼层
麻烦说具体点吧,谢谢~~

论坛徽章:
0
发表于 2007-03-05 19:49 |显示全部楼层

回复 3楼 ubac 的帖子

Thinking in logic:
1. create a new file (empty) if not exists.
    open F ">user.txt" or die $!;
    # insert first or initial records
    close F;
2. reading  this created file
3. processing data (old and new), for data uniqu, using hash is one of a choice.
4. appending new records to existed file, using open F, ">>user.txt" .

see
    perldoc -f open
    perldoc perlopentut
will more help you.   

-- ulmer
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP