I'm studying C++, now I'm reading about working with files. As I have read, there is quite a lot of variants. So I wanna ask, what is the right way to work with files in C++? Using fstream(ifstream and ofstream)? I have read some opinions that fopen works much faster, so it is better to use it, but it will be no C++.
Thanks for attention!
Use ifstream and ofstream when working in C++. It should not be much slower than FILE*, but is much safer.
See this related question.
I agree with Juraj's assessment of i/ofstream vs. FILE*, I just wanted a word about memory-mapped files. In Boost.SpiritClassic, there's a lesser-known gem called a mmap_file_iterator:
http://www.boost.org/doc/libs/1_47_0/boost/spirit/home/classic/iterator/file_iterator.hpp
I believe that it will memory-map your file if you're in a windows or POSIX environment, and it is a RandomAccessIterator, rather than a Input/OutputIterator.
As for what method is "proper", it all depends on your application's requirements. It is definitely good to explore all of your options and compare the results along as many dimensions as you can conceive.
Related
In C++, you have to include certain libraries to use certain commands.
For example, you type #include <iostream> in order to use std::cout and std::cin.
I always forget what to include to use what commands, so I wonder if there is an easy way to remember, or if it's possible to include everything.
Any way to help me and other people out there who has this problem remember the different includes or a way to make including simpler is appreciated!
Including everything would take forever to compile so that is not advised.
http://www.cplusplus.com/ can tell you what to include for almost anything you are going to need.
Also, you will start to remember them over time.
You can write programs like they are an essay. Take a look at literate programming.
For instance, at the literate programming wiki, you can find a hello world program that addresses all your concerns here.
Just do a quick search of the command you're using, and the forum explaining the command should tell you which library/libraries are involved.
I'm writing an embedded application, and the environment I use does not, unfortunately, have C++11 support at present.
I need to implement a hash/unordered map (a regular std::map won't do for performance reasons), but can't seem to find a way to do it cleanly.
Boost doesn't want to work without bringing in practically the whole library. Even the original STL hash_map from SGI wants several headers, and duplicates standard library functionality, causing ambiguous function calls. It's a real mess.
For ease of implementation, versioning, quality control, V&V, etc. I really need something that leverages the existing standard library and exists in only a few header files that I can put right in the same folder as all the other source/header files. Does such a thing exist, or am I without hope? I've searched for a long while, but have come up empty-handed.
Thanks very much for any help. I can certainly clarify further if necessary.
Did you look at the GNU implementation? On my Ubuntu Machine, unordered_map.h does not include anything. This file is located at
/usr/include/c++/4.6/bits/unordered_map.h
which is about 400 lines although the file "unordered_map" in /usr/include/c++/4.6/ has more headers but you can tweak those I guess.
I think you can find the source code for implementation from GNU.org (?) and compile it yourself?
What library to use to write XML file in a C++ program?
I've found two classes posted in CodeProject
http://www.codeproject.com/KB/stl/simple_xmlwriter.aspx
http://www.codeproject.com/KB/XML/XML_writer.aspx
but want to check if there is more standard option than these. I'm only concerned with writing, and not parsing XML.
I tried different libraries and finally decided for TinyXml. It's compact, fast, free (zlib license) and very easy to use.
Question: Are you ever going to update an XML file? Because while that sounds like it's just more writing, with XML it still requires a parser.
While xerces is large and bloated, it is fully standards compliant and it is DOM based. Should you ever have to cross platform or change language, there will always be a DOM based library for whatever language/platform you might move to so knowing how DOM based parsing/writing works is a benefit. If you are going to use XML, you may as well use it correctly.
Avoiding XML altogether is of course the best option. But short of that, I'd go with xerces.
You can use Xerces-C++, a library written by Apache foundation. This library permits read, write and manipulate XML files.
Link: http://xerces.apache.org/xerces-c/
For my purposes, PugiXML worked out really nicely
http://pugixml.org/
The reason why I thought it was so nice was because it was simply 3 files, a configuration header, a header, and the actual source.
But as you stated, you aren't interested in parsing, so why even bother using a special class to write out XML? While maybe your classes are too complex for this, I found the easy thing to do is use the std::ostream and just write out standard compliant XML this way. For example, say I have a class that represents a Company which is a collection of Employee objects, just make a method in each the Company and Employee classes that looks something like the following psuedocode
Company::writeXML(std::ostream& out){
out << "<company>" << std::endl;
BOOST_FOREACH(Employee e, employees){
e.writeXML(out);
}
out << "</company>" << std::endl;
}
and to make it even easier, your Employee's writeXML function can be declared virtual so that you can have a specific output for say a CEO, President, Janitor or whatever the subclasses should be.
I have been using the open-source libxml2 library for years, works great for me.
I ran into the same problem and wound up rolling my own solution. It's implemented as a single header file that you can drop into your project: xml_writer.h
And it comes with a set of unit tests which also serve as documentation.
Roll your own
I've been in a similar situation. I had a program that needed to generate JSON. We did it two ways. First we tried jsoncpp, but in the end I just generated the JSON directly via a std::ofstream.
Afterward we ran the generated JSON through a validator to catch any syntax errors. There were a few but they were really easy to find and correct.
If I were to do it again I would definitely roll my own again. Somewhat unexpectedly, there was less code when using std::ofstream. Plus we didn't have to use/learn a new API. It was easier to write and is easier to maintain.
I have a module load a file by its path,
However I find it is slow.
I want to accelerate it,
Is there any technology in Windows to create a virtual memory file for the module?
Many thanks
I tested BoxedApp SDK.
It is awesome, but it is not free :)
I'm not entirely sure of what you're asking, but if you just want to create a temporary file that will live in RAM (if possible), you can use CreateFile with FILE_ATTRIBUTE_TEMPORARY.
Assuming you're talking about memory mapped file, yes there are APIs for that. It's too much to discuss in a little SO answer, and besides it's many many years since I used so I don't even recall the function names. Here's how I found info on that quickly: http://www.google.com/search?q=memory+mapped+file+windows.
However, other simpler means are probably the answer to "I find it is slow".
There can be many different reasons for slowness. Please post some code. Then better answers can be given.
Cheer & hth.,
I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing.
Any help would be appreciated...
I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE *, at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one).
You may find this question to be of assistance.
Personally I would strongly recommend steering clear of iostreams, they're poorly designed, heavily C++, and nasty to look at. Take a look at Boost's iostreams, or wrap stdio.h if you need to use classes.
The relevant function for stdio is ftruncate().
The Boost.Interprocess library defines a portable truncate function. For some reason it is not documented, but you can find it this header file.
It'll depend on the OS. Most OSes support this, but in different ways. On Windows, there's a SetEndOfFile(). On Unix and similar systems, you lseek to where you want the file to end, and do an lwrite of zero bytes there. Other OSes undoubtedly use other methods.
I bit the bullet in the end and read the part of the file to be kept to an array then re-wrote it. It's not the best solution - but as the files will always be small I have decided to accept this method.