开发者

File management in C++

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 dele开发者_如何学运维te 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!


Use standard C/C++ functions fopen(), fread(), fwrite(), rename() and remove() for that. http://www.cplusplus.com/reference/clibrary/cstdio/


I recommend Boost Filesystem.

Its description reads: "The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories."


You appear to be asking two different questions at once, in a confusing way.

  • To delete a file, use the remove function, found in stdio.h.
  • To erase one line of a file, you have to read the entire file and write it back out with the line removed. There is no library routine for this. The standard "safe" technique is to read the entire file, write it back out (with the line you don't want removed) to a new file in the same directory, 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.


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


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.


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:

http://pocoproject.org

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.

http://qt.nokia.com/

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜