开发者

remove not working properly ? ( of std namespace)

Through this snippet i try to remove a certain file from a directory. Here is the code for that.

/* char* cpathToDeleteGND;  
   char* cpathToDeleteFST;
   char* cpathToDeleteSND;
 */

cout << "Enter the name to be removed : ";开发者_如何转开发

cin.ignore();

getline( cin , fullName );

string pathToDeleteGND = "d:/HostelManager/studentDetails/groundFloor/" + fullName  + ".txt";

string pathToDeleteFST = "d:/HostelManager/studentDetails/firstFloor/" + fullName + ".txt";

string pathToDeleteSND = "d:/HostelManager/studentDetails/secondFloor/" + fullName + ".txt";


ifstream checkToDeleteGND( pathToDeleteGND );
ifstream checkToDeleteFST( pathToDeleteFST );
ifstream checkToDeleteSND( pathToDeleteSND );

cpathToDeleteGND = new char[ pathToDeleteGND.size() + 1 ];
cpathToDeleteFST = new char[ pathToDeleteFST.size() + 1 ];
cpathToDeleteSND = new char[ pathToDeleteSND.size() + 1 ];

strcpy( cpathToDeleteGND , pathToDeleteGND.c_str() );
strcpy( cpathToDeleteFST , pathToDeleteFST.c_str() );
strcpy( cpathToDeleteSND , pathToDeleteSND.c_str() );

if( checkToDeleteGND ) {
  if( remove( cpathToDeleteGND) == 0 ) {
   cout << "\nStudent details cleared successfully !";
  }
}  else if( checkToDeleteFST) {
    if( remove( cpathToDeleteFST) == 0 ) {
      cout << "\nStudent details cleared successfully ! ";
            }
   }  else if( checkToDeleteSND ) {
       if( remove( cpathToDeleteSND) == 0 ) {
    cout << "\nStudent details cleared successfully !";
       }
         } else {
        cout << "\nIt seems that either the student has already been removed or does not exist.";
     }

I give the name that should be removed from the directory.Though the if else blocks work but the remove function does not work. I can't understand the reason ..

For example , the output goes like :

Enter the name to be removed : diana
Press any key to continue . . .

The file diana.txt existed that's why it didn't execute the last else block . But the remove function does not work. Why is that ?


The file diana.txt existed that's why it didn't execute the last else block . But the remove function does not work. Why is that?

You don't know because you only print a message if remove succeeds. Try:

if (remove(pathToDeleteFST.c_str()) == 0) {
    // success, print something
} else {
    // failure, much more interesting
    cout << "Can't remove " << pathToDeleteFST << ": "
         << strerror(errno) << endl;
}

errno is in errno.h, strerror in string.h.

(Instead of opening the file to check whether it exists, you could also charge ahead and try to remove it. As @n.m. notes, that may even be necessary on Windows.)


You open the file before deleting it. Windows won't delete files which are open by someone. Do not check for existence of a file by opening it, use stat or just call remove without checking.


This is a very old question but may still be relevant for people who come across this looking for a solution.

The reason this particular example can't delete the file properly is because the files to be deleted are currently being used by the code inside of the iftream variables for each.

Before you can remove and files, you must first .close() them.

If you are having trouble with deleting files that you've previously loaded via fstream, ensure that you have properly closed them before trying to use the remove function.

Hope this helps.


There are a couple of issues with your code, but the probable reason remove fails is that the file is open; Windows will not delete an open file. You should probably refactor a lot of this into separate functions. If you used a function like the following, for example:

bool
fileExists( std::string const& filename )
{
    return std::ifstream( filename.c_str() );
}

, the problem wouldn't occur. There are other reasons why the open might fail, but this is a good rough first approximation. (There are better ways of testing the existance of a file, but they are system dependent.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜