2017-03-17 8 views
0

csv(UNICODE)を読み込み、データを2次元配列に格納する必要があります。処理するのはUNICODEでエンコードされていて、文字列を使用すると何も読み取れません。CSVファイル(UNICODE符号化)の読み込み中にエラーが発生しました。C++

#include <algorithm> 
#include <fstream> 
#include <iostream> 
#include <iterator> 
#include <sstream> 
#include <string> 
#include <vector> 
#include <windows.h> 
#include <stdio.h> 
#include <time.h> 

using namespace std; 

int main() 
{ 
    vector<vector<string> > values; 
    vector<string> valueline; 



    ifstream fin("C:\\Users\\Administrator\\Desktop\\test.csv"); 
    string item; 
    for (string line; getline(fin, line);) 
    { 
     istringstream in(line); 

     while (getline(in, item, ';')) 
     { 
      valueline.push_back(item.c_str()); 
     } 

     values.push_back(valueline); 
     valueline.clear(); 
    } 

    fin.close(); 

    int i = 0; 

    for (i = 0; i < values.size(); i++) { 
     for (int j = 0; j < values[i].size(); j++){ 
      printf("%s;",values[i][j]); 

} 
     printf("\n"); 
    } 

} 

答えて

0

ファイルが本当に(UTF-16など)はUnicodeであり、2つのバイトでエンコードされた文字を持っているあなたは確かであれば、 "W" のストリームと文字列(std::wifstreamstd::wstring)を使用します。

ファイルがUTF-8またはその他のエンコーディングである可能性がある場合は、まず変換を実行する必要があります(通常のバイトストリームとして最初に読み込み、次にwstringに変換してください)。あなたがUnicode文字にあなたのUTF-8バイトを変換するstd::wstring_convertとそのメンバ関数from_bytesを使用することができます(ユニコードウィンドウの文字列に変換することができwstringに、)ワイド文字列にUTF-8から変換するための

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert; 
std::wstring myunicodestr = convert.from_bytes(myutf8str); 

他の特定のエンコーディングに対処する必要がある場合は、iconvのような特定のライブラリを使用する必要があります。

+0

私はあなたの答えの前にそれを試してみて、同じことに感謝しました:D –

関連する問題