Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:10:13+00:00 2026-06-12T00:10:13+00:00

this is just an example. Lets assume the above is out.txt . I want

  • 0
    this
    is just
    an example.

Lets assume the above is out.txt. I want to read out.txt and write onto the same file.

    <Hi >
    <this>
    <is just>
    <an example.>

Modified out.txt.
I want to add tags in the beginning and end of some lines.
As I will be reading the file several times I cannot keep writing it onto a different file each time.

EDIT 1
I tried using "+<" but its giving an output like this :

Hi
this
is just
an example.
<Hi >
<this>
<is just>
<an example.>
 **out.txt**

EDIT 2
Code for reference :

open(my $fh, "+<", "out.txt");# or die "cannot open < C:\Users\daanishs\workspace\CCoverage\out.txt: $!";
     while(<$fh>)
     {
        $s1 = "<";
        $s2 = $_;
        $s3 = ">";
        $str = $s1 . $s2 . $s3;
        print $fh "$str";
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:10:15+00:00Added an answer on June 12, 2026 at 12:10 am

    The very idea of what you are trying to do is flawed. The file starts as

    H  i  /  t  h  i  s  /  ...
    

    If you were to change it in place, it would look as follows after processing the first line:

    <  H  i  >  /  i  s  /  ...
    

    Notice how you clobbered “th”? You need to make a copy of the file, modify the copy, the replace the original with the copy.

    The simplest way is to make this copy in memory.

    my $file;
    { # Read the file
       open(my $fh, '<', $qfn)
          or die "Can't open \"$qfn\": $!\n";
       local $/;
       $file = <$fh>;
    }
    
    # Change the file
    $file =~ s/^(.*)\n/<$1>\n/mg;
    
    { # Save the changes
       open(my $fh, '>', $qfn)
          or die "Can't create \"$qfn\": $!\n";
       print($fh $file);
    }
    

    If you wanted to use the disk instead:

    rename($qfn, "$qfn.old")
       or die "Can't rename \"$qfn\": $!\n";
    
    open(my $fh_in, '<', "$qfn.old")
          or die "Can't open \"$qfn\": $!\n";
    open(my $fh_out, '>', $qfn)
          or die "Can't create \"$qfn\": $!\n";
    
    while (<$fh_in>) {
       chomp;
       $_ = "<$_>";
       print($fh_out "$_\n");
    }
    
    unlink("$qfn.old");
    

    Using a trick, the above can be simplified to

    local @ARGV = $qfn;
    local $^I = '';
    while (<>) {
       chomp;
       $_ = "<$_>";
       print(ARGV "$_\n");
    }
    

    Or as a one-liner:

    perl -i -pe'$_ = "<$_>"' file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is just an example, but given the following model: class Foo(models.model): bar =
Input: $string = this-is-just-an--example Output: this is just an-example Tried various things centered around
i have a recursive query like this (note: this is just an example): var
For example: > function foo() { > jQuery(whatever).each( function() { return; // this just
Best explained with code I think, this is just a simple example: public class
This is just a simple question. Either way works. I prefer my first example,
Just using this as an example... Here are the columns in my UserProfile table:
I just tossed this super simple code example into a Flash CS4 IDE frame
I all, I just follow this great example: http://qt-project.org/doc/qt-4.8/tools-customcompleter.html I copy the code exactly
I was just looking at this example from Deitel : #include <stdio.h> struct card

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.