Reputation: 11431
I have a requirement for reading, updating and deleting a file. I want to write a class for this. For example
class FileManagement {
private:
fstream myFile;
public:
void read();
void update();
void delete();
};
My question is while updating is it possible to delete only one line in a file in C++ and should be portable, if it is possible how we can achieve this. Other question is if above option is not possible how we can achieve the above.
In C++ how we can delete a file in portable way.
Thanks!
Upvotes: 0
Views: 8813
Reputation: 4258
If you are looking for a higher-level C++ library that is object-oriented and can handle both filename manipulation and file I/O, POCO is a decent choice:
ACE is an older, battle-tested framework that includes lots of I/O support. It's commonly used for it's excellent CORBA support, but there's a lot in there:
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
And, finally, there's QT. Normally known for its cross-platform UI library, QT actually includes several other useful pieces (including file management and I/O), and you don't even have to link in the UI stuff if you don't need it.
If you'd rather not bring in another framework, I would recommend rolling your own File I/O classes using boost::filesystem and either the standard iostream or stdio functions. You can use the interfaces in the above frameworks as a reference, but you will also want to familiarize yourself with modern C++ design, as demonstrated by Boost and explained in Modern C++ Design.
Upvotes: 0
Reputation: 140445
You appear to be asking two different questions at once, in a confusing way.
remove
function, found in stdio.h
.fsync
the new file, close it, then rename
the new file to the old name. If you don't care about concurrent readers or the computer crashing in the middle of the operation, you can instead open the old file read/write, read its contents into memory, rewind
the file handle, and rewrite it directly.Upvotes: 3
Reputation: 136208
Use standard C/C++ functions fopen(), fread(), fwrite(), rename() and remove() for that. http://www.cplusplus.com/reference/clibrary/cstdio/
Upvotes: 8
Reputation: 3250
I recommend Boost Filesystem.
Its description reads: "The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories."
Upvotes: 7
Reputation: 25495
Most systems will accept posix compliant statements. You could always just define abstract base class and create different concrete implementations that use whatever platform specific instructions you need. You could have one if def that instantiates the correct concrete class.
Upvotes: 0
Reputation: 9314
You should look at the posix standard, and find the file operations (like fopen()
). Where platforms do not support posix, or diverge from the standard, you'll likely need to
#ifdef NONPOSIXOS1 // really, this should be a good identifier of hte OS // write code to handle the special case #else // write code to handle the posix compliant case #endif
Upvotes: 1