本文整理汇总了C++中ifstream::fail方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::fail方法的具体用法?C++ ifstream::fail怎么用?C++ ifstream::fail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ifstream
的用法示例。
在下文中一共展示了ifstream::fail方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: openInput
int openInput(ifstream& opIn,string unsortfile){
opIn.open(unsortfile.c_str());
if(opIn.fail()){
cout<<unsortfile<<" does not exist.\n";
opIn.close();
return 0;
}
else return 1;
}
示例2: encrypt
//*****************************************************
//Encrypt function uses the virtual transform *
//member function to transform individual characters. *
//*****************************************************
void Encryption::encrypt()
{
char ch;
char transCh;
inFile.get(ch);
while (!inFile.fail())
{
transCh = transform(ch);
outFile.put(transCh);
inFile.get(ch);
}
inFile.clear();
inFile.seekg(0);
}
示例3: OpenFiles
void OpenFiles (ifstream &Inp, ofstream &Out)
{
char Fname[255];
do
{
cout << "Input file name: " << flush;
cin.getline(Fname, 255);
Inp.clear();
Inp.open(Fname, ios::in); //Inp.open(Fname, ios::in|ios::nocreate);
if (Inp.fail())
cout << "Failed to open " << Fname << " --- try again\n";
} while ( Inp.fail() );
do
{
cout << "Output file name: " << flush;
cin.getline(Fname, 255);
Out.clear();
Out.open(Fname);
if (Out.fail())
cout << "Failed to open hw2output.txt " << Fname << " --- try again\n";
} while ( Out.fail() );
}
示例4: openInFile
/********************************
** Function: void openInFile(ifstream &fin, string str);
** Description: Open the file and check for errors
** throw exception if file open fails
**
**
** Parameters: ifstream fin is the file handle
** string str is the name of the file to open
**
** pre-conditions: fin has been declared, str is a valid file name
** post-conditions: file opened and checked for errors in opening
********************************/
void openInFile(ifstream &fin, char str[])
{
// can't open empty names
if (str[1] == '\0') throw (-1);
// attempt to open file
fin.open(str);
if (fin.fail())
{
cout << "SOMETHING WENT TERRIBLY WRONG! FAILED TO OPEN INPUT FILE"
<< endl;
throw(-1);
}
return;
}
示例5: InputNumScores
int InputNumScores(ifstream& input)
{
int numScores;
string fileName = "c";
cout << "What file will you be reading from, sir or madame?\n";
cin >> fileName;
input.open(fileName.c_str());
if (input.fail())
{
cout << "File read error.\n";
exit(1);
}
input >> numScores;
return numScores;
}
示例6: connectFileAndGetNumLines
// Pre Condition: name is the file that will be attempted to open with ifs
// Post Condition: Returns the number of lines in the file, or -1 for fail to open
// closes ifs
int connectFileAndGetNumLines(ifstream &ifs, string name) {
ifs.open(name);
int numLines = 0;
string trash;
// if open fails return -1
if (ifs.fail())
return -1;
else
while (getline(ifs, trash))
numLines++;
ifs.close();
return numLines;
}
示例7: ReadScores
void ReadScores(ifstream &in1, Vector<int> &scores) {
while (true) {
int x;
in1 >> x;
if (in1.fail()) {
break; // no more lines to read
}
cout << x << endl;
//Input can haveletters, punctuation or whitespace, but only integers between 0 and 99 are accepted.
if (x >= 0 && x <= 99) {
scores[x / 10] += 1;
}
}
}
示例8: getEdges
vector<Edge> getEdges(ifstream & input){
vector<Edge> edges;
while(true){
int start;
int end;
input>>start;
input>>end;
if(input.fail())
break;
Edge currEdge;
currEdge.start=start;
currEdge.end=end;
edges.push_back(currEdge);
}
return edges;
}
示例9: openFiles
void openFiles(char *argv[], ifstream &in, ofstream &out)
{
in.open(argv[1]);
if (in.fail())
{
cerr << endl << "*** ERROR: Can not open " << argv[1] << " for input." << endl;
exit(0);
}
out.open(argv[2]);
if (out.fail())
{
cerr << endl << "*** ERROR: Can not open " << argv[2] << " for output." << endl;
exit(0);
}
}
示例10: reverse
void reverse(ifstream &filein){
string buffer;
filein >> buffer;
if(filein.fail()){
return;
}
else{
reverse(filein);
cout<<buffer<<endl;
}
return;
}
示例11: inputFileValCounter
void inputFileValCounter(ifstream& in, int numVal[])
{
int valueCount = 0;
int value;
while (!in.eof())
{
in >> value;
if (!in.fail())
valueCount++;
}
numVal[0] = valueCount; //stores count for later use
cout << "I counted " << valueCount << " values.\n" << endl;
}
示例12: CountMap
void CountMap(ifstream & instr) {
Map<string, int> myMap;
string nxtword;
while (true) {
instr >> nxtword;
if (instr.fail()) break;
else {
if (myMap.containsKey(nxtword)) {
int count = myMap.get(nxtword);
myMap[nxtword] = count + 1;
} else
myMap[nxtword] = 1;
}
}
PrintMap(myMap);
}
示例13: ReadScores1
void ReadScores1(ifstream &in1, Vector<int> &scores) {
while (true) {
string line;
getline(in1, line);
if (in1.fail()) {
break; // no more lines to read
}
cout << line << endl;
//Input line must be well form. Only digits. No letters, punctuation or whitespace.
int x = StringToInteger(line);
if (x >= 0 && x <= 99) {
scores[x / 10] += 1;
}
}
}
示例14: CountNonblankLines
int CountNonblankLines( ifstream& ifg )
{
char buf[400]="";
streampos pos = ifg.tellg();
int nLines = 0;
ifg.getline( buf, ELEMENTS(buf) );
do{
trim(buf);
if( buf[0]!=NULL )nLines++;
ifg.getline( buf, ELEMENTS(buf) );
}while( !ifg.fail() );
ifg.clear();
ifg.seekg( pos, ios::beg );
return nLines;
}
示例15: check_files
void check_files( ifstream &file )
{
char buffer[ 129 ];
char file_name[ 81 ];
char revision[ 20 ];
unsigned long crc;
for ( ; ; ) {
file.getline( buffer, 129 );
if ( file.fail() || file.eof() )
return;
if ( buffer[ 0 ] != ';' && buffer[ 0 ] != ' ' && buffer[ 0 ] != '\0' ) {
istrstream s( buffer );
s >> file_name;
s >> hex >> crc;
s >> revision;
check_file( file_name, crc, revision );
} else