Skip to content

DelimitedTextWriter

vtk-examples/Cxx/InfoVis/DelimitedTextWriter

Description

Is the output of

"","",""
0,1,2
3,4,5
6,7,8

expected? What is the first line indicating? Simply that there are 3 string fields?

With my prior edit, the first line is now "column-0","column-1","column-2" -- the first line is the names of the column arrays in the table. I added them to the example because the example was crashing on Windows builds, where streaming a NULL char* is no bueno. (David Cole)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

DelimitedTextWriter.cxx

#include <vtkDelimitedTextWriter.h>
#include <vtkNew.h>
#include <vtkTable.h>
#include <vtkVariantArray.h>

#include <sstream>
#include <string>

int main(int argc, char* argv[])
{
  std::string outputFilename = "output.txt";

  // Use the specified filename if it is provided.
  if (argc == 2)
  {
    outputFilename = argv[1];
  }

  // Construct an empty table
  vtkNew<vtkTable> table;

  for (unsigned int i = 0; i < 3; i++)
  {
    vtkNew<vtkVariantArray> col;

    std::ostringstream oss;
    oss << "column-" << i;
    std::string colName = oss.str();
    col->SetName(colName.c_str());

    col->InsertNextValue(vtkVariant(0.0));
    col->InsertNextValue(vtkVariant(0.0));
    col->InsertNextValue(vtkVariant(0.0));
    table->AddColumn(col);
  }

  // Fill the table with values
  unsigned int counter = 0;
  for (vtkIdType r = 0; r < table->GetNumberOfRows(); r++)
  {
    for (vtkIdType c = 0; c < table->GetNumberOfColumns(); c++)
    {
      table->SetValue(r, c, vtkVariant(counter));
      counter++;
    }
  }

  vtkNew<vtkDelimitedTextWriter> writer;
  writer->SetFileName(outputFilename.c_str());
  writer->SetInputData(table);
  writer->Write();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DelimitedTextWriter)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOCore
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DelimitedTextWriter: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(DelimitedTextWriter MACOSX_BUNDLE DelimitedTextWriter.cxx )
  target_link_libraries(DelimitedTextWriter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS DelimitedTextWriter
  MODULES ${VTK_LIBRARIES}
)

Download and Build DelimitedTextWriter

Click here to download DelimitedTextWriter and its CMakeLists.txt file. Once the tarball DelimitedTextWriter.tar has been downloaded and extracted,

cd DelimitedTextWriter/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./DelimitedTextWriter

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.