Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ program for reading csv writing into array; then manipulating and printing into text file (already written in matlab)

was wondering if someone could give me a hand im trying to build a program that reads in a big data block of floats with unknown size from a csv file. I already wrote this in MATLAB but want to compile and distribute this so moving to c++.

Im just learning and trying to read in this to start

7,5,1989
2,4,2312

from a text file.

code so far.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <stdlib.h>

const int ROWS = 2;
const int COLS = 3;
const int BUFFSIZE = 80;

int main() {
  int array[ROWS][COLS];
  char buff[BUFFSIZE];
  std::ifstream file( "textread.csv" );
  std::string line; 
  int col = 0;
  int row = 0;
  while( std::getline( file, line ) )
  {
    std::istringstream iss( line );
    std::string result;
    while( std::getline( iss, result, ',' ) )
      {
        array[row][col] = atoi( result.c_str() );
        std::cout << result << std::endl;
        std::cout << "column " << col << std::endl;
        std::cout << "row " << row << std::endl;
        col = col+1;
      }
    row = row+1;
    col = 0;
  }
  return 0;
}

My matlab code i use is >>

fid = fopen(twoDM,'r');

s = textscan(fid,'%s','Delimiter','\n');
s = s{1};
s_e3t = s(strncmp('E3T',s,3));
s_e4q = s(strncmp('E4Q',s,3));
s_nd = s(strncmp('ND',s,2));

[~,cell_num_t,node1_t,node2_t,node3_t,mat] = strread([s_e3t{:}],'%s %u %u %u %u %u');
node4_t = node1_t;
e3t = [node1_t,node2_t,node3_t,node4_t];
[~,cell_num_q,node1_q,node2_q,node3_q,node_4_q,~] = strread([s_e4q{:}],'%s %u %u %u %u %u %u');
e4q = [node1_q,node2_q,node3_q,node_4_q];
[~,~,node_X,node_Y,~] = strread([s_nd{:}],'%s %u %f %f %f');

cell_id = [cell_num_t;cell_num_q];
[~,i] = sort(cell_id,1,'ascend');

cell_node = [e3t;e4q];
cell_node = cell_node(i,:);

Thanks to totrace for fixing the initial compile error i had on my c++ code but im looking for continued tips help, and If someone could give me a hand that would be awesome.

Props, Alex Byasse

like image 294
Alex Byasse Avatar asked Sep 13 '13 02:09

Alex Byasse


1 Answers

Change this line: array[row][col] = atoi( result.c_str );

to: array[row][col] = atoi( result.c_str() );

c_str() is a method, so you need parenthesis to call it. See http://en.cppreference.com/w/cpp/string/basic_string/c_str

like image 69
totrace Avatar answered Oct 04 '22 20:10

totrace