本文整理汇总了C++中ifstream::is_open方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::is_open方法的具体用法?C++ ifstream::is_open怎么用?C++ ifstream::is_open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ifstream
的用法示例。
在下文中一共展示了ifstream::is_open方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadRoom
bool Room::loadRoom(ifstream &ifs)
{
if(!ifs.is_open())
{
cout << "Could not open file." << endl;
return false;
}
ifs >> roomWidth;
ifs >> roomHeight;
ifs >> encounterLvl; //cout << encounterLvl; system("pause");
ifs.get();
string temp;
for(int y = 0; y < roomHeight; y++)
{
getline(ifs, temp);
for(int x = 0; x < roomWidth; x++)
{
room[x][y].setAppearance(temp[x]);
if(temp[x] == '#')
{
room[x][y].setDoor(false);
room[x][y].setSolid(true);
room[x][y].setBoss(false);
}
else if(temp[x] == 'D')
{
room[x][y].setDoor(true);
room[x][y].setSolid(false);
room[x][y].setBoss(false);
room[x][y].setDoorLoc(x, y);
if(numDoors < MAX_DOORS)
{
doors[numDoors] = &room[x][y];
numDoors++;
}
}
else if(temp[x] == 'S')
{
room[x][y].setAppearance(' ');
room[x][y].setDoor(false);
room[x][y].setSolid(false);
room[x][y].setBoss(false);
playerLoc.X = x-1;
playerLoc.Y = y;
}
else if(temp[x] >= '0' && temp[x] <= '8')
{
room[x][y].setAppearance(' ');
room[x][y].setDoor(false);
room[x][y].setSolid(false);
room[x][y].setBoss(false);
room[x][y].setEncounterChance((temp[x] - 48)*10);
}
else if(temp[x] == '9')
{
room[x][y].setAppearance(' ');
room[x][y].setDoor(false);
room[x][y].setSolid(false);
room[x][y].setBoss(false);
room[x][y].setEncounterChance(100);
}
else if(temp[x] == 'B')
{
room[x][y].setAppearance(234);
room[x][y].setDoor(false);
room[x][y].setBoss(true);
room[x][y].setSolid(false);
room[x][y].setEncounterChance(0);
}
}
}
for(int i = 0; i < numDoors; i++)
{
int r, d;
ifs >> r ;
ifs >> d;
doors[i]->setDestRoom(r, d);
}
return true;
}
示例2: lexically_analyze
void lexically_analyze(ifstream &code_file, ofstream &lex_file, ofstream &symbol_file)
{
if ( ! code_file.is_open())
throw "File not found";
std::unordered_set<string> symbols;
std::vector<token_lexeme_pair> token_lexemes;
int line_number = 1;
DFA dfaz[10];
initialize(dfaz[0], dfaz[1], dfaz[2], dfaz[3], dfaz[4], dfaz[5], dfaz[6], dfaz[7], dfaz[8], dfaz[9]);
int curr_dfa = 0;
int consecutive_break_count = 0; // To check for symbols not in the language.
std::string _subject((std::istreambuf_iterator<char>(code_file)), (std::istreambuf_iterator<char>()));
for (unsigned int _index = 0; _index < _subject.size(); _index++) {
if (_subject[_index] == '\n')
line_number++;
if (consecutive_break_count == DFA_COUNT || _subject[_index] == ' ' || _subject[_index] == '\n' || _subject[_index] == '\t' || _subject[_index] == '\0') {
consecutive_break_count = 0;
continue;
}
int i = _index;
string str;
int last_state = 0;
for (unsigned int j = i; ; j++) {
// DFA breaks.
int result = dfaz[curr_dfa].simulate_symbol(_subject[j]);
if (result == dfaz[curr_dfa].get_reserved_state()) {
if (last_state) { // Keep the 'str' string because a token has been found.
consecutive_break_count = 0;
_index = j - 1; // Continue where this DFA broke.
}
else { // Return to original position.
str.clear();
_index = i - 1;
consecutive_break_count++;
}
break;
}
// DFA enters into a neutral state.
if (result == 0) {
if (_subject[j] == '\n')
line_number++;
str.push_back(_subject[j]);
last_state = 0;
}
// DFA enters into an accept state.
if (result == 1) {
_index = j;
str.push_back(_subject[j]);
last_state = 1;
}
}
// Token found.
if (str.size() > 0) {
if (dfaz[curr_dfa].getName() == "identifier" && check_keyword(str)) {
token_lexemes.push_back(token_lexeme_pair("keyword", str, line_number));
}
else {
token_lexemes.push_back(token_lexeme_pair(dfaz[curr_dfa].getName(), str, line_number));
}
if (dfaz[curr_dfa].getName() == "identifier" && !check_keyword(str)) {
symbols.insert(str);
}
}
// Change DFA.
curr_dfa++;
if (curr_dfa == DFA_COUNT)
curr_dfa = 0;
}
for (const auto& elem : symbols) {
symbol_file << elem <<endl;
}
lex_file << setw(15);
for (std::vector<token_lexeme_pair>::iterator it = token_lexemes.begin(); it != token_lexemes.end(); it++) {
lex_file.width(30);
//.........这里部分代码省略.........
示例3: readWordEmbeddings
void Labeler::readWordEmbeddings(const string& inFile, NRMat<dtype>& wordEmb) {
static ifstream inf;
if (inf.is_open()) {
inf.close();
inf.clear();
}
inf.open(inFile.c_str());
static string strLine, curWord;
static int wordId;
//find the first line, decide the wordDim;
while (1) {
if (!my_getline(inf, strLine)) {
break;
}
if (!strLine.empty())
break;
}
int unknownId = m_wordAlphabet.from_string(unknownkey);
static vector<string> vecInfo;
split_bychar(strLine, vecInfo, ' ');
int wordDim = vecInfo.size() - 1;
std::cout << "word embedding dim is " << wordDim << std::endl;
m_options.wordEmbSize = wordDim;
wordEmb.resize(m_wordAlphabet.size(), wordDim);
wordEmb = 0.0;
curWord = normalize_to_lowerwithdigit(vecInfo[0]);
wordId = m_wordAlphabet.from_string(curWord);
hash_set<int> indexers;
dtype sum[wordDim];
int count = 0;
bool bHasUnknown = false;
if (wordId >= 0) {
count++;
if (unknownId == wordId)
bHasUnknown = true;
indexers.insert(wordId);
for (int idx = 0; idx < wordDim; idx++) {
dtype curValue = atof(vecInfo[idx + 1].c_str());
sum[idx] = curValue;
wordEmb[wordId][idx] = curValue;
}
} else {
for (int idx = 0; idx < wordDim; idx++) {
sum[idx] = 0.0;
}
}
while (1) {
if (!my_getline(inf, strLine)) {
break;
}
if (strLine.empty())
continue;
split_bychar(strLine, vecInfo, ' ');
if (vecInfo.size() != wordDim + 1) {
std::cout << "error embedding file" << std::endl;
}
curWord = normalize_to_lowerwithdigit(vecInfo[0]);
wordId = m_wordAlphabet.from_string(curWord);
if (wordId >= 0) {
count++;
if (unknownId == wordId)
bHasUnknown = true;
indexers.insert(wordId);
for (int idx = 0; idx < wordDim; idx++) {
dtype curValue = atof(vecInfo[idx + 1].c_str());
sum[idx] += curValue;
wordEmb[wordId][idx] += curValue;
}
}
}
if (!bHasUnknown) {
for (int idx = 0; idx < wordDim; idx++) {
wordEmb[unknownId][idx] = sum[idx] / count;
}
count++;
std::cout << unknownkey << " not found, using averaged value to initialize." << std::endl;
}
int oovWords = 0;
int totalWords = 0;
for (int id = 0; id < m_wordAlphabet.size(); id++) {
if (indexers.find(id) == indexers.end()) {
oovWords++;
for (int idx = 0; idx < wordDim; idx++) {
wordEmb[id][idx] = wordEmb[unknownId][idx];
}
}
totalWords++;
}
//.........这里部分代码省略.........
示例4: setData
//========================setData=====================================
// Sets this Object's description to the line of characters extracted
// from the infile stream. If the description is longer than
// MAX_SIZE the trailing data will be omitted.
//
// Preconditions: The give ifstream is open.
//
// Postconditions: my_desc is set to the first MAX_SIZE of chars in
// the infile.
//====================================================================
void Object::setData (ifstream &infile)
{
if (infile.is_open())
infile.getline (my_desc, MAX_SIZE + 1);
}
示例5: read
bool crush_player::read (ifstream & file_stream)
{
if (file_stream.is_open () == 0)
{
return false;
}
int string_size = 0;
char name [PLAYER_NAME_LEN];
char team_name [TEAM_NAME_LEN];
memset (name, 0, PLAYER_NAME_LEN);
memset (team_name, 0, TEAM_NAME_LEN);
file_stream.read ((char*) &string_size, 4);
if (string_size > PLAYER_NAME_LEN)
{
return false;
}
file_stream.read ((char*) name, string_size);
m_name = name;
m_name.resize (string_size);
file_stream.read ((char*) &string_size, 4);
if (string_size > TEAM_NAME_LEN)
{
return false;
}
file_stream.read ((char*) team_name, string_size);
m_team_name = team_name;
m_team_name.resize (string_size);
file_stream.read ((char*) &m_last_crush_week, 4);
file_stream.read ((char*) &m_last_crush_season, 4);
file_stream.read ((char*) &m_dead, 4);
file_stream.read ((char*) &m_first_crush_week, 4);
file_stream.read ((char*) &m_first_crush_season, 4);
file_stream.read ((char*) &m_jersey_color, 1);
file_stream.read ((char*) &m_trim_color, 1);
file_stream.read ((char*) &m_ap, 1);
file_stream.read ((char*) &m_checking, 1);
file_stream.read ((char*) &m_strength, 1);
file_stream.read ((char*) &m_toughness, 1);
file_stream.read ((char*) &m_reflexes, 1);
file_stream.read ((char*) &m_jump, 1);
file_stream.read ((char*) &m_hands, 1);
file_stream.read ((char*) &m_dodge, 1);
file_stream.read ((char*) &m_total_value, 2);
file_stream.read ((char*) &m_points_to_spend, 2);
file_stream.read ((char*) &m_race, sizeof (race_type));
file_stream.read ((char*) &m_best_rushing_tiles, 2);
file_stream.read ((char*) &m_best_goals_scored, 2);
file_stream.read ((char*) &m_best_kills_for, 2);
file_stream.read ((char*) &m_best_injuries_for, 2);
file_stream.read ((char*) &m_best_checks_thrown, 2);
file_stream.read ((char*) &m_best_checks_landed, 2);
file_stream.read ((char*) &m_best_rushing_attempts, 2);
file_stream.read ((char*) &m_best_sacks_for, 2);
file_stream.read ((char*) &m_rushing_tiles, 2);
file_stream.read ((char*) &m_goals_scored, 2);
file_stream.read ((char*) &m_kills_for, 2);
file_stream.read ((char*) &m_injuries_for, 2);
file_stream.read ((char*) &m_checks_thrown, 2);
file_stream.read ((char*) &m_checks_landed, 2);
file_stream.read ((char*) &m_rushing_attempts, 2);
file_stream.read ((char*) &m_sacks_for, 2);
file_stream.read ((char*) &m_total_rushing_tiles, 2);
file_stream.read ((char*) &m_total_goals_scored, 2);
file_stream.read ((char*) &m_total_kills_for, 2);
file_stream.read ((char*) &m_total_injuries_for, 2);
file_stream.read ((char*) &m_total_checks_thrown, 2);
file_stream.read ((char*) &m_total_checks_landed, 2);
file_stream.read ((char*) &m_total_rushing_attempts, 2);
file_stream.read ((char*) &m_total_sacks_for, 2);
int historical_week_list_size;
file_stream.read ((char*) &historical_week_list_size, 4);
for (int historical_week_index = 0; historical_week_index < historical_week_list_size; historical_week_index++)
{
crush_player_historical_week_ptr current_week = new crush_player_historical_week;
current_week->read (file_stream);
m_crush_player_historical_week_list.push_back (current_week);
}
return true;
}
示例6: yylexopen
bool yylexopen(const char filename[])
{
fin.open(filename, ios_base::in);
return fin.is_open();
}
示例7: deserialize_binary
void chkpt_t::deserialize_binary(ifstream& ifs)
{
if(!ifs.is_open()) {
cerr << "Could not open input stream for chkpt file" << endl;;
W_FATAL(fcINTERNAL);
}
ifs.read((char*)&highest_tid, sizeof(tid_t));
size_t buf_tab_size;
ifs.read((char*)&buf_tab_size, sizeof(size_t));
for(uint i=0; i<buf_tab_size; i++) {
PageID pid;
ifs.read((char*)&pid, sizeof(PageID));
buf_tab_entry_t entry;
ifs.read((char*)&entry, sizeof(buf_tab_entry_t));
DBGOUT1(<<"pid[]="<<pid<< " , " <<
"rec_lsn[]="<<entry.rec_lsn<< " , " <<
"page_lsn[]="<<entry.page_lsn);
// buf_tab[pid] = entry;
mark_page_dirty(pid, entry.page_lsn, entry.rec_lsn);
}
size_t xct_tab_size;
ifs.read((char*)&xct_tab_size, sizeof(size_t));
for(uint i=0; i<xct_tab_size; i++) {
tid_t tid;
ifs.read((char*)&tid, sizeof(tid_t));
xct_tab_entry_t entry;
ifs.read((char*)&entry.state, sizeof(smlevel_0::xct_state_t));
ifs.read((char*)&entry.last_lsn, sizeof(lsn_t));
ifs.read((char*)&entry.first_lsn, sizeof(lsn_t));
DBGOUT1(<<"tid[]="<<tid<<" , " <<
"state[]="<<entry.state<< " , " <<
"last_lsn[]="<<entry.last_lsn<<" , " <<
"first_lsn[]="<<entry.first_lsn);
if (entry.state != smlevel_0::xct_ended) {
mark_xct_active(tid, entry.first_lsn, entry.last_lsn);
if (is_xct_active(tid)) {
size_t lock_tab_size;
ifs.read((char*)&lock_tab_size, sizeof(size_t));
for(uint j=0; j<lock_tab_size; j++) {
lock_info_t lock_entry;
ifs.read((char*)&lock_entry, sizeof(lock_info_t));
// entry.locks.push_back(lock_entry);
add_lock(tid, lock_entry.lock_mode, lock_entry.lock_hash);
DBGOUT1(<< " lock_mode[]="<<lock_entry.lock_mode
<< " , lock_hash[]="<<lock_entry.lock_hash);
}
}
// xct_tab[tid] = entry;
}
}
示例8: open
bool LightFile::open(ifstream &file, const char *filename) {
file.open(filename, ios::in);
return file.is_open();
}
示例9: hasNext
virtual bool hasNext()
{
if (readFile.is_open()==false) return false;
getline (readFile,line);
return (readFile.eof()==false);
}
示例10: PromptUserInput
///////////////////////////////////////////////////////////////////////////////
// Name: PromptUserInput
// Author: Scott Olmstead
// Description: Prompts the user as to what file they would like to open.
// Then, based on their choice, opens the input and output files
// associated with that choice.
///////////////////////////////////////////////////////////////////////////////
void PromptUserInput(ofstream& outputFile,
ifstream& inputFile)
{
int index;
int choice;
//Output a file list title to the console window
cout << "File Names" << endl;
//Output a divider to the console window
OutputDivider((ofstream&)cout,'-',CONSOLE_WIDTH);
//Output the choices to the to console window
for(index = 0; index < FILE_COUNT; index++)
{
//Output the current file name to the console window
cout << index + 1 << ". " << INPUT_NAMES[index]
<< endl;
}
//Output a divider to the console window
OutputDivider((ofstream&)cout,'=',CONSOLE_WIDTH);
//Prompt the user to enter a file number via console
cout << "Enter the number of which file you would like to use: ";
//Get choice from the keyboard via console
cin >> choice;
//Output whitespace to the console window
cout << endl;
//If choice is out of range, select default
if(!(choice > 0 && choice <= FILE_COUNT))
{
//Output a divider to the console window
OutputDivider((ofstream&)cout,'.', 30);
//Output an error message to the console window
cout << "Choice " << choice
<< " is out of range." << endl;
//Let the user know the default was selected
// by outputting message to console window
cout << "Opening default file choice, 1"
<< endl;
//Set the default choice(1)
choice = 1;
}
//Open the input file
inputFile.open(INPUT_NAMES[choice - 1]);
//Open output file
outputFile.open(OUTPUT_NAMES[choice - 1]);
//If the input file is open
if(inputFile.is_open())
{
//Output a message to the console window saying the input file
// was opened
cout << "SUCCESS! The file " << INPUT_NAMES[choice - 1]
<< " was opened successfully. " << endl;
}
//If the input file is not open
else
{
//Output a message to the console window stating the file could not
// be opened.
cout << "ERROR: The file " << INPUT_NAMES[choice - 1]
<< " could not be opened. " << endl
<< " This program will not produce expected results";
}
//Output a message to the console window stating what the
// output file is named.
cout << endl << "Writing output to: " << OUTPUT_NAMES[choice - 1]
<< endl;
//Output a divider to the console window
OutputDivider((ofstream&)cout,'_', CONSOLE_WIDTH);
}
示例11: ss
if(fileName.size() == 0){
// The string is a list of node numbers
stringstream ss(Teuchos::getValue<string>(it->second));
int nodeID;
while(ss.good()){
ss >> nodeID;
// Convert from 1-based node numbering (Exodus II) to 0-based node numbering (Epetra and all the rest of Peridigm)
TEUCHOS_TEST_FOR_EXCEPT_MSG(nodeID < 1, "**** Error: Node number 0 detected in nodeset definition; node numbering must begin with 1.\n");
nodeList.push_back(nodeID - 1);
}
}
else{
// The string is the name of a file containing the node numbers
ifstream inFile(fileName.c_str());
TEUCHOS_TEST_FOR_EXCEPT_MSG(!inFile.is_open(), "**** Error opening node set text file: " + fileName + "\n");
while(inFile.good()){
string str;
getline(inFile, str);
boost::trim(str);
// Ignore comment lines, otherwise parse
if( !(str[0] == '#' || str[0] == '/' || str[0] == '*' || str.size() == 0) ){
istringstream iss(str);
vector<int> nodeNumbers;
copy(istream_iterator<int>(iss),
istream_iterator<int>(),
back_inserter<vector<int> >(nodeNumbers));
for(unsigned int i=0 ; i<nodeNumbers.size() ; ++i){
// Convert from 1-based node numbering (Exodus II) to 0-based node numbering (Epetra and all the rest of Peridigm)
TEUCHOS_TEST_FOR_EXCEPT_MSG(nodeNumbers[i] < 1, "**** Error: Node number 0 detected in nodeset file; node numbering must begin with 1.\n");
nodeList.push_back(nodeNumbers[i] - 1);
示例12: init
void init (string simfilename) {
/* intialization reads the simfile and populates the internal parameters
simfilename = name of the simulation parameter file
*/
cfile.open(simfilename.c_str());
if (!cfile.is_open()) {
cout << "ERROR: cannot find simfile!" << endl;
exit (10);
}
// populate the internal parameters
string returnstring; // string that gets returned for conversion to numbers
returnstring = find_header_element ("ydim", false);
ydim = atoi (returnstring.c_str());
returnstring = find_header_element ("xdim", false);
xdim = atoi (returnstring.c_str());
returnstring = find_header_element ("cellsize", false);
cellsize = atof (returnstring.c_str());
returnstring = find_header_element ("yll_corner", false);
yll_corner = atof (returnstring.c_str());
returnstring = find_header_element ("xll_corner", false);
xll_corner = atof (returnstring.c_str());
returnstring = find_header_element ("max_iterations", false);
max_iterations = atoi (returnstring.c_str());
returnstring = find_header_element ("len_timestep", false);
len_timestep = atof (returnstring.c_str());
returnstring = find_header_element ("global_basal_pres", false);
global_basal_pres = atof (returnstring.c_str());
returnstring = find_header_element ("viscosity", false);
viscosity = atof (returnstring.c_str());
returnstring = find_header_element ("Q_advection_global", false);
Q_advection_global = atof (returnstring.c_str());
returnstring = find_header_element ("Q_advection_stochasticity", false);
Q_advection_stochasticity = atof (returnstring.c_str());
returnstring = find_header_element ("Q_squish_coef", false);
Q_squish_coef = atof (returnstring.c_str());
returnstring = find_header_element ("ice_advection", false);
ice_advection = atof (returnstring.c_str());
// entrainment variables
returnstring = find_header_element ("entrainment_cavity", false);
entrainment_cavity = atof (returnstring.c_str());
returnstring = find_header_element ("entrainment_zero", false);
entrainment_zero = atof (returnstring.c_str());
returnstring = find_header_element ("entrainment_slp_1", false);
entrainment_slp_1 = atof (returnstring.c_str());
returnstring = find_header_element ("entrainment_vtx_2", false);
entrainment_vtx_2 = atof (returnstring.c_str());
returnstring = find_header_element ("entrainment_slp_2", false);
entrainment_slp_2 = atof (returnstring.c_str());
// basement erosion properties
returnstring = find_header_element ("abrasion_from_N_slope", false);
abrasion_from_N_slope = atof (returnstring.c_str());
returnstring = find_header_element ("abrasion_from_N_zero", false);
abrasion_from_N_zero = atof (returnstring.c_str());
returnstring = find_header_element ("abrasion_from_iceload", false);
abrasion_from_iceload = atof (returnstring.c_str());
returnstring = find_header_element ("global_bsmt_erodibility", false);
global_bsmt_erodibility = atof (returnstring.c_str());
returnstring = find_header_element ("iceload_surf_return_fraction", false);
iceload_surf_return_fraction = atof (returnstring.c_str());
// sediment bleed properties
returnstring = find_header_element ("iceload_bleed", false);
iceload_bleed = atof (returnstring.c_str());
returnstring = find_header_element ("surf_bleed", false);
surf_bleed = atof (returnstring.c_str());
// R script parameters
Rscript_path = find_header_element ("Rscript_path", true);
progress_utility_name = find_header_element ("progress_utility_name", true);
returnstring = find_header_element ("on_the_fly_progress_updates", false);
if (returnstring == "yes") {
on_the_fly_progress_updates = true;
} else {
//.........这里部分代码省略.........