小编Ale*_*sse的帖子

用于读取csv写入数组的c ++程序; 然后操纵并打印成文本文件(已经用matlab编写)

我想知道是否有人可以给我一只手试图建立一个程序,从csv文件中读取大小未知的浮动数据块.我已经在MATLAB中写了这个,但是想要编译和分发这个,所以转向c ++.

我只是学习并尝试阅读这个开始

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() …
Run Code Online (Sandbox Code Playgroud)

c++ csv import matlab parsing

5
推荐指数
1
解决办法
9152
查看次数

当读取错误的类型时,cin会覆盖我的初始化值?

所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:

#include "..\std_lib_facilities.h"

int main()
{
    cout << "Please enter your first name and age\n";
    string first_name = "???"; // string variable
                               // ("???” means “don’t know the name”)
    int age = -1;              // integer variable (1 means “don’t know the age”)
    cin >> first_name >> age;  // read a string followed by an integer
    cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)

当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?

编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …

c++ cin language-lawyer c++11

4
推荐指数
1
解决办法
269
查看次数

c ++与MATLAB优化速度.例如,我的matlab代码比c ++运行得更快?

我编写了一个程序,它读入带有单元顶点坐标的ascii文件,然后输出一个带有单元格中心的文件.有什么方法可以加快c ++代码,至少匹配我的matlab性能.我在我的c ++代码中使用push_back向量,但我不知道如何实现与数组类似?

我试图使用困难进行性能分析,但是如何在设置程序之前开始进行性能分析,它只能编程已经运行的运行,输出也很混乱,不要去我的代码行但是像std这样的行:: _ Vector_cal> ....等.什么是独占和包容的区别,最高的%独占名称是运营商删除,第二是运营商新????

我的c ++来源:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
    std::vector<double> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    return ret;
}

std::vector<int> GetValues_c(const std::vector<std::string>& src, int start, int end)
{
    std::vector<int> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::atoi(src[i].c_str()));
    }
    return ret;
}

std::vector<double> polycentre(const std::vector<double>&  x,const …
Run Code Online (Sandbox Code Playgroud)

c++ optimization performance matlab

2
推荐指数
1
解决办法
748
查看次数

C ++程序,用于读取数组中具有恒定(但未知)列数的未知大小的csv文件(仅用浮点数填充)

我想知道是否有人可以帮助我尝试构建一个程序,以从csv文件读取大小未知的浮点大数据块。我已经在MATLAB中编写了此代码,但希望对其进行编译和分发,因此转向c ++。

我只是学习并尝试阅读以开始

7,5,1989
2,4,2312
Run Code Online (Sandbox Code Playgroud)

从文本文件。

到目前为止的代码。

// Read in CSV
//
// Alex Byasse

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

int main() {

    unsigned int number_of_lines = 0;
    FILE *infile = fopen("textread.csv", "r");
    int ch;
    int c = 0;
    bool tmp = true;
    while (EOF != (ch=getc(infile))){
      if(',' == ch){
    ++c;
      }
      if ('\n' == ch){
    if (tmp){
      int X = c;
      tmp = false;
    }
            ++number_of_lines;
    }
    }
    fclose(infile);

  std::ifstream file( "textread.csv" …
Run Code Online (Sandbox Code Playgroud)

c c++ csv file-io matlab

1
推荐指数
1
解决办法
9410
查看次数

c ++编译器错误无法访问类'std :: basic_ios <_Elem,_Traits>'中声明的私有成员

嘿我得到一个错误我认为与从阅读其他帖子复制流变量有关,我试图改变

std::ofstream outfil;
Run Code Online (Sandbox Code Playgroud)

std::ofstream & outfil;
Run Code Online (Sandbox Code Playgroud)

但我得到错误

reference value "outfil" requires an initializer
Run Code Online (Sandbox Code Playgroud)

代码是:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
    std::vector<double> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    return ret;
}

std::vector<int> GetValues_c(const std::vector<std::string>& src, int start, int end)
{
    std::vector<int> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::atoi(src[i].c_str()));
    }
    return …
Run Code Online (Sandbox Code Playgroud)

c c++ private-members

0
推荐指数
1
解决办法
4729
查看次数

GDB:看到源代码行?

使用-g命令编译的任何程序是否都有可供gbd列出的源代码,即使源代码文件不可用?另外,当您在具有复杂多源文件结构的程序中的某一行设置断点时,您是否需要源代码文件的名称?

debugging fortran gdb gfortran

0
推荐指数
1
解决办法
167
查看次数