gctl_examples/examples/text_io_ex.cpp
2024-09-10 20:19:20 +08:00

77 lines
3.3 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "gctl/core.h"
#include "gctl/io.h"
int main(int argc, char *argv[])
{
// Read space separated mixed table to a 2D string matrix, then save to a comma separated csv file
gctl::_2s_vector table;
gctl::text_descriptor desc;
gctl::read_text2vector2d("data/table_sample.txt", table, desc);
gctl::save_vector2d2text("data/out/table_sample_out.csv", table, ',');
// Read different columns into individual vectors that are of different types
std::vector<double> weights;
std::vector<std::string> names;
gctl::read_text2vectors("data/out/table_sample_out.csv", "0,3", ',', '#', 0, names, weights);
for (int i = 0; i < names.size(); ++i)
{
std::cout << names[i] << " weights " << weights[i] << " kg." << std::endl;
}
// Save vectors to a text file as individual columns
gctl::save_vectors2text("data/table_sample_out.txt", ' ', '#', nullptr, nullptr,
names, weights);
// Read a user-defined structure. One need to reload the >> operator for this to work
// Check the function's instruction for more information
std::vector<gctl::point3dc> pos;
gctl::read_text2vectors("data/random_topo.xyz", "0", ' ', '#', 0, pos);
for (int i = 0; i < 5; i++)
{
std::cout << pos[i] << std::endl;
}
std::cout << "Size: " << pos.size() << " ============" << std::endl;
//gctl::point3dc mean;
//mean = gctl::average(pos, gctl::point3dc(0, 0, 0));
//std::cout << mean << std::endl;
gctl::array<gctl::point3dc> pos2;
gctl::read_text2array("data/random_topo.xyz", pos2);
for (int i = 0; i < 5; i++)
{
std::cout << pos2[i] << std::endl;
}
std::cout << "Size: " << pos2.size() << " ============" << std::endl;
return 0;
}