mercredi 15 janvier 2020

C++ portability from Windows to Linux

I have been successfully using the following code in C++ on Windows (via CodeBlocks) and have recently attempted to use the same code on Linux (Ubuntu 18.04) also via CodeBlocks. The code appears to compile fine but fails on execution.

The purpose of the code is to import a comma delimited text file of numbers into an array.

In both Windows and Linux I am using the GNU GCC Compiler.

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <stdlib.h>

using namespace std;

typedef vector <double> record_t;
typedef vector <record_t> data_t;

istream& operator >> ( istream& ins, record_t& record)
  {

  record.clear();

  string line;
  getline( ins, line );

  stringstream ss( line );
  string field;
  while (getline( ss, field, ',' ))
    {
    stringstream fs( field );
    double f = 0.0;  
    fs >> f;

    record.push_back( f );
    }


  }

//-----------------------------------------------------------------------------

istream& operator >> ( istream& ins, data_t& data )
  {
  data.clear();

  record_t record;
  while (ins >> record)
    {
    data.push_back( record );
    }

  }

//-----------------------------------------------------------------------------

int main()
  {
  data_t data;

  ifstream infile( "Import File.txt" );
  infile >> data;

  if (!infile.eof())
    {
    cout << "Unsuccessful Import!\n";
    return 1;
   }

  infile.close();

  /cout << "Your file contains " << data.size()-1 << " records.\n";

return 4321;
  }

I've checked that the necessary header files exist on Linux and that appears to be the case.

If I comment out the EOF check the console returns the message that

Process returned 49 (0x31)

Grateful for any help in finding a solution.

Aucun commentaire:

Enregistrer un commentaire