【问题标题】:Opening a File and putting the data into a double vector, C++打开文件并将数据放入双向量 C++
【发布时间】:2016-07-16 20:13:11
【问题描述】:

我是 C++ 的初学者,在我的程序开始时,我试图将来自 csv 文件的数据输入到双向量中。

但是,当我尝试运行程序时,我不断收到“行”和“列”未在此范围内声明。

我试图通过放置 ' int row; 来解决这个问题。国际单位; '上面'字符串输入[row][col];'尝试解决问题。
但后来我得到错误“数组绑定不是']'标记之前的整数常量”。

我想知道如何解决这个问题?或者,如果有一些我没有意识到的遗漏。

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;


string input[row][col];
const int ROW = 10;
const int COL = 4;
string input[ROW][COL];
const string datatable = "table2.csv";
string line;
int col = 0;
int row = 0;


void readCSV()   ////this is the function to read the file
{
    ifstream file(datatable);   ///opens file
    while (getline(file, line))
    {
        istringstream iss(line); //takes out white
        string result;
        while (getline(iss, result, ','))
        {
            input[row][col] = result; // creates string for element
            col = col + 1;
        }
        row = row + 1;
        col = 0;

    }

【问题讨论】:

    标签: c++ arrays file


    【解决方案1】:

    希望您不要认为这是冒犯性的,但这里有多个错误。

    我将首先评论您的代码以显示错误所在,然后我将解释问题:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    
    string input[row][col]; /* <- "row" and "col" are not yet defined */
    const int ROW = 10;
    const int COL = 4;
    string input[ROW][COL]; /* <- you already have a variable named "input" */
    const string datatable = "table2.csv";
    string line;
    int col = 0;
    int row = 0;
    
    
    void readCSV()
    {
        ifstream file(datatable);   ///opens file
        while (getline(file, line)) /* <- no check to make sure max rows are not exceeded */
        {
            istringstream iss(line); //takes out white
            string result;
            while (getline(iss, result, ',')) /* No check to make sure max columns are not exceeded */
            {
                input[row][col] = result; // creates string for element
                col = col + 1;
            }
            row = row + 1;
            col = 0;
    
        }
    

    要修改这些,我会做以下事情:

    1. 我会删除 string input[row][col],因为它稍后会正确定义。
    2. 我会将while (getline(file, line)) 更改为while (getline(file, line) &amp;&amp; row &lt; ROW)
    3. 我会将while (getline(iss, result, ',')) 更改为while (getline(iss, result, ',') &amp;&amp; col &lt; COL)

    我不保证这些是唯一的问题,并且在纠正这些问题后代码将完美运行。 (我很少再用 C++ 编程了,所以我不能保证 100% 有信心。)但是,这些只是我立即注意到的一些初始错误/问题。


    编辑:作为补充说明,我同意发布的另一个答案(现在似乎已被删除),该答案建议将 ROW 和 COL 更改为预处理器定义/宏。例如,const int ROW = 10 将变为 #define ROW 10,const int COL = 4 将变为 #define COL 4。在你这里的例子中,它不会引起巨大的变化,但这里的好处是ROW和COL不会作为变量占用内存空间,因为编译器将替换所有对COL和ROW的引用字面值分别定义为 4 和 10。

    不过,这在某种程度上是基于偏好的建议,因此请随意做任何您觉得更舒服的事情。仅基于您提供的代码的 sn-p,您可能无论如何都不会看到任何性能变化。但是,我想就此发表个人意见,因为另一个答案表明了这一点。

    【讨论】:

      【解决方案2】:

      至少,您需要删除第一个

      string input[row][col];
      

      毕竟,input 被(正确地)声明了几行。

      【讨论】:

        猜你喜欢
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多