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

  • SEARCH
  • Home
  • 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 6690981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:41:57+00:00 2026-05-26T05:41:57+00:00

i have c file and i need to add some information at the begging

  • 0

i have c file and i need to add some information at the begging of the c file. I have one hash table with keys as the numbers and values as the strings. by using that table i am searching if the string found i am adding information to c file. i did this by using script i posted in ” add information to a file using perl” question. now i need to add information at the beginging of the c file if i found string.In my script i am adding before the string.
what should i do now.
thanks in advance.

  • 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-05-26T05:41:57+00:00Added an answer on May 26, 2026 at 5:41 am

    (Cross-posted from the answer I just gave on the SitePoint forums to what appears to be the same question.)

    Sadly, there’s no way to insert information at the beginning of a file without having to rewrite the whole thing, so you’ll need to read in the entire file (rather than just one line at a time), determine which string(s) appear in the contents, write the corresponding information item(s) to a new file, and (finally!) write the original contents to the new file:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use File::Slurp;
    
    my %strings = (
      'string1' => "information \n",
      'string2' => "information2 \n",
      'string3' => "information3 \n",
    );
    my $test_string = "(" . join("|", keys %strings) . ")";
    
    # First make a list of all matched strings (including the number of times
    # each was found, although I assume that's not actually relevant)
    
    my $code = read_file('c.c');
    my %found;
    while ($code =~ /$test_string/g) {
      $found{$1}++;
    }
    
    # If %found is empty, we didn't find anything to insert, so no need to rewrite
    # the file
    
    exit unless %found;
    
    # Write the prefix data to the new file followed by the original contents
    
    open my $out, '>', 'c.c.new';
    for my $string (sort keys %found) {
      print $out $strings{$string};
    }
    
    print $out $code;
    
    # Replace the old file with the new one
    rename 'c.c.new', 'c.c';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an xml file sitemap.xml as shown below ..i need to add one
I have a use case where I need to add information about the user
i need some information what is a hbm.xml file ??? i know just *.hbm
I need to send some information from the command prompt to a text file
I need to save some data to an existing table. So i have column
I have a PHP file which writes some information into a configuration file which
In my application I download some xml data file from some information center, add
I need to generate a PDF file with some information i get from a
I have a file that I need to parse into an array but I
I have an xml file I need to modify: ... </web-app> I want to

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.