problems with writing to an output file

A

Ameya

Hello,

I have a directory with about 4000 CSV files (19MB each). I list the
files in another TXT file and then read each line and store it a $file
variable. I open $file which is a CSV file (19MB) and want to print a
variable in a file. However, that does not seem to be possible
although as a check I print the value on the screen successfully. As
an additional check I also try to print "hello" in the output file but
nothing gets printed. On the other hand if I print hello before
reading the CSV file then it does it successfully.

What could be the problem and how can I get my values to print in the
file. Following is the code snippet explaining the read and write
commands I use in my code.

********************************************
open my $fin, '<', $inFile or die "could not open file '$inFile' $!";
# Open the file for reading, $inFile is a CSV file
while (my $line = <$fh>) {
chomp($line);
...........
.........
$a = $line;
}

print $fout "$a\n" or die "Can't print in the file $outFile: $!"; #
There is no value of $a in $outFile
print $fout "Hello\n" or die "Can't print in the file $outFile: $!"; #
There is no "Hello" statement in $outFile
print "Hello\n" or die "Can't print in the file $outFile: $!"; #Prints
Hello on the screen
************************************************

Thanks and Regards,
Ameya
 
S

smallpond

Hello,

I have a directory with about 4000 CSV files (19MB each). I list the
files in another TXT file and then read each line and store it a $file
variable. I open $file which is a CSV file (19MB) and want to print a
variable in a file. However, that does not seem to be possible
although as a check I print the value on the screen successfully. As
an additional check I also try to print "hello" in the output file but
nothing gets printed. On the other hand if I print hello before
reading the CSV file then it does it successfully.

What could be the problem and how can I get my values to print in the
file. Following is the code snippet explaining the read and write
commands I use in my code.

********************************************
open my $fin, '<', $inFile or die "could not open file '$inFile' $!";
# Open the file for reading, $inFile is a CSV file
while (my $line = <$fh>) {
chomp($line);
...........
.........
$a = $line;

}

print $fout "$a\n" or die "Can't print in the file $outFile: $!"; #
There is no value of $a in $outFile
print $fout "Hello\n" or die "Can't print in the file $outFile: $!"; #
There is no "Hello" statement in $outFile
print "Hello\n" or die "Can't print in the file $outFile: $!"; #Prints
Hello on the screen
************************************************

Thanks and Regards,
Ameya

use warnings;
use strict;

Also, what is the size of $outfFile after this runs?
 
J

Jürgen Exner

What could be the problem and how can I get my values to print in the
file. Following is the code snippet explaining the read and write
commands I use in my code.

********************************************
open my $fin, '<', $inFile or die "could not open file '$inFile' $!";
# Open the file for reading, $inFile is a CSV file
while (my $line = <$fh>) {
chomp($line);
...........
.........
$a = $line;
}

print $fout "$a\n" or die "Can't print in the file $outFile: $!"; #
There is no value of $a in $outFile
print $fout "Hello\n" or die "Can't print in the file $outFile: $!"; #
There is no "Hello" statement in $outFile
print "Hello\n" or die "Can't print in the file $outFile: $!"; #Prints
Hello on the screen
************************************************

In the code you are showing above you never open $fout.

That may or may not be a problem (maybe you are opening it in the part
you snipped), but we cannot analyse something that we cannot see. Please
post a minimal, but complete program, strict and warnings clean, that we
can run and that demonstrates your problem.

jue
 
T

Tad J McClellan

Ameya said:
I open $file


There is no $file variable in the code you have posted.

What could be the problem


Did you get any error messages?

If so, what did they say?

How was $fout open()ed? That also is not in the code you have posted.

print $fout "$a\n" or die "Can't print in the file $outFile: $!"; #
There is no value of $a in $outFile


Did the die() make any output?

If so, what did it say?



Have you seen the Posting Guidelines that are posted here frequently?

You have not provided the information we need to help you solve your
problem. The guidelines point out several things that will help us
to help you...
 
A

Ameya

[writing to a file]




What could be the problem and how can I get my values to print in the
file. Following is the code snippet explaining the read and write
commands I use in my code.
********************************************
open my $fin, '<', $inFile or die "could not open file '$inFile' $!";
# Open the file for reading, $inFile is a CSV file
while (my $line = <$fh>) {
   chomp($line);
   ...........
   .........
   $a = $line;
}
print $fout "$a\n" or die "Can't print in the file $outFile: $!"; #
There is no value of $a in $outFile
print $fout "Hello\n" or die "Can't print in the file $outFile: $!"; #
There is no "Hello" statement in $outFile
print "Hello\n" or die "Can't print in the file $outFile: $!"; #Prints
Hello on the screen
************************************************

In the code you are showing above you never open $fout.

That may or may not be a problem (maybe you are opening it in the part
you snipped), but we cannot analyse something that we cannot see. Please
post a minimal, but complete program, strict and warnings clean, that we
can run and that demonstrates your problem.

jue- Hide quoted text -

- Show quoted text -

I have made opened all the file Ids necessary for reading and writing.
As I said this is not the complete code but a part of it. Following is
the complete code:

****************************************************************
#!/usr/bin/perl
use strict; # Variable declaration is required before use
use warnings;
use FileHandle;

my $path = "L:/adio/we/asathe/VestasLoadsData/Turbine7/200710/";
my $year = 2007;
my $month = 10;
my @columns;
my $tStatus;
my @yawDir;
my $sigCount = 0;
my $meanYaw = "";
chdir('L:/adio/we/asathe/VestasLoadsData/Turbine7/200710/') or die
"Can't change directory: $!\n";
my $command = "ls -1 ".$path." > D:/asathe/FLC/PROGS/tmp";

my $yawFile = "D:/asathe/FLC/PROGS/test.txt";
open my $fout, '>', $yawFile or die "could not open file '$yawFile'
$!";
system($command);
my $inFile = "D:/asathe/FLC/PROGS/tmp";
open my $fin, '<', $inFile or die "could not open file '$inFile' $!";
# Open the file for reading

while (my $file = <$fin>) {
chomp($file);
print "Filename is $file\n";
open my $fh, '<', $file or die "could not open file '$file' $!"; #
Open the file for reading
while (my $line = <$fh>) {
chomp($line);
my $lineNum = $.;
if ($lineNum == 1 || $lineNum == 2) {
next;
}
@columns = split /;/, $line;
if ($lineNum == 3) {
$tStatus = $columns[10];
if ($tStatus != 3) {
last;
}
}

if ($lineNum %2 != 0) {
$yawDir[$sigCount++] = $columns[5];
}
}

close $fh;
if ($tStatus == 3) {
print "Status = $tStatus\n";

$meanYaw = eval(join("+", @yawDir)) / @yawDir;
print "$meanYaw\n";
print $fout "%.3f\n", $meanYaw or die "Can't print in the file
$yawFile: $!";

print "Time taken = ";
print time - "$^T\n";
print " sec\n";
} else {
print "Out of Loop\n";
}
$sigCount = 0;

}

close $fin;
close $fout;
*****************************************************************************
 
J

Jürgen Exner

Ameya said:
[writing to a file] [...]
you snipped), but we cannot analyse something that we cannot see. Please
post a minimal, but complete program, strict and warnings clean, that we
can run and that demonstrates your problem.

jue- Hide quoted text -
- Show quoted text -

I wrote neither of those last 2 lines above
I have made opened all the file Ids necessary for reading and writing.
As I said this is not the complete code but a part of it. Following is
the complete code:

Better, but
- its still not a minimal example, in other words there is a lot of
unwanted baggage in this code that cunfuses and distracts from the
problem.
- we still cannot run it because we don't have any sample data to run it
with. Best way is to add a short _DATA_ section the the code.
****************************************************************
#!/usr/bin/perl
use strict; # Variable declaration is required before use
use warnings;

General comment: I really like your style; you are using all the
recommended tips and tricks to get as much help from the compiler and
runtime system as possible, including warnings, stricts, checking if IO
succeeded, etc.
[...]
open my $fout, '>', $yawFile or die "could not open file '$yawFile'
$!";

Ok, so you are opening $fout for output and checking the result. Looks
ok to me.
[...]
if ($tStatus == 3) {
print "Status = $tStatus\n";
$meanYaw = eval(join("+", @yawDir)) / @yawDir;

Uhhh that looks ugly. Have a look at the sum() function from List::Util.
print "$meanYaw\n";
print $fout "%.3f\n", $meanYaw or die "Can't print in the file
$yawFile: $!";

And this appears to be the only place where you are trying to write to
$fout.

I created a standalone minimal program

use warnings; use strict;
my $meanYaw = 3.456;
my $yawFile = 'temp.txt';
open my $fout, '>', $yawFile or die "could not open file '$yawFile' $!";
print $fout "%.3f\n", $meanYaw;

and couldn't reproduce your problem. The number was written into the
file just fine.
On a side note: I don't know why you are printing this "%.3f\n", but
it's probably not doing what you think it is doing.

My best guess at this time would be that maybe $tStatus does not equal 3
and therefore the if-case is not executed. I cannot check that because I
don't have any sample data.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top