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; }
//***************************************************** //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); }
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() ); }
/******************************** ** 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; }
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; }
// 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; }
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; } } }
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; }
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); } }
void reverse(ifstream &filein){ string buffer; filein >> buffer; if(filein.fail()){ return; } else{ reverse(filein); cout<<buffer<<endl; } return; }
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; }
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); }
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; } } }
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; }
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
void Encrypt::doFilter(ifstream &inValue, ofstream &outValue, string &inputFile, string &outputFile) { int keyVal; char input, ch; cout << "What is the offset value? " << endl; cin >> keyVal; inValue.open(inputFile.c_str(), ios::in); outValue.open(outputFile.c_str(), ios::out | ios::app); if(inValue.get(input)) while(!inValue.fail()) { if((input == ' ')||(input == '.')) { outValue << input; } else if(input == ',') { outValue << input; } else { ch = transform(input, keyVal); outValue.flush(); outValue << ch; } inValue.get(input); } else { cout << "Error opening the input file." << endl; inValue.close(); outValue.close(); } }
bool CountLetters(ifstream& input, map<char,int>& table) { char tmp; while(true) { input.get(tmp); if(input.eof()&&input.fail()) break; table[tmp]+=1; } for(map<char,int>::iterator itr=table.begin(); itr!=table.end(); ++itr) { cout << itr->first <<" : " << itr->second << endl; } return true; }
/* Function: PromptUserToOpenFile Usage: PromptUserToOpenFile(infile) ----------------------------------- User types a filename, this function opens the file. */ void PromptUserToOpenFile(ifstream &infile) { bool file_open = false; while (! file_open) { // Prompt the user for the name of a file of exam scores to compile cout << "Enter filename (RETURN to quit): "; //string filename = testfile; cout << endl; // for debugging string filename = GetLine(); if (filename == "") break; infile.open( filename.c_str() ); if (infile.fail()) { cout << "Could not open file: '" << filename << "'" << endl; infile.clear(); } else { file_open = true; } } }
bool loadGraph(string fp, int& size, queue<string>& graph) { string extract; if (isQuit(fp)) { return false; } input.open(fp.c_str()); if (input.fail()) { printf("Error loading file <%s>. Please try again.\n", fp.c_str()); return false; } input >> size; while(input.good()) { input >> extract; graph.push(extract); } return true; }
bool readInput(ifstream& inputFile, const string& filename, vector<soundtrack>& cdVector, BinarySearchTree<ItemType>& BST) { inputFile.open(filename); if (inputFile.fail()) return false; else { while (!inputFile.eof()) { soundtrack* cd = new soundtrack; inputFile >> *cd; cdVector.push_back(*cd); BST.add(cd->getDateReleased()); delete cd; cd = nullptr; } inputFile.close(); } return true; } // end readInput
void process_name(ifstream& in_file, double& total, ofstream &out) { string name; int count; double percent; in_file >> name >> count >> percent; if (in_file.fail()) { cout << "Error processing name." << endl; cin.get(); return; } // Check for failure after each input if (out.fail()) { cout << "Error process_name writing to file"; } if (total > 0) { cout << name << " "; out << name << " "; } total = total - percent; }
// Input: argc - the number of arguments on the command line // argv - the argument vector // Output: ifs - the opened input file stream // ofs - the opened output file stream bool openfiles(int argc,char *argv[],ifstream &ifs,ofstream &ofs) { if (argc != 3) // check to see that there are 3 command line args { cerr << "usage: " << argv[0] << " <inputfile> <outputfile>\n"; return false; } ifs.open(argv[1]); // open the input file if (ifs.fail()) { cerr << "error opening inputfile: '" << argv[1] << "' doesn't exist?\n"; return false; } ofs.open(argv[2]); // open the output file if (ofs.fail()) { cerr << "error opening outputfile\n"; return false; } return true; // opening the files was successful }
bool openResourceFile(const char *filename) { resourceIfs.open(filename, ios::binary); if (resourceIfs.fail()) { return false; } if (readInt(resourceIfs) != 0x255435f4) { return false; } resFiles.resize(readInt(resourceIfs)); for (resource &res : resFiles) { resourceIfs.read(res.res_id, ID_MAXSIZE); res.size = readInt(resourceIfs); res.ptr = readInt(resourceIfs); } return true; }
/*********************************************************************** * This function will allow reading and writing to input and output * files. It uses a call to a virtual function "transform()" to allow * its use as a file filter. **********************************************************************/ void fileFilter::doFilter(ifstream &in, ofstream &out) { char ch; // Variable to hold the input character char transCh; // Varialbe to hold the output character // Get first character from input file in.get(ch); // Continue to get and operate on characters in file until EOF reached while (!in.fail()) { // Call virtual transform() function to perform desired task based on class of object transCh = transform(ch); // Write operated on character to output file out.put(transCh); // Get next character in input file in.get(ch); } }
//FUNCTION DEFINITIONS void openStreams(ifstream & fin_par, ofstream & fout_par) { string infile, outfile; cout << "\nPlease enter the name of your input file:\n"; cin >> infile; cout << "\nPlease enter the name of your output file:\n"; cin >> outfile; fin_par.open(infile); if(fin_par.fail()) { cout << "\nFailed to open input file."; exit(1); } fout_par.open(outfile); if(fout_par.fail()) { cout << "\nFailed to open output file."; exit(1); } }
void CMap::LoadMap() { printw("loading map\n"); file_in.open("map.txt"); if(file_in.fail()) printw("couldn't open\n"); else printw("loaded map\n"); string temp; while(!file_in.eof()) { getline(file_in, temp); Map.push_back(temp); } file_in.close(); for(int i = 0; i < Map.size(); i++) for(int j = 0; j < Map[i].size(); j++) { if(Map[i][j] == ' ') TileMap.push_back(Tile(' ', i, j, "wall", false)); else if(Map[i][j] == '-') TileMap.push_back(Tile('-', i, j, "wall", false)); else if(Map[i][j] == '|') TileMap.push_back(Tile('|', i, j, "wall", false)); else if(Map[i][j] == 'H') TileMap.push_back(Tile('H', i, j, "hall", true)); else if(Map[i][j] == '.') { TileMap.push_back(Tile('.', i, j, "floor", true)); } else if(Map[i][j] == '~') { TileMap.push_back(Tile('~', i, j, "water", true)); } else if(Map[i][j] == 'L') { TileMap.push_back(Tile('~', i, j, "lava", true)); } } }
uint Genome::OpenStream(string name, ifstream & stream) { stream.open((P->genomeDir+ "/" +name).c_str(), ios::binary); if (!stream.good()) { ostringstream errOut; errOut << "EXITING because of FATAL ERROR: could not open genome file "<< P->genomeDir << "/" << name <<"\n" << endl; errOut << "SOLUTION: check that the path to genome files, specified in --genomeDir is correct and the files are present, and have user read permsissions\n" <<flush; exitWithError(errOut.str(),std::cerr, P->inOut->logMain, EXIT_CODE_GENOME_FILES, *P); }; uint size = 0; P->inOut->logMain << "checking " << name << " size"; stream.seekg (0, ios::end); size=(uint) stream.tellg(); stream.clear(); stream.seekg (0, ios::beg); P->inOut->logMain << "file size: "<< size <<" bytes; state: good=" <<stream.good()\ <<" eof="<<stream.eof()<<" fail="<<stream.fail()<<" bad="<<stream.bad()<<"\n"<<flush; return size; }
int main(int argc,char *argv[]) { string nameIn; if (argc != 4) { cout << "Usage: combine inputfile outputfile $n" << endl << endl; return 0; } nameIn.assign(argv[1]); for (int i=0;i<atoi(argv[3]);i++) { input_is.open(nameIn.c_str()); if (input_is.fail()) { cout << "Bad file name: [" << nameIn << "]!" << endl << endl; return 0; } cout << "Combine(V2.0)" << endl; cout << "Reading " << nameIn << "..."; readfile(); input_is.close(); input_is.clear(); cout << "Done." << endl; nameIn=nextName(nameIn); } res_os.open(argv[2]); work(); res_os.close(); res_os.clear(); return 0; }
double GetInputDouble(ifstream& file) { string temp = ""; double number = 0; //get the line from the text file. (Just like with cin) getline(file, temp); //test if the file is at the end if (file.eof()) { throw IOException(ERROR_EOF, DEFAULT); return 0.0; } //test if input is bad, or failed else if (file.bad() || file.fail()) { throw IOException(ERROR_FILE_FAIL, DEFAULT); } number = ValidateDouble(temp); return number; } // End GetValidInputDouble()
int GetInputInt(ifstream& file) { string temp = ""; int number = 0; //get the line from the text file. (Just like with cin) getline(file, temp); //test if the file is at the end if (file.eof()) { throw IOException(ERROR_EOF, DEFAULT); } //test if input is bad, or failed else if (file.bad() || file.fail()) { throw IOException(ERROR_FILE_FAIL, DEFAULT); } //convert the validated temp string to integer //store as int number = ValidateInt(temp); return number; } // End GetValidInputInt()