school/cs236/submission/lab02/util.h
2016-04-06 20:46:32 -07:00

57 lines
1.6 KiB
C++

#ifndef __UTIL_H__
#define __UTIL_H__
#include <vector>
#include <iostream>
#include <fstream>
vector<string> open_file(string file_name) {
ifstream myfile;
vector<string> data;
myfile.open(file_name.c_str());
string temp;
while(!myfile.eof()) {
getline(myfile, temp);
data.push_back(temp);
}
myfile.close();
return data;
}
bool get_file_name(string input) {
bool file_correct = false;
string input_file_name;
while(!file_correct) {
ifstream inputs(input.c_str());
if(inputs.good()) {
input_file_name = input;
file_correct = true;
open_file(input_file_name);
return true;
}
else {
cerr << "incorrect file name" << endl;
return false;
}
}
return false;
}
void write_file(string output, string file_name) {
ofstream myfile;
myfile.open(file_name.c_str());
myfile << output << "\n";
}
void write_file(vector<string> output, string file_name) {
ofstream myfile;
myfile.open(file_name.c_str());
for(unsigned int i = 0; i < output.size(); i++) {
if(i != output.size() -1) {
myfile << output[i] << "\n";
}
myfile << output[i];
}
}
#endif