Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Writing to a File??

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
hey every1.
Im very new to perl, but im getting on gr8 with it, and learning more every day!
my problem is this, i want to keep records in a file, instead of only at run-time in an array.
But when i write anything to a file, then view it, I get a load of wierd characters and numerous linefeeds!!
----------------------code--------------------------
#!/usr/bin/perl

open (FILE, ">>C:/files.dat");
chomp($user = <stdin>);
$user_copy;
print FILE $user;
$user_copy = <FILE>;
close(FILE);
print &quot;contents of file are: $user_copy\n&quot;;
----------------------------------------------------

please if anyone could help, id be V.appreciative!!
thanks very much, JoE
 
chomp($user = <stdin>);

open (FILE, &quot;>>C:/files.dat&quot;);
binmode(FILE);
print FILE $user;
$user_copy = <FILE>;
close(FILE);
print &quot;contents of file are: $user_copy\n&quot;;

it a dat file ...just open it in notepad u will see ...
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
sorry 'neversleep', didnt work :(
this is what is attached to the variable $user when it is put in FILE.

joe
¸4¶ : ¯ k Æ ß ˆ
t`¶ ¶ ð ¶ < ¶ À^Î @mÎ @mÎ C  mÎ @mÎ @mÎ

is there any way or removing this goobeldygook?? I realise most languages do this (VB etc) but a) can it be got rid of? and b) would it be read in if i put the contents of the file in an array???

please if anyone could help, id b really grateful!!!
try running the script to see what i mean :p
thankyou all!
JoE
 
just a tip that may help:

try and open the file like this:

open(FILE, &quot;>>C:\foo.dat&quot;) or die &quot;file i/o err: $!\n&quot;;

the 2 things that I noticed are that the file separator you use is a forward slash but in windows it is a back slash and the other is that you are not checking if there was a problem opening the file. In perl, $! is a special variable that is the stderr variable, that variable will get set if there is a problem with a call to open. try that and see what you get.


hope that helps.
 
Alright, I have a number of problems with what you have. First, you open the file with the >> prefix, which is open for append, but later on you try and read from the same filehandle. If you want to append and then read from a file, you must open it with a +< prefix.

Second, if you change your open prefix like that, the pointer to the position in the file will begin at the end. You will write to end, lengthening the file, but keeping the pointer at the end. Then you try and read from the current position, which ought to return an EOF. You should use the seek function to set the file position to where the data is.

Third, Perl variables don't need to be instatiated before they are used. The line $user_copy; can be eliminated. Try something more like this:
Code:
#+> is open append, versus +< which is open truncate
open (FILE, &quot;+>C:\files.dat&quot;);
chomp($user = <stdin>);
print FILE $user;
#check perldoc.com for seek's usage, this line sets the position to the file's beginning
seek FILE, 0, 0;
$user_copy = <FILE>;
close(FILE);
print &quot;contents of file are: $user_copy\n&quot;;
If you are writing binary data that needs to be written into the file, set binmode(FILE) as neversleep mentioned and write to the file using syswrite (again, check perldoc.com for usage). ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Oh yeah, and add in that error checking like scoon said. Good practice. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top