2017-05-08 12 views
0

現在、sfml .loadfrommemoryメソッドを使用しようとしています。バイト配列としてファイルをテキストファイルに保存する方法は? C++

私の問題は、ファイルへのバイト配列としての仕組みがわかりません。 私は何かをコーディングしようとしましたが、ファイル全体を読み取るわけではありません。 は本当のサイズのファイルではありません。しかし、なぜ私は考えがありません。

using namespace std; 

if (argc != 2) 
    return 1; 

string inFileName(argv[1]); 
string outFileName(inFileName + "Array.txt"); 

ifstream in(inFileName.c_str()); 

if (!in) 
    return 2; 

ofstream out(outFileName.c_str()); 

if (!out) 
    return 3; 

int c(in.get()); 

out << "static Byte const inFileName[] = { \n"; 

int i = 0; 

while (!in.eof()) 
{ 
    i++; 
    out << hex << "0x" << c << ", "; 
    c = in.get(); 

    if (i == 10) { 
     i = 0; 
     out << "\n"; 
    } 
} 

out << " };\n"; 

out << "int t_size = " << in.tellg(); 
+0

は、Windows上で実行していますか?どのくらいのファイルが読み込まれませんか? '\ r '文字が飲み込まれている可能性はありますか? –

+0

@Martin:IIRCでは、EOF文字(26)も、テキストモードではifstreamに影響します。 –

+0

@BenVoigt - 実際、彼はそうではありません。 'c'の定義(配列の先頭を書く前)は' in.get() 'を呼び出します。 –

答えて

0

はそれが働いて得た:

は、ここに私の実際のコードです!

データをベクターに保存するだけで作業できます。

すべてのバイトを取得した後、それをtxtファイルに入れます。

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

int main(int argc, const char* argv[]) { 

if (argc != 2) 
    return 1; 

std::string inFileName(argv[1]); 
std::string outFileName(inFileName + "Array.txt"); 

std::ifstream ifs(inFileName, std::ios::binary); 

std::vector<int> data; 

while (ifs.good()) { 
    data.push_back(ifs.get()); 
} 
ifs.close(); 

std::ofstream ofs(outFileName, std::ios::binary); 

for (auto i : data) { 

    ofs << "0x" << i << ", "; 

} 

ofs.close(); 

return 0; 

}

関連する問題