2010-12-21 27 views
5

ファイルの途中で問題が発生していますfseek次は、長さがのBytesで置き換える長さがmのいくつかのバイトが存在します。単純なwriteはまだm-nバイトを保持します。 m > nm < nの場合、私が変更したくないいくつかのバイト(n-m)が上書きされます。ファイルの途中で上書きする

私は、既知のstartPos to endPosバイトストリームを可変長バイトで置き換えたいだけです。最高の解決策は何ですか?

- EDIT - バックアップを取ることで可能ですが、直接的な解決策はありますか? これはあまりにも厄介ですか?不十分なコーディングの種類。元

+3

ファイルを書き換えます。 –

+0

@ user256007、私はあなたの句読点やいくつかの大文字の大文字は私に困惑していると思います。 –

+0

http://blogs.msdn.com/b/oldnewthing/archive/2010/12/01/10097859.aspx – MSalters

答えて

5

最も堅牢なソリューションになりました

o = fopen(original, 'r') 
b = fopen(backup, 'w') 
while(fpos(o) <= startPos){ 
    buffer += fgetc(o) 
} 
fwrite(b, buffer) 
fwrite(b, replaceMentBytes) 
buffer = "" 
fseek(o, endPos) 
while(!feof(o)){ 
    buffer += fgetc(o) 
} 
fwrite(b, buffer) 

//コピーのバックアップは最初から、ファイル全体を再作成することです。ほとんどのオペレーティングシステムでは、バイトを上書きするだけで、ファイルから挿入または削除することはできません。そのためには、基本的にファイルをコピーして、コピー中にターゲットバイトを置き換える必要があります。 fstreamのライブラリを使用して

+0

を参照してください。しかし、その方法は、貧乏人の貧しい人々がコーディングするように見えます。 –

+0

@ user256007 @Thanatosは言ったように、あなたは本当にバイトを上書きまたは追加することしかできません。挿入する必要がある場合は、通常、新しいファイルを作成し、それに書き込んだ後、元のファイルに移動する必要があります。 –

0

、ここではこのような場合には読み(あなたはreadFileの機能で、または単にchar型の配列内のすべてのあなたのシークを行うことができます他の人が

/** 
* Overwrite a file while replacing certain positions 
*/ 

#include <iostream> 
#include <fstream> 

using namespace std; 

int readFile(char* filename,int& len,char*& result) 
{ 
    ifstream in(filename); // Open the file 
    if(!in.is_open()) 
     return 1; 

    // Get file length 
    in.seekg(0,ios::end); 
    len = (int)in.tellg(); 
    in.seekg(0,ios::beg); 

    // Initialize result 
    result = new char[len+1]; 

    // Read the file and return its value 
    in.read(result,len); 

    // Close the file 
    in.close(); 

    return 0; 
} 

void writeFile(char* filename,char* data,int from,int to,int origdata,int trunc) 
{ 
    ofstream out; 
    (trunc == 1) ? out.open(filename,ios::trunc) : out.open(filename,ios::app); // Simple ternary statement to figure out what we need to do 

    // Find position if we're not starting from the beginning 
    if(trunc == 1) 
     out.seekp(from); 
    else // Otherwise send us to the beginning 
     out.seekp(0,ios::beg); 

    if(origdata == 1) // If we need to start in the middle of the data, let's do so 
     for(int i=0;i<(to-from);++i) 
      data[i] = data[from+i]; // Reverse copy 

    out.write(data,(to-from)); 

    out.close(); 
} 

int main() 
{ 
    char* read; 
    int len = 0; 
    if(readFile("something.txt",len,read) != 0) 
    { 
     cout<< "An error occurred!" << endl; 
     return 0; 
    } 

    // Example to make this work 
    cout<< "Writing new file...\r\n"; 
    writeFile("something.txt",read,0,20,1,1); // Initial write 
    writeFile("something.txt","\r\nsome other mumbo jumbo",21,45,0,0); 
    writeFile("something.txt",read,46,100,1,0); // Replace the rest of the file back 

    cout<< "Done!\r\n"; 
    cin.get(); // Pause 
    delete [] read; 
    return 0; 
} 

を言っているかもしれないものの単純な実装であります)。そこから、位置を格納し、writeFile()関数を適切に使用することができます。

幸運を祈る!
デニスM.

関連する問題