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 7059673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:15:38+00:00 2026-05-28T04:15:38+00:00

I am working on ubuntu. I have a file called test.txt. I would like

  • 0

I am working on ubuntu. I have a file called test.txt. I would like to replace the second line with another line. How can I do that? I don’t want to create a new file and delete the first one.

I would like to specify that the lenght of the new line is the same with the length of the ond one

  • 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-28T04:15:39+00:00Added an answer on May 28, 2026 at 4:15 am

    Try something like:

    #include <fstream>
    #include <string>
    
    int main() {
        const int lineToReplace = 14;
        std::fstream file("myfile.txt", std::ios::in | std::ios::out);
        char line[255];
        for (int i=0; i<lineToReplace-1; ++i) 
            file.getline(line, 255); // This already skips the newline
        file.tellg();
        file << "Your contents here" << std::endl;
        file.close();
        return 0;
    }
    

    Note that line can hold up to 254 bytes (plus the null terminator), so if your line takes more than that, adjust accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in ubuntu, I have a folder that contains a subfolder. in
Working in bash under Linux ubuntu 10 here I have Bash script that reads
I'm working on ubuntu 10.04 and a gcc. I have a binary file with
I am working on Ubuntu. I would like to record on asterisk video files
I've got a django project working on a development server(ubuntu) that we have been
In Ubuntu 11.10 x86_64 I have this this simple test.c file: #include <openssl/ssl.h> int
I have a multithreaded file converter that I'm working on. On Windows, it puts
I have ELDK-3.1 installed in a Ubuntu box working perfectly. In another machine, running
I am working on ubuntu. I have a file.h263 and I don't know how
I am working in ubuntu server 10.04 I am creating a test.h file which

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.