尝试将新用户添加到文本文件的新行中,但我的所有行均已被修改。
这是我的文本文件:
乔·W·约翰·阿斯杜夫
阿诺德·K·布纳德斯(1234)
玛丽莎(Marissa G)
#!/usr/bin/perl
open(FILE, "+<test.txt") or die "Couldn't open file $!";
if ($user eq $fields[1]) {
print "Taken";
elsif ($user ne $fields[1]) {
print FILE "$user";
}
}
close(FILE);
当我运行此文件时,我的文本文件更改为
乔·W·约翰·阿斯杜夫
测试K,bnads,1234
测试G,mglav,通过
测试
将用户添加到文件末尾(仅当尚未使用时),
my $taken = 0;
while (my $line = <FILE>) {
chomp $line;
# splits each line into three fields
my @fields = split "," , $line;
if ($user eq $fields[1]) {
print "Username is taken.\n";
$taken = 1;
last;
}
}
if (!$taken) {
print FILE "$user\n";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句