2016-07-28 3 views
0

私は初心者で、このようなcsvファイルを読み込んで印刷するためのC++プログラムを書く必要があります。マルチマップを使用してC++で2列以上のcsvファイルを読み込んで印刷する

DateTime,value1,value2 
12/07/16 13:00,3.60,50000 
14/07/16 20:00,4.55,3000 

どうすればプログラミングを進めることができますか? 私は日付を単純なマルチマップコードでしか得ることができません。

+0

あなたの現在のコードは何ですか? – jonathanGB

+0

マルチマップはこれのための奇妙な選択のように思えます。 –

+0

@ジョナサン・ポッター私は完全にあなたに同意します – VolAnd

答えて

0

もちろん、this質問を開いてこれを理解していただければ、その作業にはさまざまな解決策があります。

まず第一に、私は次の例を考慮し、最も簡単な方法であなたのタスクを作成しようとすることを提案する:

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

int main() 
{ 
    string str = "12/07/16 13:00,3.60,50000"; 
    stringstream ss(str); 
    vector<string> singleRow; 
    char ch; 
    string s = ""; 
    while (ss >> ch) 
    { 
     s += ch; 
     if (ss.peek() == ',' || ss.peek() == EOF) 
     { 
      ss.ignore(); 
      singleRow.push_back(s); 
      s.clear(); 
     } 
    } 
    for (vector<string>::iterator i = singleRow.begin(); i != singleRow.end(); i++) 
     cout << *i << endl; 
    return 0; 
} 

は、私はそれがあなたのために役立つことができると思います。

1

私はあなたに正確な解決策をほとんど(最後に注意書きを読んでください)しました。

あなたのプログラムは、元のcsvファイル名をコマンドライン引数として受け取るコンソールアプリケーションであると仮定します。

したがって、次のコードを参照し、必要であれば、必要な変更を行います。

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

std::vector<std::string> getLineFromCSV(std::istream& str, std::map<int, int>& widthMap) 
{ 
    std::vector<std::string> result; 
    std::string line; 
    std::getline(str, line); 

    std::stringstream lineStream(line); 
    std::string cell; 

    int cellCnt = 0; 

    while (std::getline(lineStream, cell, ',')) 
    { 
     result.push_back(cell); 
     int width = cell.length(); 
     if (width > widthMap[cellCnt]) 
      widthMap[cellCnt] = width; 
     cellCnt++; 
    } 
    return result; 
} 

int main(int argc, char * argv[]) 
{ 
    std::vector<std::vector<std::string>> result; // table with data 
    std::map<int, int> columnWidths; // map to store maximum length (value) of a string in the column (key) 
    std::ifstream inpfile; 
    // check file name in the argv[1] 
    if (argc > 1) 
    { 
     inpfile.open(argv[1]); 
     if (!inpfile.is_open()) 
     { 
      std::cout << "File " << argv[1] << " cannot be read!" << std::endl; 
      return 1; 
     } 
    } 
    else 
    { 
     std::cout << "Run progran as: " << argv[0] << " input_file.csv" << std::endl; 
     return 2; 
    } 
    // read from file stream line by line 
    while (inpfile.good()) 
    { 
     result.push_back(getLineFromCSV(inpfile, columnWidths)); 
    } 
    // close the file 
    inpfile.close(); 
    // output the results 
    std::cout << "Content of the file:" << std::endl; 
    for (std::vector<std::vector<std::string>>::iterator i = result.begin(); i != result.end(); i++) 
    { 
     int rawLen = i->size(); 
     for (int j = 0; j < rawLen; j++) 
     { 
      std::cout.width(columnWidths[j]); 
      std::cout << (*i)[j] << " | "; 
     } 
     std::cout << std::endl; 
    } 
    return 0; 
} 

注:あなたの仕事は、単にベクトルのベクトルを置き換えることです(resultのために使用されているタイプstd::vector<std::vector<std::string>>)マルチマップへ(あなたの解決策の鍵となるものを理解していただければ幸いです)

+0

こんにちは、そして大きな助けをありがとう!実際には学校のプロジェクトであり、プログラムで地図を使用する必要があります。 –

+0

私はそれを前提としており、これにより、列の幅を格納する単純な 'map'が追加されています。いずれの場合でも、あなたのソリューションに私の例を自由に使用することができます。 – VolAnd

関連する問題