Simple c++ program

Before discussing how to submit jobs, we need to create a sample program we are going to run on the grid. To compile the short c++ programme below, first save it (on the gridui) in a file simple.cc as well as save the file input.txt below. Then run

g++ -std=c++0x simple.cc -o simple

and to execute the program

./simple input.txt

This produces the file output.txt with content identical to input.txt

1. Source: simple.cc

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

//**
// This is a simple c++ program for grid tutorial.
// This program takes a text file as an input and
// saves a number from the first line to another
// file.
//
// To compile run: g++ -std=c++0x simple.cc -o simple
//
// Notice the -std=c++0x flag which you need if you use
// the defaul (old) c++ compiler on gridui.
//**

int main(int argc, char **argv)
{

  std::string infile = argv[1];
  
  std::ifstream myfile;
  myfile.open(infile);
  
  if(myfile.is_open()){
    
    std::ofstream outfile("output.txt");

    std::string line;
    while(std::getline(myfile,line)){

      // Here we assume that the input file is valid
      // and contains a number per line.

      std::istringstream stream(line);
      int numb;
      stream >> numb;

      outfile << numb << std::endl;
    
    }

    outfile.close();

  }else{

    std::cerr << "No input file found" << std::endl;
    exit (EXIT_FAILURE);

  }

  
  return 0;
}

2. Input file: input.txt

1
2
3
4