|< 1 2 >| | 13 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
AAAAAAA¡aaaaaaa¡aaa
BBBB¡bbbb¡bbb
CCCCCC¡cccc¡ccc
DDDDDDDD¡ddddd¡dddd
EEEEE¡eeeeeeeeee¡eee
FFFFFFFF¡fffff¡ff
GGGGGG¡ggggggggggggg¡ggg
HHHHH¡hhhhh¡h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl
use warnings; use strict; use diagnostics;
use Fcntl qw(:DEFAULT :flock SEEK_SET SEEK_CUR);
sub systell { sysseek($_[0], 0, SEEK_CUR) }
sysopen FH, "datei.txt", O_RDWR or die "Kann Datei nicht öffnen: $!\n";
flock FH, LOCK_EX or die "Kann Datei nicht sperren: $!\n";
my $pos = 0;
my ($feld1, $feld2, $feld3) = "";
while (<FH>) {
chomp;
($feld1, $feld2, $feld3) = split /\xA1/;
if ($feld1 eq "EEEEE") {
sysseek FH, $pos, SEEK_SET; # möchte aktuelle Zeile überschreiben
print FH "E-E-E¡eeeeeeeeee¡eee\n"; # geänderte Zeile
};
$pos = main::systell FH; # ich möchte den Zeilenanfang treffen
}
close FH;
Quotesysseek() bypasses normal buffered IO, so mixing this with reads (other than sysread, for example <> or read()) print, write, seek, tell, or eof may cause confusion.
Quotesysseek() bypasses normal buffered IO, so mixing this with reads (other than sysread, for example <> or read()) print, write, seek, tell, or eof may cause confusion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use strict;
use warnings;
use Fcntl qw/ SEEK_CUR O_RDONLY LOCK_EX /;
sub sys_verwenden {
 print '-' x 30, "\n";
 sysopen my $fh, "file.txt", O_RDONLY or die $!;
 flock $fh, LOCK_EX or die $!;
 sysread($fh, my $buf, 50);
 my $pos = sysseek($fh, 0, SEEK_CUR);
 print "Position: $pos\n";
 print "Buffer:\n";
 print $buf, "\n";
 close $fh;
}
sub vermischt {
 print '-' x 30, "\n";
 sysopen my $fh, "file.txt", O_RDONLY or die $!;
 flock $fh, LOCK_EX or die $!;
 while (my $line = <$fh>) {
   printf "%5s %s", sysseek($fh, 0, SEEK_CUR), $line;
 } Â
 close $fh;
}
sub ohne_sys {
 print '-' x 30, "\n";
 sysopen my $fh, "file.txt", O_RDONLY or die $!;
 flock $fh, LOCK_EX or die $!;
 while (my $line = <$fh>) {
   printf "%5s %s", tell($fh), $line;
 } Â
 close $fh;
}
sys_verwenden();
vermischt();
ohne_sys();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock SEEK_SET SEEK_CUR);
use utf8;
sysopen my $fh, "datei.txt", O_RDWR or die "Kann Datei nicht öffnen: $!\n";
flock $fh, LOCK_EX or die "Kann Datei nicht sperren: $!\n";
binmode $fh, ':utf8';
my $pos = 0;
while (my $line = <$fh>) {
  chomp($line);
  my ($feld1, $feld2, $feld3) = split /\xA1/, $line;
  if ($feld1 eq "EEEEE") {
    seek($fh, $pos, SEEK_SET);
    print $fh "E-E-E¡$feld2¡$feld3\n";
  }; Â
  $pos = tell($fh);
}
close $fh;
1
2
3
4
5
6
7
8
9
sub sys_seek {
sysopen my $fh, "file.txt", O_RDONLY or die $!;
flock $fh, LOCK_EX or die $!;
print "Position: ", sysseek($fh, 0, SEEK_CUR), "\n";
<$fh>;
print "Position: ", sysseek($fh, 0, SEEK_CUR), "\n";
}
foo();
|< 1 2 >| | 13 Einträge, 2 Seiten |