用于读取csv写入数组的c ++程序;然后操作并打印到文本文件中(已经用matlab编写) [英] c++ program for reading csv writing into array; then manipulating and printing into text file (already written in matlab)
问题描述
想知道是否有人可以帮助我尝试构建一个程序,以从csv文件读取大小未知的浮点数据块。我已经在MATLAB中编写了此文件,但想编译并分发到C ++。
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
7,5,1989
2,4,2312
来自文本文件。
个代码。
#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;
}
我使用的Matlab代码是>>
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,:);
感谢totrace修复了我在c ++代码上遇到的初始编译错误,但是我正在寻找继续的技巧帮助,如果有人可以帮我,那真是太棒了。
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.
道具,
Alex Byasse
Props, Alex Byasse
推荐答案
更改此行:
array [row] [col] = atoi(result.c_str);
to :
array [row] [col] = atoi(result.c_str());
c_str()是一种方法,因此需要括号才能调用它。参见 http://en.cppreference.com/w/cpp/string/basic_string/ 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
这篇关于用于读取csv写入数组的c ++程序;然后操作并打印到文本文件中(已经用matlab编写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!