LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-06-2003, 04:14 PM   #1
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
Simple Perl question! Please help!


I need to be able to read from a file and then write over the text I just read from the same file.

I know to read from a file, you just use:
open(INDAT, "<filename");

And to output to a file, you just use:
open(OUTDAT,">filename");

But what if I want to read and write to the same filename?? I keep getting errors when I try doing it like above.

Thanks for any help.
 
Old 11-06-2003, 04:29 PM   #2
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
try open(HANDLE,"+<filename"); or open(HANDLE,"+>filename");

what's the difference between +< and +>?

+> truncates the file first and +< does not
 
Old 11-06-2003, 09:15 PM   #3
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
This is helpful, but I still have a problem. Here is my code:

open(INDAT, "+<somefile.dat") || die "Error blah blah";

$line = <INDAT>;
print $line;
print INDAT "My name is John";


That's it for this simple program. Now, if my somefile.dat file has one line that says: "My name is Bob", I want this program to do the following:
- output "My name is Bob"
- overwrite that line in the file with " My name is John"

The code above doesn't overwrite it, it somehow leaves the file looking like this:

My name is Bob
My name is Bob
My name is John

But I just want "My name is John" to be the only line.

Sorry if this seems like a dumb question, but thanks again for any help!
 
Old 11-06-2003, 10:13 PM   #4
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Rep: Reputation: 30
try
Code:
open (INDAT, "somefile.dat") || die "error message";
$line = <INDAT>;
close (INDAT);
print $line;
open (OUTDAT, ">somefile.dat") || die "error message";
print OUTDAT "My name is John";
close <OUTDAT>;
 
Old 11-07-2003, 05:56 AM   #5
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Thank you so much! I didn't realize how to close the streams I guess - can you tell I knew?

Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple Perl Question whohasit Programming 3 11-05-2004 10:52 AM
simple Perl question true_atlantis Programming 5 11-18-2003 08:37 PM
Simple Perl Question jacksmash Programming 1 11-13-2003 09:17 PM
Simple Perl Question Danny.au Programming 6 07-18-2002 09:46 AM
A simple perl programming question J_Szucs Programming 12 07-14-2002 10:03 PM


All times are GMT -5. The time now is 07:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration