当前位置: 首页>>代码示例>>C++>>正文


C++ ifstream::is_open方法代码示例

本文整理汇总了C++中std::ifstream::is_open方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::is_open方法的具体用法?C++ ifstream::is_open怎么用?C++ ifstream::is_open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std::ifstream的用法示例。


在下文中一共展示了ifstream::is_open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: readFile

	// Read edgelist input file
	inline void readFile(char* filePath, nodeCollection* ncp){
		inputFileStream1.open(filePath);
			std::string line;
			std::string x, y, del;
			int maxValue, a, b;
			char oneLine[256];
			maxValue = 0;

			//To find max Value (to make dynamic array)
			if(inputFileStream1.is_open()){
				int lineNumber = 0;
				while(getline(inputFileStream1, line)){
					strcpy(oneLine,line.c_str());
					del = strtok(oneLine,"\t ");
					x = del;
					y = strtok(NULL,"\t ");
					lineNumber++;
					a = atoi(x.c_str());
					b = atoi(y.c_str());
					
					if ( a > b){
						if(a > maxValue){
							maxValue = a;}
					}
					else{
						if(b > maxValue){
							maxValue = b;}
					}
				}
			}
			else{
				std::cout<<"Error Occured!"<<std::endl;
			}

			//ncp->setAdjMat(maxValue);	//Make matrix
			ncp->setDegMat(maxValue);	//Make degMatrix
			inputFileStream1.seekg(0, inputFileStream1.beg);		//to read again
			inputFileStream1.close();
			inputFileStream2.open(filePath);
			if(inputFileStream2.is_open()){
				while(getline(inputFileStream2, line)){	
					strcpy(oneLine,line.c_str());
					del = strtok(oneLine,"\t ");
					x = del;
					y = strtok(NULL,"\t ");
					a = atoi(x.c_str());
					b = atoi(y.c_str());
					ncp->putNode(a, b);
					ncp->putNode(b, a);
					}
			}
			else{
				std::cout<<"Error Occured!"<<std::endl;
			}
			std::cout<<"####  FILE READ & SETUP FINISHED####"<<std::endl;
			inputFileStream2.close();
			
	}
开发者ID:kujungmul,项目名称:BlackHole,代码行数:59,代码来源:edgeReader.hpp

示例2: OpenFile

std::string MiscUtil::OpenFile(std::string name, std::ifstream& f)
{
#ifndef ABS_TOP_SRCDIR
    #error ABS_TOP_SRCDIR not defined!
#endif
#ifndef DATADIR
    #error ABS_TOP_SRCDIR not defined!
#endif
    std::string programDirFile;
    std::string absFile;
    std::string dataFile;
    # if defined(BOOST_FILESYSTEM_VERSION)
        BenzeneAssert (BOOST_FILESYSTEM_VERSION == 2 || BOOST_FILESYSTEM_VERSION == 3);
    #endif
    {
        path p = programDir / name;
        p.normalize();
        #if (defined (BOOST_FILESYSTEM_VERSION) && (BOOST_FILESYSTEM_VERSION == 3))
            programDirFile = p.string();
        #else
            programDirFile = p.native_file_string();
        #endif
        f.open(programDirFile.c_str());
        if (f.is_open())
            return programDirFile;
    }
    {
        path p = boost::filesystem::path(ABS_TOP_SRCDIR) / "share" / name;
        p.normalize();
        #if (defined (BOOST_FILESYSTEM_VERSION) && (BOOST_FILESYSTEM_VERSION == 3))
            absFile = p.string();
        #else
            absFile = p.native_file_string();
        #endif
        f.open(absFile.c_str());
        if (f.is_open())
            return absFile;
    }
    {
        path p = boost::filesystem::path(DATADIR) / name;
        p.normalize();
        #if (defined (BOOST_FILESYSTEM_VERSION) && (BOOST_FILESYSTEM_VERSION == 3))
            dataFile = p.string();
        #else
            dataFile = p.native_file_string();
        #endif
        f.open(dataFile.c_str());
        if (f.is_open())
            return dataFile;
    }
    throw BenzeneException() << "Could not find '" << name << "'. Tried \n"
                             << "\t'" << programDirFile << "' and\n"
                             << "\t'" << absFile << "' and\n"
                             << "\t'" << dataFile << "'.";
}
开发者ID:kenjyoung,项目名称:benzene-vanilla,代码行数:55,代码来源:Misc.cpp

示例3: Load

  //-------------------------------------------------------------------------//
  bool Material::Load(std::ifstream& ifs)
  {
    //-----------------------------------------------------------------------//
    // Throw assertion is file streams is not open.
    assert(ifs.is_open() && ifs.good());
    //-----------------------------------------------------------------------//

    //-----------------------------------------------------------------------//
    // If the file stream is not open return false.
    if(!ifs.is_open() || !ifs.good())
      return false;
    //-----------------------------------------------------------------------//

    //-----------------------------------------------------------------------//
    // This string will hold the label that describes some camera parameter.
    string strLabel;
    //-----------------------------------------------------------------------//

    //-----------------------------------------------------------------------//
    // This unsigned short will count the number of camera parameters read in
    // and will be used to verify that the correct number of parameters have 
    // been initialized.
    unsigned short usParameterCount = 0;
    //-----------------------------------------------------------------------//

    //-----------------------------------------------------------------------//
    // The for loop will read in data from the file stream and will set 
    // the objects data members accordingly.
    for(unsigned short i = 0; i < NUMBER_OF_MATERIAL_PARAMETERS; i++)
    {
      //---------------------------------------------------------------------//
      // Read in the label
      ifs >> strLabel;
      //---------------------------------------------------------------------//

      //---------------------------------------------------------------------//
      // Match the label to some predefined token and set the proper data member.
      float fR, fG, fB, fA;
      if(strLabel == DIFFUSE_COLOR_LABEL)
      {
        ifs >> fR;
        ifs >> fG;
        ifs >> fB;
        ifs >> fA;
        this->SetDiffuse(fR, fG, fB);
        usParameterCount++;
      }
      else if(strLabel == AMBIENT_FACTOR_LABEL)
开发者ID:skevy,项目名称:CSC-350---Assignment-4,代码行数:49,代码来源:Material.cpp

示例4: _configure

            /**
            * @brief Configures the block: defines the input file.
            * @param n The configuration parameters
            */
            virtual void _configure(const xml_node& n)
            {
                xml_node source = n.child("source");
                xml_node gates_node = n.child("gates");
                if( (!source)  || (!gates_node) )
                    throw std::runtime_error("TweetReader: missing parameter");
                std::string gates_s = gates_node.attribute("number").value();
                std::string type = source.attribute("type").value();
                std::string ip = source.attribute("ip").value();
                file_name = source.attribute("name").value();
                if(!type.length() || !file_name.length() || !gates_s.length())
                    throw std::runtime_error("TweetReader: missing attribute");
                if(type.compare("offline") != 0)
                    throw std::runtime_error("TweetReader: invalid type parameter");

                num_gates = atoi(gates_s.c_str());

                file.open(file_name);
                if(!file.is_open()) {
                    throw std::runtime_error("TweetReader: cannot open source file");
                }

                // Create and register the output gates
                m_outgate_ids = new int[num_gates];
                for(int i=0; i<num_gates; i++){
                    std::string ogate = outgate_basename + boost::lexical_cast<std::string>(i);
                    m_outgate_ids[i] = register_output_gate(ogate.c_str());
                }
            }
开发者ID:cnplab,项目名称:blockmon,代码行数:33,代码来源:TweetReader.cpp

示例5: fOpen

/*Name: fOpen
//Purpose: Opens file with given filename
//Parameters: ifstream object, string filename, bool fail bit
//Returns: null
*/
void fOpen(std::ifstream& fin, string filename, bool& fail){
	fin.open(filename.c_str());
	if(!fin.is_open()){
		fail = 1;
		cerr << "File failed to open";
	}
}
开发者ID:jbu92,项目名称:schoolstuff,代码行数:12,代码来源:Project1.cpp

示例6: test_reverse_word

// Unit test.
bool test_reverse_word(std::ifstream &file)
{
	if(!file.is_open())
	{
		throw; //file not open
	}

	std::string line;

	while(getline(file,line))
	{
		//make a deep copy...
		std::string expected = line + "";

		char* exp = &expected[0];
		char* actual = &line[0];

		reverse_words_quick_and_dirty(exp);
		reverse_words(actual);
		
		if(strcmp(actual,exp) != 0)
		{

			std::cout << "expected: " << exp << std::endl;
			std::cout << "actual  : " << actual << std::endl;
			std::cout << "Test Failed!" << std::endl;
			return false;
		}
	}

	std::cout << "Test Passed!" << std::endl;

	return true;
}
开发者ID:balasuar,项目名称:Bungie-Code-Review-2010,代码行数:35,代码来源:Problem1.cpp

示例7: setConstantsFromFile

	void Constants::setConstantsFromFile(std::ifstream & infile)
	{
		if(infile.is_open())
		{
			std::string input;
			while(std::getline(infile,input))
			{
				std::vector<float> params;
				if(input[0] == '#') continue;
				std::string commandName = input.substr(0,input.find_first_of(" "));

				input = input.substr(input.find_first_of(" ")+1,input.length());
				while(input.length() > 0)
				{
					int spaceloc = input.find_first_of(" \n\0");
					std::string param = input.substr(0,(spaceloc>=0)?spaceloc:input.length());
					if(input[0] == '#') ;
					if(spaceloc == -1)
						input = "";
					else
						input = input.substr(spaceloc+1,input.length());
					char * charParam = new char[input.size()+1];
					strcpy(charParam,param.c_str());
					float parameter = (float)atof(charParam);
					params.push_back(parameter);
				}
			
				if(commandName == "G_ACCEL")
				{
					Constants::getInstance()->gravity = params[0];
				}
			}
		}
	}
开发者ID:Nammy1101,项目名称:ENSE470_Lab5,代码行数:34,代码来源:Constants.cpp

示例8: closeEdgeFile

  bool closeEdgeFile(int iter_counter) {
 	 if (fin.is_open()) {
 		 fin.close();
 		 //logstream(LOG_INFO) << "close edge block-" << block_id << std::endl;
 	 }
 	 return true;
  }
开发者ID:HybridGraph,项目名称:GraphLab-PowerGraph,代码行数:7,代码来源:dynamic_csr_storage_disk.hpp

示例9: m_load_matches_from_stream

/*--------------------------------------------------------------------
						m_load_matches_from_stream
----------------------------------------------------------------------*/
bool CStereoOdometryEstimator::m_load_matches_from_stream(
		std::ifstream		& stream,
		vector<cv::DMatch>	& matches,
		vector<size_t>		& matches_ids )
{
	/* FORMAT
	- # of matches
		- match id
		- queryIdx
		- trainIdx
		- distance
	*/
	if( !stream.is_open() )
		return false;

	size_t num_matches, num_matches_id;
	stream.read( (char*)&num_matches, sizeof(size_t) );
	stream.read( (char*)&num_matches_id, sizeof(size_t) );
	matches.resize( num_matches );
	matches_ids.resize( num_matches_id );
	const bool add_ids = num_matches == num_matches_id;
	for( size_t m = 0; m < matches.size(); ++m )
	{
		if( add_ids ) stream.read( (char*)&(matches_ids[m]), sizeof(size_t) );
		stream.read( (char*)&(matches[m].queryIdx), sizeof(matches[m].queryIdx) );
		stream.read( (char*)&(matches[m].trainIdx), sizeof(matches[m].trainIdx) );
		stream.read( (char*)&(matches[m].distance), sizeof(matches[m].distance) );
		stream.read( (char*)&(matches[m].imgIdx), sizeof(matches[m].imgIdx) );
	} // end-for-matches

	return true;
} // end-loadMatchesFromStream
开发者ID:caomw,项目名称:stereo-vo,代码行数:35,代码来源:common.cpp

示例10: deserialize

void RigidBodyScene::deserialize( std::ifstream& inputstream )
{
  assert( inputstream.is_open() );

  // Load the state of each rigid body
  for( std::vector<RigidBody>::size_type i = 0; i < m_rbs.size(); ++i ) m_rbs[i].deserialize( inputstream );
}
开发者ID:js4768,项目名称:4167T3M1,代码行数:7,代码来源:RigidBodyScene.cpp

示例11: VInitialize

//----------------------------------------------------------------------
bool KeyboardProfile::VInitialize( std::ifstream& fin, WkActionTranslatorPtr pActionTransl )
{
	auto pActiontranslator = pActionTransl.lock();
	auto pDevice = m_pKeyboard.lock();
	if( fin.is_open() && pActiontranslator && pDevice )
	{
		string temp;

		if( !InitializeGOID(fin) )
		{
			return false;
		}

		// Mapping button/action
		fin >> temp;
		while( temp != ";" )
		{
			if( StringUtilities::ToLower(temp) == "b" )
			{
				temp = MapButtonToAction( pDevice, pActiontranslator, fin );
			}
			else // Skip
			{
				do 
				{
					fin >> temp;
				} 
				while( temp != "b" && temp != ";" );
				return false;
			}
		}
		return true;
	}
开发者ID:GillesLeclercq,项目名称:GameEngineProjectGillesLeclercq,代码行数:34,代码来源:KeyboardProfile.cpp

示例12: hideFile

/** \brief Hide a file (or some other ifstream) in the loaded picture
 * It loads the streamsize to this->hundredPercentValue and the amount of finished bytes to this->doneBytes
 * \param &toHideFileStream std::ifstream the filestream whose contents should be hidden in the picture (should be opened binary)
 * \param &password const std::string the password (not used yet)
 * \throw SteganoException::ImgNotLoaded if no container file is loaded.
 * \throw SteganoException::Img2Small if the loaded image is too small to hide the given phrase
 * \throw SteganoException::FileStreamClosed if the specified fileStream is not opened
 */
void SteganoHide::hideFile(std::ifstream &toHideFileStream, const std::string &password) {
    if(!this->steganoImage.isValid()) {
        throw SteganoException::ImgNotLoaded();
    }

    // we need to divide it by 2 because in worst case every byte takes 2 pixel
    if(getFileStreamSizeInBytes(toHideFileStream) > (this->xResolution * this->yResolution) / 2) {
        throw SteganoException::Img2Small();
    }

    if(!toHideFileStream.is_open()) {
        throw SteganoException::FileStreamClosed();
    }

    this->origImageBackup = this->steganoImage;
    this->doneBytes = 0;
    // 2 * |imagePixel| because we normalize all pixels first and afterwards we reset it.
    this->hundredPercentValue = getFileStreamSizeInBytes(toHideFileStream) + 2 * this->pixelAmount;

    normalizeImage();
    unsigned int loopCount = 0;
    while(toHideFileStream.good()) {
        Pixel hidingPixel = calculateHidingPosition(loopCount);

        hideByteAtPixel(toHideFileStream.get(), hidingPixel);

        loopCount++;
        this->doneBytes++;
   //    std::cout << "byte" << std::endl;
    }

    drawFinishPixel(calculateHidingPosition(loopCount));
    resetNormalizedImage();
}
开发者ID:Vaphen,项目名称:Stegano,代码行数:42,代码来源:SteganoHide.cpp

示例13: openfile

void openfile(const char *filename, std::ifstream &infile){
    infile.open(filename);
    if (! infile.is_open()) {
        std::cout << "Error opening file:" << filename << std::endl;
        exit(1);
    }
}
开发者ID:c19,项目名称:cpp_exercises,代码行数:7,代码来源:basics1.cpp

示例14: read_file

    bool InpNativeInfo::read_file(const std::string& file_name)
    {
        if(already_read)
            std::cout << "Warning: trying to substitute ninfo for already initiated object." 
                      << std::endl;
        already_read = true;

        if(inpfile.is_open())
        {
            std::cout << "Warning: trying to re-open file named " << file_name
                      << std::endl;
        }
        inpfile.open(file_name);
        bool read(false);
        
        while(!inpfile.eof())
        {
            std::string line;
            std::getline(inpfile, line);

            if(line.empty()) continue;
            if(line[0] == '\x2a') continue;

            if(eq_ignorecase(line.substr(0,20), "<<<< native_info_sim"))
            {
                read = true;
                std::string temp(line, 20);
                simN = std::stoi(temp);
                read_block(inpfile);
            }
        }
        return read;
    }
开发者ID:ToruNiina,项目名称:NinfoTools,代码行数:33,代码来源:InpNativeInfo.hpp

示例15: open

		void open(std::ifstream& stream) const {
			stream.open(m_path.c_str(), std::ios::in|std::ios::binary);
			if (!stream.is_open()) {
				LOG4CXX_ERROR(m_logger, "Couldn't open file [" << m_path << "]");
				MR4C_THROW(std::logic_error, "Couldn't open file [" << m_path << "]");
			}
			stream.exceptions(std::ifstream::badbit);
		}
开发者ID:Andy-Amoy,项目名称:mr4c,代码行数:8,代码来源:LocalDataFileSource.cpp


注:本文中的std::ifstream::is_open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。