home
|
classes
|
snippets
|
examples
|
topics
|
tutorials
|
reference
[Open all]
[Close all]
Getting started
The example code given in the following tutorials can be compiled
by creating a directory called `myprograms` in the base directory
of the humlib library. Then if you create a file called
`myprograms/myprog.cpp`, you can type:
```console
make myprog
```
in the base directory to compile the program. The executable,
`myprog` will be created in the `bin` directory. Note that you may
need to compile the library first with the command:
```console
make
```
Reading Humdrum data
The HumdrumFile class is used to
store data from a Humdrum file, or a data string in Humdrum syntax.
To load content from a file, use the
HumdrumFileBase::read
function. The following program loads content from a file
called "file.krn" and then prints the content (as a standard
Humdrum file) to console out.
```cpp
#include "humlib.h"
int main(void) {
humlib::HumdrumFile infile;
infile.read("file.krn");
std::cout << infile;
return 0;
}
```
The Humdrum namespace prefix can be omitted by adding `using namespace Humdrum;`
at the top of the file:
```cpp
#include "humlib.h"
using namespace Humdrum;
using namespace std;
int main(void) {
HumdrumFile infile;
infile.read("file.krn");
cout << infile;
return 0;
}
```
The HumdrumFile class can also read from standard input as well as
input file streams or `stringstreams`.
```cpp
#include "humlib.h"
using namespace Humdrum;
using namespace std;
int main(void) {
HumdrumFile infile;
infile.read(cin);
cout << infile;
return 0;
}
```
Data can be read from an `ifstream` (or `stringstream`):
```cpp
#include "humlib.h"
using namespace Humdrum;
using namespace std;
int main(void) {
HumdrumFile infile;
ifstream instream;
instream.open("file.krn");
infile.read(instream);
cout << infile;
return 0;
}
```
To read content from a string, use the
HumdrumFileBase::readString
function (since a string is interpreted as a filename in the
read function).
```cpp
#include "humlib.h"
using namespace Humdrum;
using namespace std;
int main(void) {
HumdrumFile infile;
string contents;
contents = "**kern\n1c\n*-";
infile.readString(contents);
cout << infile;
return 0;
}
```
```cpp
#include "humlib.h"
using namespace Humdrum;
using namespace std;
int main(void) {
HumdrumFile infile;
const char* contents = "**kern\n1c\n*-";
infile.readString(contents);
cout << infile;
return 0;
}
```
More information about reading data can be found in the
reference manual.