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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:20+00:00 2026-05-26T06:24:20+00:00

I have problem with remove() function First, look at the example and you will

  • 0

I have problem with remove() function

First, look at the example and you will see the problem

cout << "Please enter the phone number to remove" << endl << "Phone number: ";
string rphNumber;
cin >> rphNumber;
ifstream ifile("db/" + rphNumber + ".txt");
if(ifile)
  remove(("db/" + rphNumber + ".txt").c_str()); // the problem here
else
  cout << "failure" << endl;

The problem in this line (the file path), always the function return -1 Although the file exists

remove(("db/" + rphNumber + ".txt").c_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-05-26T06:24:21+00:00Added an answer on May 26, 2026 at 6:24 am

    Your problem may be that you still have ifile open at the point where you try to remove it. Some operating systems don’t let you delete a file that’s open. Another possibility is that the string rphNumber might have a newline at the end which you need to strip off before assembling the filename. (I don’t remember if cin does that or not.)

    Your problem definitely is that you are trying to find out if a filesystem operation will work. You can’t do that. In between when you make the test and when you actually try to do the operation, another process may change things so that the operation won’t work, even though your test said it will. Also, being able to open a file is not the same as being able to delete a file; there are probably lots of files on your hard drive that you can open but not delete (such as /dev/null).

    You have to just do the filesystem operation. It will tell you if it worked or not, with its return value. Then, when it doesn’t work, you look at errno to find out why. The C utility function strerror (include <cstring>) will convert an errno value to a human-readable error message.

    Putting it together, here’s the correct way to write your program:

    cout << "Please enter the phone number to remove.\nPhone number: ";
    string rphNumber;
    cin >> rphNumber;
    string fname("db/" + rphNumber + ".txt");
    
    if (remove(fname.c_str()))
        cout << "Failed to delete '" << fname << "': " << strerror(errno) << '\n';
    else
        cout << '\'' << fname << "' successfully deleted.\n";
    

    Incidentally, never use endl; if '\n' doesn’t work, it means your streambufs are not configured correctly.

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

Sidebar

Related Questions

I have a problem where I need to remove all code and triggers from
i have problem use link_to_remote link_to_remote document example say link_to_remote Delete this post, :update
I have a small problem with jQuery $.ajax() function. I have a form where
Let's say I have a function like this (it's only an example, so do
I am having issues getting my remove element function working. I have this function:
First, the problem: I have several free projects, and as any software they contains
My problem is a little bit complex. I have the following code: $(document).ready(function() {
Its a string problem.First remove all repeated consecutive substring with length 1,then delete substring
I have a big form, that will be serialized by a jQuery function. The
I have the following problem deploying my application. It uses JMS and a remote

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.