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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:38:46+00:00 2026-05-18T11:38:46+00:00

I’m trying to edit the /etc/fstab file on a CentOS installation using C++. The

  • 0

I’m trying to edit the /etc/fstab file on a CentOS installation using C++. The idea being that based on another config file I will add entries that don’t exist in the fstab, or edit entries in the fstab file where the mount point is the same. This lets us set the system up properly on initial bootup.

I’ve found setmntent() and getmntent() for iterating over the exiting entries so I can easily check whether an entry in fstab also exists in my config file. And I can then use addmntent() to add any entry that doesn’t already exist – the documentation says nothing about this being able to edit an entry, only add a new entry to the end of the file. There seems to be no way to edit an existing entry or delete an entry. It seems odd that this feature doesn’t exist, only the CR and not the UD of CRUD.

I’d rather not have to write my own parser if I can at all help it.

My other alternative is to:

  • open the file using setmntent()
  • read the whole of fstab into memory using getmentent() and perform any additions and/or edits
  • close the file using endmntent()
  • open /etc/fstab for writing
  • close /etc/fstab (thus emptying the file)
  • open the fstab using setmntent()
  • loop through the entries I read in previously and write them out using addmntent()

Which although probably fine, just seems a bit messy.

  • 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-18T11:38:47+00:00Added an answer on May 18, 2026 at 11:38 am

    When modifying system configuration files such as /etc/fstab keep in mind that these are critical state and, should your “edit” be interrupted by a power loss, might result in a failure to reboot.
    The way to deal with this is:

    1. create an empty output:
      FILE* out = setmntent("/etc/fstab.new", "rw");
    2. open the original for input:
      FILE* in = setmntent("/etc/fstab", "r");
    3. copy the contents:
      while (m = getmntent(in)) { addmntent(out, m); }
    4. make sure the output has it all:
      fflush(out); endmntent(out); endmntent(in);
    5. atomically replace /etc/fstab:
      rename("/etc/fstab.new", "/etc/fstab");

    It’s left as an exercise to the reader to change the body of the while loop to make a modification to an existing element, to substitute a specifically crafted mntent or whatever. If you have specific questions on that please ask.

    UN*X semantics for rename() guarantee that even in the case of power loss, you’ll have either the original version or your new updated one.

    There’s a reason why there is no modifymntent() – because that would encourage bad programming / bad ways of changing system critical files. You say at the end of your post “… probably fine …” – not. The only safe way to change a system configuration file is to write a complete modified copy, sync that to safe storage, and then use rename to replace the old one.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put

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.