views:

82

answers:

6

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?

+3  A: 

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.

Benoît
@Benoit: Merci!~
luna
+1  A: 

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.

Timo Geusch
@Timo Geusch: Thank you!!
luna
+1  A: 

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/

Starkey
@Starkey: Thank you for the help!
luna
A: 

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.

Jonathan Leffler
@Jonathan, thank you. so the cache file is created into the working directory or not?
luna
@luna: this statement reads an existing cache file that was created somewhere else - an `ifstream` is an input file stream. It will be read from (and therefore will have been created in) the same directory as the basic file name. If that contains just "xyz.txt", that will be the current directory; if it contains "abc/def/xyz.tzt", it will be in the "abc/def" sub-directory, etc. Without knowing the value in `filename`, we can say no more.
Jonathan Leffler
@Janathan: Ok, I see, thank you. I should but cant upvote this answer becuase I do not have enough reputation score, sorry but thankyou
luna
A: 

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.

James
@James, thanks, i refer to c++ wiki pages
luna
A: 
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.

sbi