Thursday, 12 September 2013

c++ program I'm writing for reading .csv (already written in matlab)

c++ program I'm writing for reading .csv (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 unknown size floats in a csv containing
spatial data about triangular and quad cells and then writes a csv with
cell centre locations. 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;
}
Im getting a compiler error >>
readcsv.cpp: In function 'int main()':
readcsv.cpp:28:46: error: argument of type 'const char*
(std::basic_string::)()const' does not match 'const char*'
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,:);
If someone could give me a hand that would be awesome.
Props, Alex Byasse

No comments:

Post a Comment