本文整理汇总了C++中fstream类的典型用法代码示例。如果您正苦于以下问题:C++ fstream类的具体用法?C++ fstream怎么用?C++ fstream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了fstream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadModelFromFile
bool KMeansQuantizer::loadModelFromFile(fstream &file){
initialized = false;
numClusters = 0;
clusters.clear();
quantizationDistances.clear();
if( !file.is_open() ){
errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl;
return false;
}
string word;
//First, you should read and validate the header
file >> word;
if( word != "KMEANS_QUANTIZER_FILE_V1.0" ){
errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << endl;
return false;
}
//Second, you should load the base feature extraction settings to the file
if( !loadFeatureExtractionSettingsFromFile( file ) ){
errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl;
return false;
}
file >> word;
if( word != "QuantizerTrained:" ){
errorLog << "loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << endl;
return false;
}
file >> trained;
file >> word;
if( word != "NumClusters:" ){
errorLog << "loadModelFromFile(fstream &file) - Failed to load NumClusters!" << endl;
return false;
}
file >> numClusters;
if( trained ){
clusters.resize(numClusters, numInputDimensions);
file >> word;
if( word != "Clusters:" ){
errorLog << "loadModelFromFile(fstream &file) - Failed to load Clusters!" << endl;
return false;
}
for(UINT k=0; k<numClusters; k++){
for(UINT j=0; j<numInputDimensions; j++){
file >> clusters[k][j];
}
}
initialized = true;
featureDataReady = false;
quantizationDistances.resize(numClusters,0);
}
return true;
}
示例2: clear
bool Classifier::loadBaseSettingsFromFile(fstream &file){
if( !file.is_open() ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << endl;
return false;
}
//Try and load the base settings from the file
if( !MLBase::loadBaseSettingsFromFile( file ) ){
return false;
}
string word;
//Load if the number of clusters
file >> word;
if( word != "UseNullRejection:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read UseNullRejection header!" << endl;
clear();
return false;
}
file >> useNullRejection;
//Load if the classifier mode
file >> word;
if( word != "ClassifierMode:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read ClassifierMode header!" << endl;
clear();
return false;
}
file >> classifierMode;
//Load if the null rejection coeff
file >> word;
if( word != "NullRejectionCoeff:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NullRejectionCoeff header!" << endl;
clear();
return false;
}
file >> nullRejectionCoeff;
//If the model is trained then load the model settings
if( trained ){
//Load the number of classes
file >> word;
if( word != "NumClasses:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumClasses header!" << endl;
clear();
return false;
}
file >> numClasses;
//Load the null rejection thresholds
file >> word;
if( word != "NullRejectionThresholds:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NullRejectionThresholds header!" << endl;
clear();
return false;
}
nullRejectionThresholds.resize(numClasses);
for(UINT i=0; i<nullRejectionThresholds.size(); i++){
file >> nullRejectionThresholds[i];
}
//Load the class labels
file >> word;
if( word != "ClassLabels:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read ClassLabels header!" << endl;
clear();
return false;
}
classLabels.resize( numClasses );
for(UINT i=0; i<classLabels.size(); i++){
file >> classLabels[i];
}
if( useScaling ){
//Load if the Ranges
file >> word;
if( word != "Ranges:" ){
errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read Ranges header!" << endl;
clear();
return false;
}
ranges.resize(numInputDimensions);
for(UINT i=0; i<ranges.size(); i++){
file >> ranges[i].minValue;
file >> ranges[i].maxValue;
}
}
}
return true;
}
示例3: loadModelFromFile
bool DecisionStump::loadModelFromFile(fstream &file){
if(!file.is_open())
{
errorLog <<"loadModelFromFile(fstream &file) - The file is not open!" << endl;
return false;
}
string word;
file >> word;
if( word != "WeakClassifierType:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read WeakClassifierType header!" << endl;
return false;
}
file >> word;
if( word != weakClassifierType ){
errorLog <<"loadModelFromFile(fstream &file) - The weakClassifierType:" << word << " does not match: " << weakClassifierType << endl;
return false;
}
file >> word;
if( word != "Trained:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Trained header!" << endl;
return false;
}
file >> trained;
file >> word;
if( word != "NumInputDimensions:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << endl;
return false;
}
file >> numInputDimensions;
file >> word;
if( word != "DecisionFeatureIndex:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read DecisionFeatureIndex header!" << endl;
return false;
}
file >> decisionFeatureIndex;
file >> word;
if( word != "Direction:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Direction header!" << endl;
return false;
}
file >> direction;
file >> word;
if( word != "NumSteps:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read NumSteps header!" << endl;
return false;
}
file >> numSteps;
file >> word;
if( word != "DecisionValue:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read DecisionValue header!" << endl;
return false;
}
file >> decisionValue;
//We don't need to close the file as the function that called this function should handle that
return true;
}
示例4: OpenInputFile
//*********************************************************************
// FUNCTION: OpenInputFile
// DESCRIPTION: Open the input text file.
// INPUT:
// Parameters:
// std::string filename - Path to the input file.
// std::fstream file - The file object to use when opening the
// input file.
// OUTPUT:
// Return Val: Boolean indicating success of the file open
// operation.
// Parameters:
// std::fstream file - Initialized with the input file open on
// success.
// PERFORMANCE: f(n) = k
//**********************************************************************
bool OpenInputFile(string filename, fstream& file) {
file.open(filename.c_str(), fstream::in);
return(file.good());
} // OpenInputFile
示例5: loadLegacyModelFromFile
bool LogisticRegression::loadLegacyModelFromFile( fstream &file ){
string word;
file >> word;
if(word != "NumFeatures:"){
errorLog << "loadLegacyModelFromFile( fstream &file ) - Could not find NumFeatures!" << endl;
return false;
}
file >> numInputDimensions;
file >> word;
if(word != "NumOutputDimensions:"){
errorLog << "loadLegacyModelFromFile( fstream &file ) - Could not find NumOutputDimensions!" << endl;
return false;
}
file >> numOutputDimensions;
file >> word;
if(word != "UseScaling:"){
errorLog << "loadLegacyModelFromFile( fstream &file ) - Could not find UseScaling!" << endl;
return false;
}
file >> useScaling;
///Read the ranges if needed
if( useScaling ){
//Resize the ranges buffer
inputVectorRanges.resize(numInputDimensions);
targetVectorRanges.resize(numOutputDimensions);
//Load the ranges
file >> word;
if(word != "InputVectorRanges:"){
file.close();
errorLog << "loadLegacyModelFromFile( fstream &file ) - Failed to find InputVectorRanges!" << endl;
return false;
}
for(UINT j=0; j<inputVectorRanges.size(); j++){
file >> inputVectorRanges[j].minValue;
file >> inputVectorRanges[j].maxValue;
}
file >> word;
if(word != "OutputVectorRanges:"){
file.close();
errorLog << "loadLegacyModelFromFile( fstream &file ) - Failed to find OutputVectorRanges!" << endl;
return false;
}
for(UINT j=0; j<targetVectorRanges.size(); j++){
file >> targetVectorRanges[j].minValue;
file >> targetVectorRanges[j].maxValue;
}
}
//Resize the weights
w.resize(numInputDimensions);
//Load the weights
file >> word;
if(word != "Weights:"){
errorLog << "loadLegacyModelFromFile( fstream &file ) - Could not find the Weights!" << endl;
return false;
}
file >> w0;
for(UINT j=0; j<numInputDimensions; j++){
file >> w[j];
}
//Resize the regression data vector
regressionData.resize(1,0);
//Flag that the model has been trained
trained = true;
return true;
}
示例6: init
void init() {
if (ifstream ("Pontuacao.bin")) {
arquivoPontuacao.open ("Pontuacao.bin", ios::in | ios::binary);
arquivoPontuacao >> pontuacaoMaisAlta;
arquivoPontuacao.close();
}
示例7: readFile
void readFile(fstream &file, bool isSequential) {
int fd;
uint64_t start, end;
double average, total = 0, totalAverage = 0;
const char* fileName;
warmup();
double overhead = getReadOverhead();
void* buffer = malloc(BLOCK_SIZE);
pid_t pids[TEST_FILE_NUMBER];
// using TEST_FILE_NUMBER as thread number
for(int i = 1; i < TEST_FILE_NUMBER; i++) {
file << "Company process number=" << i << "\n";
for(int j = 0; j < i; j++) {
// fork new process
pids[j] = fork();
if(pids[j] < 0) {
cout << "Can't fork a new process" << endl;
abort();
}
else if(pids[j] == 0) {
// enter the child process, do pure read process without any fetching data
pureRead(1 + j, isSequential);
exit(0);
}
}
totalAverage = 0;
// parent process finishing fetching performance data
for (int j = 0; j < TEST_TIMES; j++) {
fileName = (TEST_FILE_PREFIX + to_string(1)).c_str();
fd = open(fileName, O_SYNC | O_RDONLY);
if(fd < 0) {
cout << "Can't open the test file, please create a test file first: "<< fileName << endl;
return;
}
// http://stackoverflow.com/questions/2299402/how-does-one-do-raw-io-on-mac-os-x-ie-equivalent-to-linuxs-o-direct-flag
if(fcntl(fd, F_NOCACHE, 1) < 0) {
cout << "Can't close cache of the test file" << endl;
return;
}
// call purge to clear cache
// system("purge");
total = 0;
if(isSequential) {
for(int k = 0; k < BLOCK_NUMBER; k++) {
start = rdtscStart();
read(fd, buffer, BLOCK_SIZE);
end = rdtscEnd();
total += (double) end - (double) start - overhead;
}
}
else {
off_t offset;
for(int k = 0; k < BLOCK_NUMBER; k++) {
start = rdtscStart();
offset = rand() % BLOCK_NUMBER;
// using lseek to set offset
lseek(fd, offset, SEEK_SET);
read(fd, buffer, BLOCK_SIZE);
end = rdtscEnd();
total += (double) end - (double) start - overhead;
}
}
average = (total / BLOCK_NUMBER) * 0.37 / 1000000;
file << average << "\n";
file.flush();
totalAverage += average;
close(fd);
}
cout << "Company thread count = " << i << " average cycles = " << totalAverage / TEST_TIMES << endl;
wait(NULL);
}
// close file and free memory
free(buffer);
}
示例8: clear
bool AdaBoost::loadModelFromFile(fstream &file){
clear();
if(!file.is_open())
{
errorLog << "loadModelFromFile(string filename) - Could not open file to load model!" << endl;
return false;
}
std::string word;
file >> word;
//Check to see if we should load a legacy file
if( word == "GRT_ADABOOST_MODEL_FILE_V1.0" ){
return loadLegacyModelFromFile( file );
}
if( word != "GRT_ADABOOST_MODEL_FILE_V2.0" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read file header!" << endl;
errorLog << word << endl;
return false;
}
//Load the base settings from the file
if( !Classifier::loadBaseSettingsFromFile(file) ){
errorLog << "loadModelFromFile(string filename) - Failed to load base settings from file!" << endl;
return false;
}
file >> word;
if( word != "PredictionMethod:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read PredictionMethod header!" << endl;
return false;
}
file >> predictionMethod;
if( trained ){
file >> word;
if( word != "Models:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Models header!" << endl;
return false;
}
//Load the models
models.resize( numClasses );
for(UINT i=0; i<models.size(); i++){
if( !models[i].loadModelFromFile( file ) ){
errorLog << "loadModelFromFile(fstream &file) - Failed to load model " << i << " from file!" << endl;
file.close();
return false;
}
}
//Recompute the null rejection thresholds
recomputeNullRejectionThresholds();
//Resize the prediction results to make sure it is setup for realtime prediction
maxLikelihood = DEFAULT_NULL_LIKELIHOOD_VALUE;
bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
classLikelihoods.resize(numClasses,DEFAULT_NULL_LIKELIHOOD_VALUE);
classDistances.resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
}
return true;
}
示例9: parseCNFString
//void LMParser::parseCNFString(string formula,vector<PredicateSymbol*> predicateList)
void LMParser::parseCNFString(string formula,fstream& filestr)
{
//apend multiple lines
char* buf = new char[1024];
size_t pos;
while(1)
{
pos = formula.find(WEIGHTSEPARATOR);
if(pos!=string::npos)
break;
else
{
filestr.getline(buf,1024);
string s(buf);
s.erase( remove(s.begin(), s.end(), ' '), s.end() );
formula.append(s);
}
}
delete[] buf;
//extract the weight
string weight = formula.substr(pos+2);
stringstream convert(weight);
double wt;
convert >> wt;
formula = formula.substr(0,pos);
//cout<<wt<<endl;
vector<string> clauseStrings;
LStringConversionUtils::tokenize(formula,clauseStrings,ANDOPERATOR);
vector<WClause*> CNF;
for(int i=0;i<clauseStrings.size();i++)
{
//If clause starts with a paranthesis
if(clauseStrings[i].find(LEFTPRNTH)==0)
{
//eliminate the first and last paranthesis
clauseStrings[i] = clauseStrings[i].substr(1,clauseStrings[i].size()-2);
}
vector<string> atomStrings;
LStringConversionUtils::tokenize(clauseStrings[i],atomStrings,OROPERATOR);
vector<vector<string> > sTermsList(atomStrings.size());
//sign of an atom
vector<bool> sign(atomStrings.size());
//index into the predicate symbols
vector<int> predicateSymbolIndex(atomStrings.size());
for(int j=0;j<atomStrings.size();j++)
{
//find opening and closing braces
int startpos=atomStrings[j].find(LEFTPRNTH);
string predicateName = atomStrings[j].substr(0,startpos);
if(predicateName.find(NOTOPERATOR)==0)
{
//negation
predicateName = predicateName.substr(1,predicateName.size());
sign[j]=true;
}
for(int k=0;k<mln.symbols.size();k++)
{
//found the predicate
if(mln.symbols[k]->symbol.compare(predicateName)==0)
{
predicateSymbolIndex[j] = k;
break;
}
}
int endpos=atomStrings[j].find(RIGHTPRNTH);
string termsString = atomStrings[j].substr(startpos+1,endpos-startpos-1);
vector<string> terms;
LStringConversionUtils::tokenize(termsString,terms,COMMASEPARATOR);
sTermsList[j]=terms;
//check if the number of terms is equal to the declared predicate
if(terms.size()!=mln.symbols[predicateSymbolIndex[j]]->variable_types.size())
{
cout<<"Error! Number/domain of terms in the predicate delcaration does not match in formula::"<<predicateName.c_str()<<endl;
exit(-1);
}
}
//create required terms
vector<vector<LvrTerm*> > iTermsList(atomStrings.size());
for(int j=0;j<atomStrings.size();j++)
{
//for each term of atom i, check if it has already appeared in previous atoms of clause
vector<LvrTerm*> iTerms(sTermsList[j].size());
for(int k=0;k<sTermsList[j].size();k++)
{
int domainIndex = predicateDomainMap[predicateSymbolIndex[j]].at(k);
//if term is a constant must be a unique term
if(isTermConstant(sTermsList[j].at(k)))
{
//find the id of the term
int id=-1;
for(int m=0;m<domainList[domainIndex]->values.size();m++)
{
if(domainList[domainIndex]->values[m].compare(sTermsList[j].at(k))==0)
{
id=m;
break;
//.........这里部分代码省略.........
示例10: loadLegacyModelFromFile
bool AdaBoost::loadLegacyModelFromFile( fstream &file ){
string word;
file >> word;
if( word != "NumFeatures:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read NumFeatures header!" << endl;
return false;
}
file >> numInputDimensions;
file >> word;
if( word != "NumClasses:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read NumClasses header!" << endl;
return false;
}
file >> numClasses;
file >> word;
if( word != "UseScaling:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read UseScaling header!" << endl;
return false;
}
file >> useScaling;
file >> word;
if( word != "UseNullRejection:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read UseNullRejection header!" << endl;
return false;
}
file >> useNullRejection;
if( useScaling ){
file >> word;
if( word != "Ranges:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Ranges header!" << endl;
return false;
}
ranges.resize( numInputDimensions );
for(UINT n=0; n<ranges.size(); n++){
file >> ranges[n].minValue;
file >> ranges[n].maxValue;
}
}
file >> word;
if( word != "Trained:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Trained header!" << endl;
return false;
}
file >> trained;
file >> word;
if( word != "PredictionMethod:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read PredictionMethod header!" << endl;
return false;
}
file >> predictionMethod;
if( trained ){
file >> word;
if( word != "Models:" ){
errorLog <<"loadModelFromFile(fstream &file) - Failed to read Models header!" << endl;
return false;
}
//Load the models
models.resize( numClasses );
classLabels.resize( numClasses );
for(UINT i=0; i<models.size(); i++){
if( !models[i].loadModelFromFile( file ) ){
errorLog << "loadModelFromFile(fstream &file) - Failed to load model " << i << " from file!" << endl;
file.close();
return false;
}
//Set the class label
classLabels[i] = models[i].getClassLabel();
}
}
//Recompute the null rejection thresholds
recomputeNullRejectionThresholds();
//Resize the prediction results to make sure it is setup for realtime prediction
maxLikelihood = DEFAULT_NULL_LIKELIHOOD_VALUE;
bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
classLikelihoods.resize(numClasses,DEFAULT_NULL_LIKELIHOOD_VALUE);
classDistances.resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
return true;
}
示例11: sizeof
void disk_list<T>::disk(fstream& f, long int k, long int m)
{
long int i,j,kk;
T w, u, t;
i=k; j=m; kk=(long)(i+j)/2;
f.seekg(kk*sizeof(T));
f.read((char *)&w, sizeof(T));
t = w;
do
{
f.seekg(i*sizeof(T));
f.read((char *)&w, sizeof(T));
while ((strcmp(w.SORT_KEY,t.SORT_KEY)<0)&&(i<=j))
{
i=i+1;
f.seekg(i*sizeof(T));
f.read((char *)&w, sizeof(T));
}
f.seekg(j*sizeof(T));
f.read((char *)&w, sizeof(T));
while ((strcmp(w.SORT_KEY,t.SORT_KEY)>0)&&(i<=j))
{
j=j-1;
f.seekg(j*sizeof(T));
f.read((char *)&w, sizeof(T));
}
if (i<=j)
{
f.seekg(i*sizeof(T));
f.read((char *)&w, sizeof(T));
f.seekg(j*sizeof(T));
f.read((char *)&u, sizeof(T));
f.seekg(i*sizeof(T));
f.write((char *)&u, sizeof(T));
f.seekg(j*sizeof(T));
f.write((char *)&w, sizeof(T));
i=i+1; j=j-1;
}
}while (i<=j);
if (k<j) disk(f,k,j);
if (i<m) disk(f,i,m);
return;
}
示例12: write
void BLOCK::write(fstream &fp){
fp.write((char*)&bytesUsed, sizeof(bytesUsed));
for(int i = 0; i < bytesUsed; i++)
fp.write((char*)&data[i], sizeof(char));
fp.write((char*)&next, sizeof(next));
}
示例13: Write
void Chunk::Write(fstream &OutputFile)
{
OutputFile.write(ChunkID, sizeof(ChunkID));
OutputFile.write((const char *)&ChunkSize, sizeof(ChunkSize));
}
示例14:
bool CloseTable ()
{
if ( StreamData.is_open() ) StreamData.close();
if ( StreamIndex.is_open() ) StreamData.close();
return true;
}
示例15: stopLocLog
void Noggin::stopLocLog()
{
outputFile.close();
loggingLoc = false;
}