In Perl, I know this method:
open( my $in, "<", "inputs.txt" );
reads a file, but it only does so if the file exists.
In other words, with +:
open( my $in, "+>", "inputs.txt" );
writes a file / truncates if it exists, so I do not get the opportunity to read the file and save it in the program.
How to read files in Perl if the file exists or not?
Ok, I edited my code, but the file is not readable. The problem is that it is not in the loop. Anything naughty with my code?
open( my $in, "+>>", "inputs.txt" ) or die "Can't open inputs.txt : $!\n";
while (<$in>) {
print "Here!";
my @subjects = ();
my %information = ();
$information{"name"} = $_;
$information{"studNum"} = <$in>;
$information{"cNum"} = <$in>;
$information{"emailAdd"} = <$in>;
$information{"gwa"} = <$in>;
$information{"subjNum"} = <$in>;
for ( $i = 0; $i < $information{"subjNum"}; $i++ ) {
my %subject = ();
$subject{"courseNum"} = <$in>;
$subject{"courseUnt"} = <$in>;
$subject{"courseGrd"} = <$in>;
push @subjects, \%subject;
}
$information{"subj"} = \@subjects;
push @students, \%information;
}
print "FILE LOADED.\n";
close $in or die "Can't close inputs.txt : $!\n";
source
share