Created
December 19, 2018 16:37
-
-
Save hsiuhsiu/b24dc15a930d4a90391320da5a304cf2 to your computer and use it in GitHub Desktop.
[Read Files] files handling related snippet #file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <fstream> | |
| #include <sstream> | |
| using namespace std; | |
| int main(int argc, char *argv[]) { | |
| string input_filename = argv[1]; | |
| ifstream input_stream; | |
| input_stream.open(input_filename); | |
| string line; | |
| getline(input_stream, line); // Get title line | |
| while (getline(input_stream, line)) { | |
| // parse a line | |
| istringstream iss(line); | |
| string field; | |
| getline(iss, field, ','); | |
| int time = stoi(field); | |
| getline(iss, field, ','); | |
| string sym = field; | |
| getline(iss, field, ','); | |
| bool isSell = (field == "S"); | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/stat.h> | |
| bool is_file(const string path) { | |
| struct stat buf; | |
| stat(path.c_str(), &buf); | |
| return S_ISREG(buf.st_mode); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <dirent.h> | |
| // http://www.martinbroadhurst.com/list-the-files-in-a-directory-in-c.html | |
| void read_directory(const string& name, vector<string>& v) { | |
| DIR* dirp = opendir(name.c_str()); | |
| struct dirent * dp; | |
| while ((dp = readdir(dirp)) != NULL) { | |
| v.push_back(dp->d_name); | |
| } | |
| closedir(dirp); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bool is_alpha(const char c) { | |
| if (c >= 'a' && c <= 'z') return true; | |
| if (c >= 'A' && c <= 'Z') return true; | |
| return false; | |
| } | |
| vector<string> tokenize(string file_path) { | |
| ifstream is; | |
| is.open(file_path); | |
| string str; | |
| vector<string> vs; | |
| for (string::iterator pos, prev; getline(is, str); ) { | |
| for (pos = find_if(str.begin(), str.end(), is_alpha); pos != str.end(); | |
| pos = find_if(prev, str.end(), is_alpha)) { | |
| prev = find_if_not(pos, str.end(), is_alpha); | |
| string token(pos, prev); | |
| transform(token.begin(), token.end(), token.begin(), ::tolower); | |
| vs.push_back(token); | |
| } | |
| } | |
| is.close(); | |
| return vs; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment