Hey FI-Community!
Zuerst:
"Hello World"
Das wird mein erster Eintag hier.
Und zwar..:
Hab ein kleines Problem mit meinem C++ -Code und hoffe ihr könnt mir helfen.
Und zwar soll ich in meinem Praktikum als Übung (bin noch n "Frischling"^^) eine Datei binär teilen und wieder zusammenfügen.
Hier erstmal mein bisheriger Code:
BINÄR TEILEN:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int main()
{
system("clear");
// deklarieren
// ...
string filePath;
string fileName;
string fileName1;
string fileName2;
char *content1;
char *content2;
long len;
long pos;
long pos2;
int cut;
int i;
int x;
bool restart;
cout << "#########<<<<#########>>>>######### " << endl;
cout << "#Dateien#<<<<##binär##>>>>#spliten# " << endl;
cout << "#########<<<<#########>>>>######### " << endl;
do
{
do
{
// eingabe Pfad
// bestimmung der Dateinamen
// ...
cout << "\nPfad zu der zu splitenden Datei: " << endl;
cin >> filePath;
cut = filePath.rfind("/");
fileName.insert(0, filePath.substr(cut));
fileName1 = filePath.substr(0, cut) + fileName + "-1";
fileName2 = filePath.substr(0, cut) + fileName + "-2";
// Abfrage ob Pfad ok
// ...
if(filePath[0] != (char)47)
{
cerr << "Falsche Eingabe!" << endl;
restart = true;
}
else(filePath[0] == (char)47);
{
restart = false;
}
}
while(restart == true);
cout << fileName1 << endl;
cout << fileName2 << endl;
// Dateien öffnen
// ...
ifstream fileIn(filePath.c_str(), ios_base::binary);
ofstream fileOut1(fileName1.c_str()ios_base::binary);
ofstream fileOut2(fileName2.c_str()ios_base::binary);
// Dateilänge bestimmen
// ...
fileIn.seekg(0, ios_base::end);
len = fileIn.tellg();
pos = len / 2;
pos2 = len - pos;
cout << pos << " pos" << endl;
cout << len << " len" << endl;
// Speicher erstellen
// ...
content1 = new char[len];
content2 = new char[len];
// Daten in 2 Schritten kopieren
// ...
fileIn.seekg(0, ios_base::beg);
fileIn.read(content1, 1);
fileIn.seekg(pos, ios_base::beg);
fileIn.read(content2, 1);
// Daten in neue Dateien schreiben
// ...
fileOut1.write(content1, pos);
fileOut2.write(content2, pos2);
// Dateien schließen
// ...
fileOut2.close();
fileOut1.close();
fileIn.close();
// Speicher freigeben
// ...
delete[] content1;
delete[] content2;
cout << "Again? Type '1'" << endl;
cin >> x;
}
while(x == 1);
// Programmende
// ...
for(i=0; i<3; i++)
{
if(i == 2)
{
cout << "thank you for using!" << endl;
cout << "END" << endl;
sleep(2);
break;
}
cout << "......" << endl;
sleep(1);
}
system("clear");
return 0;
}
BINÄR ZUSAMMENFÜGEN:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int main()
{
system("clear");
// deklarieren
// ...
string filePath1;
string filePath2;
string fileNew;
char *content1;
char *content2;
long length1;
long length2;
int cut1;
int cut2;
int i;
int x;
bool restart;
cout << "#########\t+\t#########\t=\t############### " << endl;
cout << "#Dateien#\t+\t##binär##\t=\t#zusammenfügen# " << endl;
cout << "#########\t+\t#########\t=\t############### " << endl;
do
{
do
{
// eingabe Pfad
// bestimmung der Dateinamen
// ...
cout << "\nPfad zu der ersten Datei: " << endl;
cin >> filePath1;
cout << "\nPfad zu der zweiten Datei: " << endl;
cin >> filePath2;
cut1 = filePath1.rfind("/");
cut2 = filePath2.rfind("/");
fileNew = filePath1.substr(0, cut1) + "/FILE";
cout << fileNew << endl;
cout << filePath1 << endl;
cout << filePath2 << endl;
// Abfrage ob Pfad ok
// ...
if(filePath1[0] != (char)47)
{
cerr << "Falsche Eingabe!" << endl;
restart = true;
}
else(filePath1[0] == (char)47);
{
restart = false;
}
if(filePath2[0] != (char)47)
{
cerr << "Falsche Eingabe!" << endl;
restart = true;
}
else(filePath2[0] == (char)47);
{
restart = false;
}
}
while(restart == true);
// Dateien öffnen
// ...
ofstream fileOut(fileNew.c_str());
ifstream fileIn1(filePath1.c_str(), ios_base::binary);
ifstream fileIn2(filePath2.c_str(), ios_base::binary);
// Dateilänge bestimmen
// ...
fileIn1.seekg(0, ios_base::end);
fileIn2.seekg(0, ios_base::end);
length1 = fileIn1.tellg();
length2 = fileIn2.tellg();
// Speicher erstellen
// ...
content1 = new char[length1];
content2 = new char[length2];
// Daten in 2 Schritten kopieren
// ...
fileIn1.seekg(0, ios_base::beg);
fileIn2.seekg(0, ios_base::beg);
fileIn1.read(content1, 1);
fileIn2.read(content2, 1);
// Daten in neue Dateien schreiben
// ...
fileOut.write(content1, length1);
fileOut.write(content2, length2);
// Dateien schließen
// ...
fileIn2.close();
fileIn1.close();
fileOut.close();
// Speicher freigeben
// ...
delete[] content1;
delete[] content2;
cout << "Again? Type '1'" << endl;
cin >> x;
}
while(x == 1);
// Programmende
// ...
for(i=0; i<3; i++)
{
if(i == 2)
{
cout << "thank you for using!" << endl;
cout << "END" << endl;
sleep(2);
break;
}
cout << "......" << endl;
sleep(1);
}
system("clear");
return 0;
}
Das mit dem Teilen klappt auch schon ganz gut nur wenn ich meine Datei wieder zusammenfügen will bekommt die Datei in die ich die Daten schreibe den typ "Unbekannt" und lässt sich nicht mehr öffnen.
Ich hoffe ihr könnt mir helfen.
LG Chris