使用C ++计算CSV文件中的行数和列数

时间:2016-07-31 19:55:41

标签: c++ csv

这是我正在使用的代码,这是第一部分,我想获取/计算文件中的行数和列数,程序成功计算了行数....但是列数是错的,当它假设为26时它表示0;有人可以帮我确定问题吗?

// variable declarations
string LineA;
int x;
int col = 0;
int row = 0;
int arrayA[10][10] = { {0} };

//open files
fs_chola.open("CHOLAWCODE.xlsx"); //i'll convert this file to .csv later
if (fs_chola.is_open()){
    cout << "chola file successfully opened\n";
}
if (fs_chola.fail()){
    cerr << "file(chola) not found!"<< endl;
}
fs_master_menu.open("master_menu.csv");
if (fs_master_menu.is_open()){
    cout << "master menu file successfully opened\n";
}
if (fs_master_menu.fail()){
    cerr << "file(master menu) not found! " << endl;
}

// read file
while (fs_master_menu.good()){

    while (getline(fs_master_menu, LineA)){
        istringstream streamA(LineA);

        while (streamA >>x){
            arrayA[row][col] = x;
            col++;
        }
        row++;
    }
}
cout << "done \n";

// show colums and rows (checking to make sure everything is right)
cout << "number of rows: " << row << endl;
cout << "number of columns: " << col << endl;

system("PAUSE");
return 0;
}

0 个答案:

没有答案