问题描述
X,Y,Z
0,0
0,0
-0.00624347,-0.0182673,1.00063
-0.00845628,-0.0374925,1.00058
-0.00494793,-0.0295639,0.927447
-0.00285682,-0.0926582,0.885783
-0.00832563,-0.02957,0.697834
我想把它放入分别对应于X列、Y列和Z列的三个gsl_vectors(来自GSL:https://www.gnu.org/software/gsl/doc/html/vectors.html)。我之所以要这样做,是因为我以后想对这些数据使用 GNU 科学库中实现的函数。我想强调的是,这些函数只能在 gsl_vectors 上工作,而不能在 std:vectors 上工作。
我的方法:
- 将 .csv 中的数据放入 std:vectors,然后将这些向量转换为 gsl_vectors。那没有用。
- 将 .csv 文件中的数据直接放入 gsl_vectors:
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <sstream>
#include<algorithm>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_vector.h>
struct acceleration {
gsl_vector AccX;
gsl_vector AccY;
gsl_vector AccZ;
};
//function prototypes
acceleration read_csv(acceleration& A);
//end function prototypes
int main(void)
{
// ==================READ THE CSV=========================
acceleration data;
data = read_csv(data);
printf("/n");
gsl_vector_fprintf(stdout,&data.AccX,"%lf");
return 0;
}
acceleration read_csv(acceleration& SA)
{
std::string buffer;/* Declare a buffer for the data that will be read*/
std::string bacx,bacy,bacz;
std::ifstream inputfile;
inputfile.open("buffer.csv"); /* open file for reading*/
if (!inputfile.is_open())
{
std::cout << "Error opening file" << std::endl;
}
std::stringstream aux(buffer);
getline(aux,bacx,',');
getline(aux,bacz,');
size_t i{ 0 };
while (getline(inputfile,buffer))
{
std::stringstream aux(buffer);
getline(aux,');
if (bacx.compare("AX") != 0)
gsl_vector_set(&SA.AccX,i,stod(bacx));
getline(aux,');
if (bacy.compare("AY") != 0)
gsl_vector_set(&SA.AccY,stod(bacy));
getline(aux,');
if (bacz.compare("AZ") != 0)
gsl_vector_set(&SA.AccZ,stod(bacz));
i++;
}
inputfile.close();
return (SA);
}
这在控制台上没有输出,如果我调试它,函数 gsl_vector_set
会抛出异常:
在 progr.exe 中的 0x7A5EED1A (gsld.dll) 处抛出异常:0xC0000005:访问冲突写入位置 0x3333332
在gsl_set_vector
的行:v->data[i * v->stride] = x;
- 将 .csv 数据放入 gsl_block 中,然后将其切片为 gsl_vectors。在这里,我在尝试将数据放入块时遇到异常。然后,为了将块切成向量,我假设我必须使用
gsl_vector_alloc_from_block()
函数,但我没有找到任何关于如何使用这个函数的例子。我确实需要了解其他人通常如何使用函数,因为我是 C++ 新手。 到目前为止,我对这个想法的看法如下:
#include <iostream>
#include <fstream>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_vector.h>
//function prototypes
gsl_block read_csv(void);
//end function prototypes
int main(void)
{
// ==================READ THE CSV INTO A BLOCK=========================
gsl_block data;
data = read_csv();
// =================Now SLICE THE BLOCK: HOW?==========================
// use gsl_vector_alloc_from_block() but how?
return 0;
}
//function declarations
gsl_block read_csv(void)
{
FILE* inputfile;
fopen_s(&inputfile,"buffer.csv","r"); /* open file for reading*/
if (inputfile == NULL)
std::cout << "file does not exist \n";
fseek(inputfile,0L,SEEK_END); //go until the end
int file_size = ftell(inputfile); //in order to tell the size of the file
gsl_block* b = gsl_block_alloc(file_size);
if(inputfile)
gsl_block_fscanf(inputfile,b);
fclose(inputfile);
return *b;
}
//end function declarations
如果我运行这个,我得到:
在控制台上显示:
gsl: C:\DEV\vcpkg\buildtrees\gsl\src\gsl-2-fb511965d5.clean\block\fprintf_source.c:90: 错误: fscanf 失败 调用了默认的 GSL 错误处理程序。
如果我调试它,gsl_error
函数会抛出异常:
progr.exe 触发了断点。
在abort ();
总而言之,我真正想要的是将 .csv 读入 gsl_vectors。如果我的方法不行,那是可以理解的。但我希望有更多有经验的程序员可以帮助我摆脱困境。谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)