C++ cache file mean? - c++

I got the C++ code as follows:
string cachefile = filename + ".cache";
ifstream cache(cachefile.c_str(), ios::binary);
As I did not find it in a C++ reference, what are these codes doing (like ifstream etc)? And what is the cache file please? Why should it be created? What are the advantages and how to interpret it? Above all, what is the function of the above codes?

See here for some information on ifstream, which is simply the class you can use to open a file just to read it.
There is no particular reference for ".cache" files, it all depends on the application you are working on.

ifstream is an input file stream that's part of the standard C++ iostreams library.
What the above code does is open a file in binary mode (ie, without any character translations) for reading.
Here's a tutorial for C++ file input/output.

A cache file is a file to store data for later use (usually to use again, and again.) A cache is used to help speed up your program. A cache isn't necessarily a C++ thing, just a software thing.
All the above code is open up a file (an ifstream) that could be used as a cache. You actually don't cache anything in the code you have.
Here's a description of ifstream: http://www.cplusplus.com/reference/iostream/ifstream/

The first line concatenates the string ".cache" to the end of the given file name. If filename contains "xyz.txt", then the cachefile name becomes "xyz.txt.cache".
The second line creates a binary input stream that reads from that cache file.
The cache file is used by the program to store data that now needs to be read. That much can be deduced from the names and context. We can't say much more than that; it depends on the entire application. There is no standard that I'm aware of for ".cache" files.

The easiest way to find out what a certain function or class does is to look at a reference website. My favorite for c++ is cplusplus.com. Here is a link to their entry on ifstream.
As far as cachefile goes, its a string and represents the filename for the filestream.

string cachefile = filename + ".cache";
This creates a new string, named cachefile, whose content is the content of filename, with ".cache" concatenated to it.
ifstream cache(cachefile.c_str(), ios::binary);
This opens an input file stream to binary read from a file with the name stored in cachefile.

Related

How do I delete bytes from a file in C or C++?

I've been Googling this for hours...reading and reading and reading, and yet nothing I come across seems to answer this simple question: In C or C++ programming: I have a file, it contains "hello world". I want to delete "world" (like pressing Backspace in a text editor), then save the file. How do I do this?
I know that files are streams (excellent info on that here!), which don't seem to have a way to delete items from a file per say, and I've studied all of the file-related functions in stdio.h: http://www.cplusplus.com/reference/cstdio/fopen/.
It seems to me that files and streams therefore are NOT like arrays: I can't just delete a byte from a file! Rather (I guess?) I have to create an entire new file and copy the whole original file into the new file withOUT the parts I want to delete? Is that the case?
The only other option I can think of is to seek to the position before "world", then write binary zeros to the end of the file, thereby overwriting "world". The problem with this, however, is a text editor will now no longer properly display this file, as it has non-printable characters in it--and the file size hasn't shrunk--it still contains these bytes--it's just that they hold zeros now instead of ASCII text, so this doesn't seem to be right either.
Related
Resizing a file in C++
You want std::filesystem::resize_file()
Assume your original file is "data.txt". As part of your code, open a new temp file say "data.txt.tmp" and start writing contents to it from original file. Upon writing data, replace the original file with the new one.
You can use a memory map from the source file and copy the data blocks you want to another memory map over target file. That's the easy and fast way (see http://man7.org/linux/man-pages/man2/mmap.2.html)

Does fin in C++ work with .doc files?

I used fin to read in a .doc file, and then store all the text in a string. When I tried printing the string, I just saw unknown characters.
When I copied the contents of the .doc file into a .txt file and then read the .txt file in using fin, everything worked fine.
My question is whether fin works with complex files (such as .doc) or just with .txt files. I only had text in my .doc file (no graphics or anything), but the font was Calibri, which is not the font that fout uses to print text to a .doc file.
If by fin you mean an fistream yes it will work to read the file contents, however in the case of complex files you have to deal with the file format, the c++ library will not automatically extract just the text contents. In the case where you saved the file as text that's all that is left and so that's all a stream would read.
fstream by default does all operations in text mode and .doc files use MS-DOC binary file format. So probably when you tried to read the doc file and print it, it showed characters that you couldn't understand (probably that was binary).
If you try to read any file in fstream, it does read it.
I tried reading a .mp4 file in binary using fstream and it did read the file( i can assure that because i pasted the read contents in another file and that file turned out to be the same video).
So answer to your question is you can read any file in fstream but fstream does all this operations in only two ways, either text or binary.
So reading just any file won't do much good unless you want to do something like copying the file contents to another.
You first need to understand the .doc file format. Read first the doc (computing) wikipage. It is very complex (so you'll need months of work at least) but more or less documented.
You could consider a different approach to your overall goal. For example, if you need to parse a .doc file (provided by some Microsoft Word software), you might use libreoffice which provides some library to parse it, or you could find another library (e.g. DocxFactory, wvware, ...), or you could use some COM interface to Word (on a Microsoft Windows operating system with MicroSoft Word installed).
If your goal is to generate some document, you might consider the PDF format (which is a standard), perhaps using some text formatter like LaTeX or Lout to generate it, or some library (e.g. cairo, PoDoFo, etc ...).
My question is whether fin works with complex files (such as .doc)
BTW, C++ standard IO is capable of reading binary files, but you need to write your parser for them (so you need to understand precisely your file format). You should prefer open formats to proprietary formats.

Read and Rewrite the same file in C++

I have to read from a file, do some operations based on the data I got from that file and then rewrite the whole file with new values obtained after the operations were made. I tried
fstream file("date.in", ios::in|ios::out)
but seems like it puts the new set of data at the end of file. Also tried
fstream file("date.in", ios::in|ios::out|ios::trunc)
but then I can't even read the first set of data as it appears not to be there.
If you want to read and write to the same offsets in the file, you can set the put and get pointers with seekp() and seekg(). Their documentation can be found at cppreference.com.

Opening an existing .doc file using ofstream in C++

Assuming I have a file with .doc extension in Windows platform, how can I open the the file for outputting its contents on the screen using the ofstream object in C++? I am aware that the object can be used to open files in text and binary modes. But I would like to know if a .doc (or even .pdf) file can be opened and its contents read.
I've never actually done this before, but after reading up on it, I think I might have a suggestion. The .docx format is actually just XML that is zipped up. After unzipping, the file is located at word/document.xml. Doing this in a program is where it gets fun.
Two options: If you're using C++ CLR (.NET) then Microsoft has an SDK for you. It should make it pretty easy to open Office documents.
Otherwise if you're just using regular C++, you might have to do some extra work.
Open the file and unzip it using a library like zlib
Find the document.xml file inside
Parse the XML document. You'll probably want to use some kind of XML parsing library for this. You'll have to look up the specs for the XML to figure out how to get the text you want.
C++ std library has ifstream class that can be used to read simple text files, and for read binary files too.
It is up to you to interpret these bytes in the file. To proper interpret the binary file you need to know the format of the file.
If you think of MS Word files then I would start from here: http://en.wikipedia.org/wiki/Office_Open_XML to understand MS Word 2007 format.
You might find the Boost Iostreams library ( http://www.boost.org/doc/libs/1_52_0/libs/iostreams/doc/home.html ) somehow useful if you want to make some filter by yourself.

Adding Data at beginning of file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Delete a Line from a file in C Language
In C++, what is the proper way to insert a line at the beginning of a text file?
How can i add data at the beginning of a file using c/c++ programming?
I have tried following code :
fstream file;
stmt.open(L"d:\\xyz.txt",ios::in|ios::out|ios::app);
but this is appending at the end of file.
You cannot do that.
With only standard C or C++, if you want to do it atomically, you have to write everything to a new file (i.e. new data plus old file), and then move the file over. If you want to play it risky, you can read a block of data, and write the new content at the beginning and move the data up block by block (but if something interrupts you, you've destroyed the file).
If you have access to memory mapping, you can try a different approach: Memory-map the entire file, memmove it by the required offset, and memcpy the new data into the initial segment.
You could do this:
Create a new file
Add the new data at the top
Append the data from the old file
There's rename in cstdio but I'm sure there's also something C++-specific.
Your only options of opening a file as far as i know are read,write and append. Therefore you should read the entire file content (provided it's not a huge file), open a temp file for writing , then write what you want followed by the buffer you read from the old file.
You can also try to open the file in w+ mode and try to position the cursor at the beginning of the file but i don't know if that would work unfortunately