Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created December 10, 2024 03:34
Show Gist options
  • Save abikoushi/b1fc6c0ad8d0765537445d4c4e56d967 to your computer and use it in GitHub Desktop.
Save abikoushi/b1fc6c0ad8d0765537445d4c4e56d967 to your computer and use it in GitHub Desktop.
get a line from csv (Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
// [[Rcpp::export]]
std::vector<double> readaline(std::string readtxt, int ind_n) {
std::ifstream file(readtxt);
std::string str;
std::vector<double> result;
int index = 0;
while (std::getline(file, str))
{
if(index == ind_n){
std::stringstream ss(str);
while( ss.good() )
{
std::string substr;
getline(ss, substr, ',');
result.push_back( stod(substr) );
}
}
index++;
if(index>ind_n){
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment