简体   繁体   English

使用Perl从文件读取

[英]Reading from file using Perl

I am learning Perl and have looked up this question but haven't been able to get it to work for me although it terminates without error. 我正在学习Perl,已经查找了这个问题,但是尽管它没有错误地终止,却无法使它对我有用。

I enter a file that it should want ( name-0-0-0 ) but it just skips the while loop altogether. 我输入了它需要的文件( name-0-0-0 ),但它完全跳过了while循环。

open FILE, '+>>userinfo.txt';
print("What is your name?");
$name = <>;
chomp $name;

while (<FILE>) {

chomp;
($nameRead,$wins, $losses, $cats) = split("-");

if ($nameRead eq $name){
    print("Oh hello $name, your current record is $wins wins - $losses losses - $cats ties");
    print("Would you like to play again? type y for yes or n for no\n");
    $bool = <>;
    if ($bool == "y"){
        print("Okay let's play!");
        play();
        exit();
    }
    else {
        printf("well fine goodbye!");
        exit();
    }

} 

} }

Well it seems my problem was indeed related to the +>>. 好吧,看来我的问题确实与+ >>有关。 I am trying to add on to the file, but I wanted to be able to write, not just append. 我正在尝试添加到文件中,但我希望能够编写,而不仅仅是追加。 I changed it to +< and everything worked great. 我将其更改为+ <,并且一切正常。 Thanks guys I really appreciate it! 谢谢大家,我真的很感激!

I appears like you're using the wrong syntax to open the file for reading. 我似乎好像在使用错误的语法打开文件进行读取。 Try 尝试

use autodie qw(:all);
open my $FILE, '<', '/path/to/file';

The syntax you're using opens a file for appending. 您使用的语法会打开一个文件进行追加。

Your primary problem is that you have chosen an arcane open mode for userinfo.txt , which will allow you to open an existing file for both read and write but create a new file if it doesn't exist. 您的主要问题是您为userinfo.txt选择了一种神秘的打开模式,它将允许您打开现有文件进行读写操作,但如果不存在则创建一个新文件。

You must always check whether a file open has succeeded, and it looks like all you want to do is read from this file, so you want 您必须始终检查是否已成功打开文件,并且看起来您想要做的所有事情都是从该文件中读取的,因此您需要

open FILE, '<', 'userinfo.txt' or die $!;

You must also always add 您还必须始终添加

use strict;
use warnings;

to the top of your program, and declare all variables with my at their first point of use. 到程序的顶部,并在第一个使用点用my声明所有变量。

Once you have made these changes you will most likely understand yourself what is going wrong, but if you have further problems please post your modified code. 进行这些更改后,您很可能会了解自己出了什么问题,但是,如果您还有其他问题,请发布修改后的代码。

The following says while you're reading the file and it hasn't reached the end of file: 以下内容表示您正在读取文件,但尚未到达文件末尾:

while (<FILE>) {
   ...
}

You might want to remove the while loop entirely and just do everything inside it. 您可能希望完全删除while循环,然后在其中执行所有操作。


Edit/Alternative Solution: 编辑/替代解决方案:

The real reason nothing was happening is because when you use +>> , it opens the file up for read/append as you'd expect, but it immediately sets the cursor at the end of the file. 什么也没发生的真正原因是因为当您使用+>> ,它将按您的期望打开文件以进行读取/追加,但是它将光标立即设置在文件的末尾。 So that when you encounter the while (<FILE>) { ... } there's nothing to read. 这样一来,当您遇到while (<FILE>) { ... }就无需阅读任何内容。

One solution would be to reset the file cursor position: 一种解决方案是重置文件光标位置:

open FILE, '+>>userinfo.txt';
seek(FILE,0,0);                 # set the file cursor at the top

Why are you opening the file in +>> mode? 为什么以+>>模式打开文件? That mode opens your file for input and output, but sets the filehandle cursor to the end of the file. 该模式打开文件进行输入和输出,但是将文件句柄光标设置到文件的末尾。 Even experienced Perl programmers rarely have a need to do that. 即使是经验丰富的Perl程序员,也很少需要这样做。

Since the filehandle is positioned at the end of the file when you open it, you won't get anything when you attempt to read from it. 由于文件句柄在您打开文件时位于文件的末尾,因此当您尝试从文件句柄中读取任何内容时都不会得到任何信息。

Is there a reason you aren't just saying open FILE, '<', 'userinfo.txt' ? 您是否有理由不只是说open FILE, '<', 'userinfo.txt'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM