Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;来自csv的信息在数组中变得杂乱无章[][] 我是C++新手,实际上是学习我正在学习的硕士论文的作业。_C++_Csv - Fatal编程技术网

C++;来自csv的信息在数组中变得杂乱无章[][] 我是C++新手,实际上是学习我正在学习的硕士论文的作业。

C++;来自csv的信息在数组中变得杂乱无章[][] 我是C++新手,实际上是学习我正在学习的硕士论文的作业。,c++,csv,C++,Csv,首先,在我的代码中,我需要从csv文件中读取每一行和每一列,并将其存储在数组[]中 我的CSV文件是(列A1、A2、B1、B2): 我的代码是: #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ string val; string arr[2][2]; ifstream myFile; myF

首先,在我的代码中,我需要从csv文件中读取每一行和每一列,并将其存储在数组[]中

我的CSV文件是(列A1、A2、B1、B2):

我的代码是:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    string val;
    string arr[2][2];

    ifstream myFile;
    myFile.open("excel.csv");
    while (myFile.good())
    {    
        for(int row = 0; row <= 1; row++)
        {
            for(int column = 0; column <= 1; column++)
            { 
                getline(myFile, val, ',');
                arr[row][column] = val;
            }
        }         
    }
    cout <<"cell 0.0: " << arr[0][0] << endl;
    cout <<"cell 0.1: " << arr[0][1] << endl;
    cout <<"cell 1.0: " << arr[1][0] << endl;
    cout <<"cell 1.1: " << arr[1][1] << endl;

}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串val;
字符串arr[2][2];
ifstreammyfile;
myFile.open(“excel.csv”);
while(myFile.good())
{    
对于(int row=0;row这是我的atm代码。
它打印:

    cell 0.0: 46842 
    cell 0.1: test 
    cell 1.0: 46842 
    cell 1.1: test 

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
main(){
字符串arr[2][2];
字符串val;
串我的_str;
ifstreammyfile;
myFile.open(“excel.csv”);

对于(int row=0;row您犯了一些需要纠正的错误。您使用
std::getline
读取到下一个逗号。即使新行开始,它也会这样做。因此它将继续读取,直到找到下一个逗号。这将解释

cell 0.1: test
46825
它显示“test”,然后是新行,然后是46825

因此,这将不起作用。我们需要使用基于行的方法。我们将首先读取完整的行
46842,测试
,然后拆分它。我们将使用
std::getline
拆分它。但现在,它将起作用,因为它将提取数据,直到下一个逗号或行尾

您也不应使用:

while (myFile.good())
请参阅3种可能的解决方案:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string val;
    string arr[2][2];

    ifstream myFile;
    myFile.open("excel.csv");
    int row = 0;
    while (getline(myFile,val))
    {
        std::istringstream iss(val);
        for (int column = 0; column <= 1; column++)
        {
            getline(iss, arr[row][column], ',');
        }
        ++row;
    }
    cout << "cell 0.0: " << arr[0][0] << endl;
    cout << "cell 0.1: " << arr[0][1] << endl;
    cout << "cell 1.0: " << arr[1][0] << endl;
    cout << "cell 1.1: " << arr[1][1] << endl;

}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串val;
字符串arr[2][2];
ifstreammyfile;
myFile.open(“excel.csv”);
int行=0;
while(getline(myFile,val))
{
标准:istringstream iss(val);

for(int column=0;column
getline(myFile,val,,”);
只会在逗号处停止。换行符对它不再重要。您可以调整中使用的技巧来执行您想要的操作。我将代码更改为在getline()中不使用分隔符逗号.因此,现在基本上读取以逗号分隔的整行,如46842,test和46825,test2。现在只需找到一种方法,以逗号分隔该行,并将其存储在数组[][]中。我正在查看您发布的链接,谢谢。要读取此链接,需要任何特定函数,因为我知道我需要在类似数组[row][column]的数组中读取它??谢谢,您可以创建两个嵌套循环。外部循环将读取完整的行(由
'\n'
分隔)。然后您可以使用
std::istringstream
std::string
(使用当前行)来解析此行的单元格(由
'分隔,
)在内部循环中。我尝试使用上面提出的解决方案,它工作得更好,但还不理想。这是我代码的一部分,现在它打印:cell 0.0:46842 cell 0.1:test cell 1.0:46842 cell 1.1:test稍等。我会回答你的问题。不要让问题变得更糟。谢谢Armin!非常感谢你,这太神奇了,所有提出的解决方案好的。我会研究这些代码来了解你是如何做到的。再次感谢:)@FernandoSalviati这确实是一个很好的答案。公平地说,你可以通过检查它是否被接受来支付。()你也可以留下一张不算的追加投票,但如果你自己赢得了一些代表,也许以后会有。“谢谢”-评论实际上是反对建议的(但我从来没有看到过比答案被接受还要多的抱怨)。一个被接受的回答者也可以向提问者支付一张赞成票。(如果这个问题值得回答,这是有道理的。)
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

main() {
   string arr[2][2];
   string val;
   string my_str; 
   ifstream myFile;
   myFile.open("excel.csv");   

   for(int row = 0; row <= 1; row++) {
       getline(myFile, val);
       stringstream s_stream(val);
       for(int column = 0; column <= 1; column++) {                            
           string substr;
           getline(s_stream, substr, ',');
           result.push_back(substr);
           arr[row][column] = result.at(column);
       }
   }
   cout <<"cell 0.0: " << arr[0][0] << endl;
   cout <<"cell 0.1: " << arr[0][1] << endl;
   cout <<"cell 1.0: " << arr[1][0] << endl;
   cout <<"cell 1.1: " << arr[1][1] << endl;
}
cell 0.1: test
46825
while (myFile.good())
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string val;
    string arr[2][2];

    ifstream myFile;
    myFile.open("excel.csv");
    int row = 0;
    while (getline(myFile,val))
    {
        std::istringstream iss(val);
        for (int column = 0; column <= 1; column++)
        {
            getline(iss, arr[row][column], ',');
        }
        ++row;
    }
    cout << "cell 0.0: " << arr[0][0] << endl;
    cout << "cell 0.1: " << arr[0][1] << endl;
    cout << "cell 1.0: " << arr[1][0] << endl;
    cout << "cell 1.1: " << arr[1][1] << endl;

}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>


int main() {

    std::string val;
    std::string arr[2][2];

    std::ifstream myFile;
    myFile.open("excel.csv");

    for (unsigned int row = 0; std::getline(myFile, val); ++row)
    {
        std::istringstream iss(val);
        for (unsigned int column = 0; column <= 1; column++)
        {
            std::getline(iss, arr[row][column], ',');
        }
    }

    std::cout << "cell 0.0: " << arr[0][0] << std::endl;
    std::cout << "cell 0.1: " << arr[0][1] << std::endl;
    std::cout << "cell 1.0: " << arr[1][0] << std::endl;
    std::cout << "cell 1.1: " << arr[1][1] << std::endl;

}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

int main() {

    // Here we will store all data from our CSV file
    std::vector<std::vector<std::string>> data{};

    // Open the csv file with the source data
    std::ifstream myFile{ "excel.csv" };

    // Check, if we could open the source file
    if (myFile) {

        // We will read all lines of the source file in a for loop
        for (std::string line{}; std::getline(myFile, line); ) {

            // Stuff the line in an std::istringstream, so that we can extract data of jkust one line
            std::istringstream lineAsStream{ line };

            // The single parts of one line
            std::vector<std::string> parts{};

            for (std::string partData{}; std::getline(lineAsStream, partData, ','); parts.push_back(partData))
                ; // Empty body

            // Store new row data
            data.push_back(parts);
        }
        std::cout << "cell 0.0: " << data[0][0] << std::endl;
        std::cout << "cell 0.1: " << data[0][1] << std::endl;
        std::cout << "cell 1.0: " << data[1][0] << std::endl;
        std::cout << "cell 1.1: " << data[1][1] << std::endl;
    }
    else {
        std::cerr << "\n***Error: COuld not open source file\n";
    }
    return 0;
}