Programm, das sich selbst kopiert?
-
Ist doch alles Blödsinn hier.
Das LESEN (was man zum kopieren eben benötigt) wird doch nicht verboten, wenn das Programm läuft
-
Ich verstehe das Problem auch nicht so ganz. Kopieren ist doch kein Problem.
Könnte es sein das du dein Programm zur Laufzeit eigentlich Verschieben willst.
Unter Windows must du hierfür MoveFileEx() mit dem Flag MOVEFILE_DELAY_UNTIL_REBOOT verwenden. Das ist das, was üblicherweise von Setup-Programmen verwendet wird.mfg JJ
-
nein, ich hatte eigentlich schon an kopieren gedacht.
Das Programm ist 'c:\file.exe'
sein code sieht so aus.
[cpp]#include <fstream>
#include <iostream>void copy(std::ifstream& is, std::ofstream& os);
int main()
{
std::ifstream inFile("c:\\file.exe",std::ios::binary);
if(!inFile.is_open())
return 1;std::ofstream outFile("c:\\copy_of_file.exe",std::ios::binary);
if(!outFile.is_open())
return 1;copy(inFile,outFile);
outFile.close();
inFile.close();
return 0;
}void copy(std::ifstream& is, std::ofstream& os)
{
char c;
while(is.get(c))
os.put(c);
}[/code]So klappt's nicht. das ist das problem...
-
nein, ich hatte eigentlich schon an kopieren gedacht.
Das Programm ist 'c:\file.exe'
sein code sieht so aus.#include <fstream> #include <iostream> void copy(std::ifstream& is, std::ofstream& os); int main() { std::ifstream inFile("c:\\file.exe",std::ios::binary); if(!inFile.is_open()) return 1; std::ofstream outFile("c:\\copy_of_file.exe",std::ios::binary); if(!outFile.is_open()) return 1; copy(inFile,outFile); outFile.close(); inFile.close(); return 0; } void copy(std::ifstream& is, std::ofstream& os) { char c; while(is.get(c)) os.put(c); }
So klappt's nicht. das ist das problem...
-
So klappt's nicht. das ist das problem...
hier tut's das. Bauch doch zb. mal Fehlermeldungen ein, um dein Problem etwas mehr zu lokalisieren.
-
das würd ich winapi machen lassen! da gibs so ne schöne kopier funktion!
schau mal da:
// varinate 1: char[] sourcefile="source.txt"; //quelldatei char[] destfile="dest.txt"; //zieldatei char * call=(char*)calloc(11+strlen(sourcefile)+strlen(destfile)+,sizeof(char)); sprintf(call,"copy \"%s\" "\s\"",sourcefile,destfile); system(call); // variante 2: #include <stdio.h> // C Variante!!! #include <string.h> int filecopy(const char* sourcefile, const char* destfile) { FILE* pSource = NULL; // Inputstream fürs Lesen der Quelldatei FILE* pDestination = NULL; // Outputstream fürs Schreiben der Zieldatei char c; // Zeichen als "Byte" mißbrauchen (hehehe) if(!sourcefile || !destfile) return 0; // Irgendein Pfad fehlt... else if(!strcmp(sourcefile, destfile) || sourcefile==destfile) return -1; // Quelle und Ziel identisch... else { pSource = fopen(sourcefile, "rb"); // Quelldatei öffnen if(!pSource) return -2; // Fehler beim Öffnen der Datei pDestination = fopen(destfile, "wb"); // Zieldatei öffnen, ggf. neu anlegen if(!pDestination) return -3; // Fehler beim Öffnen der Datei while((c=getc(pSource)) != EOF) putc(c, pDestination); // Byteweises Kopieren bis Dateiende von pSource fclose(pDestination); // Zieldatei schließen fclose(pSource); // Quelldatei schließen return 1; } } // varinate 3: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { CopyFile("c:\\Datei.txt","c:\\Datei2.txt",FALSE); /* BOOL CopyFile( LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists ); lpExistingFileName [in] Pointer to a null-terminated string that specifies the name of an existing file. In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File. Windows Me/98/95: This string must not exceed MAX_PATH characters. lpNewFileName [in] Pointer to a null-terminated string that specifies the name of the new file. In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File. bFailIfExists [in] If this parameter is TRUE and the new file specified by lpNewFileName already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds. */ } varinate 4: int ch; FILE *ein; FILE *aus; ein = fopen("test1.txt","r"); // Quelldatei öffnen aus = fopen("test2.txt","w"); // Zieldatei ch = fgetc(ein); // Ein Zeichen auslesen... while (!feof(ein)) { fprintf(aus,"%c",ch); // ...und in die Zieldatei schreiben ch = fgetc(ein); }
-
no ne kopier funktion;-)
bool CopyFile (string source, string destination) { CFile dat_in; // Defines two streams, one for the opening file, // the other one for the saving file ofstream dat_out; dat_in.Open(source, ios::in | ios::binary); //Opens the source file dat_out.open(destination.c_str(), ios::out | ios::binary | ios::trunc); // Opens the destination file, or creates it string line_content; //Buffer for lonely lines from the source file int loop = 0; //counts the loops if(!dat_in.File) // if source file doesn't exist, //stop the function { cout << "File couldn't be opend!" << endl; return false; } else //else go on { while(loop < dat_in.length-1) // As long as file end //isn't reached, { line_content = dat_in.Content.substr(loop,1); dat_out << line_content; loop++; } dat_in.File.close(); dat_out.close(); return true; } }
binär kopieren is wohl geeigneter?? sonst liest er nicht alle zeichen oder?
lg
-
char * call=(char*)calloc(11+strlen(sourcefile)+strlen(destfile)+,sizeof(char)); sprintf(call,"copy \"%s\" "\s\"",sourcefile,destfile); system(call);
ist der erste Parameter von sprintf nicht ein FILE *?
if(!sourcefile || !destfile) return 0; // Irgendein Pfad fehlt...
das verwirrt mich ein bischen, weil hier muss man destfile schon erstellt haben, und die wird dann überschrieben. aber was ist wenn sie noch nicht da is, dann machst return 0.
ich würde da nur
if(!sourcefile) return 0; // Irgendein Pfad fehlt...
machen
else if(!strcmp(sourcefile, destfile) || sourcefile==destfile) return -1; // Quelle und Ziel identisch...
sourcefile==destfile
wie soll das funktionieren, man kann 2 strings nicht mit einem == vergleichen.solltest das so umändern:
else if(!strcmp(sourcefile, destfile) || equal(sourcefile,destfile)) return -1; // Quelle und Ziel identisch...
hier die funktion equal:
int equal(char * str1,char *str2) { for(int i=0; str1[i]!='\0';i++) if(str1[i]!=str2[i])return 1; return 0; }
-
@Greenberet: gut gut...
mir gefällt die letzte variante besser
bool CopyFile (string source, string destination)
.
.
.gibs da comments?
oder halt sonst eine copy funktion der winapi entnehmen!cu
-
Wahnsinn... jetzt geht es.
Ich hätte schwören können, dass es vorher nicht ging.
Ich habs jetzt mit dem BCB 6 gemacht... vorher mit MSVC 6.Was wird jetzt genau verboten, wenn die exe läuft... das kopieren anscheinen ja nicht.
-
Des Schreiben, du Döschkopp.
-
lol, schon klar, ich meinte jetzt eher so etwas wie das verschieben einer exe, das kopieren, was ja anscheinend geht, das schreiben( ist klar
)
das lesen... wie sich das bei der laufzeit verhaelt.
-
Anm.: Irgendwie kann man aber auch sogar sich selbst löschen: Siehe WinAPI-FAQ.
MfG SideWinder