Witam,
czy ktoś mógłby pomóc mi z funkcją wstawiającą "WOW!" na 50 pozycje w pliku?
Ciągle dodaje mi tą informacje na koniec pliku, pomimo że ustawiłem pozycje za pomocą seekp.
Podaję kod:
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
void addToFile (string txt, string fileName) {
fstream save;
save.open(fileName+".txt", fstream::out | fstream::app);
save << txt << endl;
save.close();
}
void putSignToFile (string fileName){
cout << "FUNKCJA PUT SIGN!" << endl;
fstream save;
save.open(fileName+".txt", fstream::out | fstream::app );
int wherePut = 50;
cout << "Where is Position at start? " << save.tellp() << endl;
cout << "Where Put? " << wherePut << endl;
save.seekp(wherePut);
cout << "Where is position after seek? " << save.tellp() << endl;
save << "WOW!";
cout << "Where position right now? " << save.tellp() << endl;
save.close();
}
void showFile (string fileName){
cout << "FUNKCJA SHOW FILE!" << endl;
fstream show;
string load;
show.open(fileName+".txt", fstream::in | fstream::app);
while (!show.eof()) {
show >> load;
cout << load << endl;
}
show.close();
}
int main ()
{
setlocale(LC_ALL, "");
string fileName = "testRandomFile";
string signs;
char sign;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
sign = rand()%11+33;
signs.append(1,sign);
}
addToFile(signs, fileName);
signs = "";
}
putSignToFile(fileName);
showFile (fileName);
return 0;
}