我想从给定的输入文件中逐行读取,处理每一行(即它的单词),然后继续前进到其他行...
所以我使用fscanf(fptr,"%s",单词)来读取单词,它应该在遇到行尾时停止...
但这在fscanf中是不可能的,我猜...所以请告诉我如何做的方式......
我应该读取给定行中的所有单词(即应该遇到行尾)以终止然后继续到其他行,并重复相同的过程.
我想在文件的顶部和底部添加一行.我可以按照以下方式做到.
open (DATA, "</usr/old") || die "cant open old\n"; #file to which line has to be added
my @body=<DATA>;
close(DATA);
open (FILE, ">/usr/new") || die "cant open new\n"; #file after stuff has been added
print FILE "9 431";
print FILE "\n";
my $body=@body;
for (my $i=0; $i<$body;$i++){
print FILE "$body[$i]";#not using for loop leads to addition of spaces in new file
}
print FILE "(3,((((1,4),(7,6)),(2,8)),5),9)";
Run Code Online (Sandbox Code Playgroud)
由于我运行大量文件,因此这个过程非常耗时.Perl是否具有用于在文件顶部和底部添加行的任何特定功能?
有没有人能够解决一次处理多行字符串的任务,除了下面显示的字符串作为文件句柄解决方案?
my $multiline_string = "line one\nline two\nline three\nline four";
my $filehandle;
open( $filehandle, '<', \$multiline_string )
or croak("Can't open multi-line string as a filehandle: $!");
while ( defined (my $single_line = <$filehandle>) ) {
# do some processing of $single_line here ...
}
close( $filehandle );
Run Code Online (Sandbox Code Playgroud)
我不想使用文件句柄的原因相当薄弱.Test :: Perl ::当我在任何文件句柄上打开命令和关闭命令之间有超过10个源代码行时,批评者会发牢骚.我正在对$ single_line进行相当多的处理,所以在我的公开通话和近距离通话之间我实际上有大约40行代码,我没有看到任何方法将其降低到10.
而且我真的不想忽略我的构建中的Perl :: Critic测试,因为这实际上是一个不错的测试,我想在我的代码中打开一个实际的磁盘文件时通过它.
我有几个这样的日志文件:
是否可以将它们全部加载到一个文件句柄中,还是需要分别加载它们?
我正在设计一个图像解码器,作为第一步,我试图复制使用cie打开文件,并将其内容写入新文件.下面是我使用的代码.
while((c=getc(fp))!=EOF)
fprintf(fp1,"%c",c);
Run Code Online (Sandbox Code Playgroud)
其中fp是源文件,fp1是目标文件.程序执行时没有任何错误,但图像文件(".bmp")未正确复制.我观察到复制文件的大小较小,只有20%的图像可见,其他都是黑色.当我尝试使用简单的文本文件时,副本已完成.
你知道问题是什么吗?
为Java Homework任务编写了一个基本的文件处理程序,当我收到作业时,我有一些关于未能捕获一些实例的注意事项:
以下是用于打开文件的代码块:
/**
* Create a Filestream, Buffer, and a String to store the Buffer.
*/
FileInputStream fin = null;
BufferedReader buffRead = null;
String loadedString = null;
/** Try to open the file from user input */
try
{
fin = new FileInputStream(programPath + fileToParse);
buffRead = new BufferedReader(new InputStreamReader(fin));
loadedString = buffRead.readLine();
fin.close();
}
/** Catch the error if we can't open the file */
catch(IOException e)
{
System.err.println("CRITICAL: Unable to open …Run Code Online (Sandbox Code Playgroud) 鉴于我有一个需要处理的4GB文件,在Perl中是否有一种方法可以像数组一样引用文件句柄而不将其复制到实际的数组/内存中?
就像是:
open (LOG, "less file.txt |");
my @reference = \<LOG>;
print $reference[1000000];
close LOG;
Run Code Online (Sandbox Code Playgroud)
谢谢!!
我有一个由三个名字组成的文件:daniel,elaine和victoria.如果我搜索丹尼尔,我会得到"你不在名单上".有人可以指出我的错误在哪里吗?谢谢.
#!/usr/bin/perl
#open file
open(FILE, "names") or die("Unable to open file");
# read file into an array
@data = <FILE>;
# close file
close(FILE);
print "Enter name\n";
$entry = <STDIN>;
chomp $entry;
if (grep {$_ eq $entry} @data)
{
print "You are on the list $entry";
}
else
{
print "Your are not on the list";
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含打开的文件句柄的多维哈希,SEEK_END目的是始终读取最新的行而不需要太多的I/O(我会得到的tail).
我现在通过for循环遍历所有这些句柄并调用readline它们.
它看起来像这样:
for $outer ( keys %config ) {
my $line = readline($config{$outer}{"filehandle"});
if (not defined $line || $line eq '' ){
next;
}
else{
print "\nLine: -->".$line."<--\n";
$line =~ m/(:)(\d?\.?\d\d?\d?\d?\d?)/;
$wert = $2;
}
}
Run Code Online (Sandbox Code Playgroud)
如果将新内容写入这些文件,我的脚本会读取它并按照计划行事.
问题是,readline通常会返回任何结果,因为目前还没有在文件的结尾,但我if似乎并没有确定的空返readline的undef为空-它只是打印什么,这是因为没有将正确的这个字符串中没有任何内容,但我根本不希望它被处理.
我有一个hashref中的套接字句柄: $self->{socket}.
我想从中读取它$line = <$self->{socket}>,但是我收到了语法错误.
现在,我知道
print {$self->{socket}} "Hello";
Run Code Online (Sandbox Code Playgroud)
会照顾印刷,但是
$line = < {$self->{socket}} >;
Run Code Online (Sandbox Code Playgroud)
不行.
如何在不使我的代码混乱的情况下执行此操作:
$fh = $self->{socket};
$line = < $fh >;
Run Code Online (Sandbox Code Playgroud)
谢谢.
filehandle ×10
perl ×7
c ×2
arrays ×1
copying ×1
for-loop ×1
href ×1
if-statement ×1
java ×1
multiline ×1
perl-critic ×1
pointers ×1