gePuffertes Lesen-/Schreiben Problem
-
Hallo Forum
Folgendes Problem mit dem Code hier. Ich möchte ein File auslesen mit der Puffergrösse von z.B. 4096, dann diesen Puffer in eine Zieldatei schreiben. Dann wieder 4096 Bytes lesen usw. in einer Schleife bis END OF FILE. Nur mein Problem ist, dass er mir immer den ganzen Puffer schreibt, auch wenn das Quell-File nicht genau ein Vielfaches vom Puffer ist.
#include <stdio.h> #include <fstream.h> #include <string.h> #include <iostream.h> #include <time.h> //#include <windows.h> //#include <stat.h> void sleep( clock_t wait) { clock_t goal; goal = wait + clock(); while(goal > clock() ); } int main(void) { //ofstream myFile; //myFile.open ("source.txt", ios::out ¦ ios::in ¦ ios::binary) char buffer [4096]; long lSize; ifstream inFile ("c:\\temp\\source.txt", ios::in | ios::binary); if (!inFile) { cout << "Cannot open file"; return(1); } ofstream outFile ("c:\\temp\\dest.txt", ios::app | ios::binary); if (!outFile) { cout << "Cannot open file"; return(1); } //Grösse ermitteln der Ziel-Datei outFile.seekp(0,ios::end); lSize = outFile.tellp(); while (! inFile.eof()) { inFile.read (buffer, sizeof(buffer)); outFile.write (buffer, sizeof(buffer)); } //outFile << inFile.rdbuf(); inFile.close; outFile.close; }
-
Hallo,
die Methode istream::gcount löst dein Problem.