Page History
...
Code Block |
---|
#include <string> std::string basename(const std::string& path) { size_t pos = path.find_last_of('/'); //if (pos == std::string::npos) return std::string(); if (pos < path.size()-1) return std::string(path,pos+1); else return std::string(); } |
...
Input from file value by value
Code Block |
---|
#include <sys/stat.h><fstream> // for ifstream bool dir_exists(const std::ifstream f("file.txt"); std::string& dir)val; while (f>>val) { struct stat st; if( (stat(dir.c_str(),&st) == 0) && (((st.st_mode) & S_IFMT) == S_IFDIR) ) return true; return false; } |
Check if file exists
Code Block |
---|
#include <fstream> // for ifstream
std::ifstream f("file.txt");
if(not f.good()) ...
|
Input from file value by value
Code Block |
---|
#include <fstream> // for ifstream std::ifstream f("file.txt"); std::string val; while (f>>val) { std::cout << "val: " << val << std::cout << "val: " << val << '\n'; } f.close(); |
Input from file line by line
...
Code Block |
---|
#include <getopt.h> #include <stdio.h> #include <iostream> //=================== void usage(char* name) { std::cout << "Usage: " << name << " [-a <aaa>] [-b <bbb>] [-c <ccc>] [-h] <p1> [<p2> [<p3> ...]] \n"; } //=================== int main (int argc, char **argv) { char *avalue = NULL; char *bvalue = NULL; char *cvalue = NULL; int index; int c; extern char *optarg; extern int optind, optopt, opterr; while ((c = getopt (argc, argv, ":a:b:c:h")) != -1) switch (c) { case 'a': avalue = optarg; printf ("a: avalue = %s\n",avalue); break; case 'b': bvalue = optarg; printf ("b: bvalue = %s\n",bvalue); break; case 'c': cvalue = optarg; printf ("c: cvalue = %s\n",cvalue); break; case 'h': printf ("h: "); usage(argv[0]); break; case ':': printf ("(:) Option -%c requires an argument.\n", optopt); usage(argv[0]); return EXIT_FAILURE; case '?': printf ("?: Option -%c requires an argument.\n", optopt); usage(argv[0]); return EXIT_FAILURE; default: printf ("default: You should not get here... option -%c .\n", optopt); abort default:(); } printf ("defaultEnd of options: You should not get here... option -%c .avalue = %s, bvalue = %s, cvalue = %s\n", optopt); avalue, abortbvalue, (cvalue); for (index = optind; index < argc; index++) } printf ("End of options: avalue = %s, bvalue = %s, cvalue = %s\n", avalue, bvalue, cvalue); for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]); return EXIT_SUCCESS; } Non-option argument %s\n", argv[index]); return EXIT_SUCCESS; } |
Check if directory exists
Code Block |
---|
#include <sys/stat.h>
bool dir_exists(const std::string& dir)
{
struct stat st;
if( (stat(dir.c_str(),&st) == 0) && (((st.st_mode) & S_IFMT) == S_IFDIR) ) return true;
return false;
} |
Check if file exists
Code Block |
---|
#include <fstream> // for ifstream
std::ifstream f("file.txt");
if(not f.good()) ...
|
Iterate over files in the directory
...
Overview
Content Tools