Hier bin ich wieder Jungs, verloren in der Komplexität der Datei arbeiten. Also hier ist mein Programm:C++ | Zurück mit den Dateien wieder
{alt für no-whiny}
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <stdexcept>
#include <limits>
#include <Lmcons.h>
#include <fstream>
#include <stdio.h>
using namespace std;
void out(string x){x+="\n";cout<<x;}
void outn(){out("");}
void delay(int x){Sleep(x);}
void delayS(int x){Sleep(x*1000);}
void cs(){std::system("cls");}
void UserName(string *x){char username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName(username, &size);
string transition(username);*x=transition;}
//use this syntax in main : string username;UserName(&username);
//main in case you ain't noticed
int main()
{
//getting username
string username;
UserName(&username);
/*out(username);*/
//init
const string namefile="preferences.txt";
string path;
const string beginning="C:\\Users\\";
const string ending="\\AppData\\Roaming\\Processing\\";
//path init
path+=beginning;
path+=username;
path+=ending;
string path2;
path2=path;
path2+="new.txt";
string filename2="new.txt";
path+=namefile;
//new lines init
string line76="proxy.http.host=proxy-eple.in.ac-nantes.fr";
string line77="3128";
string line78="proxy.https.host=proxy-eple.in.ac-nantes.fr";
string line79="3128";
string line80="proxy.socks.host=proxy-eple.in.ac-nantes.fr";
string line81="3128";
//files init
fstream file;
//fstream newfile;
ofstream newfile(path2.c_str());
//main try-catch process
try{
file.open(path.c_str() ,ios::in);
if(!file.is_open()){throw 404;}
//procédure de modif
else{
file.close();
//file manipulation
file.open(path.c_str());
try{newfile.open(path2.c_str(),ios::out|ios::trunc);
if(!newfile.is_open()){throw 404;}}
catch(int x){cout<<"Error "<<x<<" : failed to open file \\new.txt\\";}
for (unsigned int i = 1; i < 76; i++)
{
std::string text;
getline(file, text);
newfile << text <<endl;
}
newfile << line76 <<endl;
newfile << line77 <<endl;
newfile << line78 <<endl;
newfile << line79 <<endl;
newfile << line80 <<endl;
newfile << line81 <<endl;
for (unsigned int i = 82; i < 97; i++)
{
std::string text;
std::getline(file, text);
newfile << text <<endl;
}
newfile << "";
}
//end of file manipulation
file.close();newfile.close();
string old1 = beginning+username+ending+"old_pref.txt";
//renaming process
//rename preferences.txt to old_pref.txt
try{int rf=rename(path.c_str(),old1.c_str());
if(rf!=0){throw 911;}}
//1st catch in case you ain't noticed
catch(int x){cout<<"Error "<<x<<" : Failed to rename to \\old_pref.txt\\";}
//end of 1st catch
//rename new.txt to preferences.txt
try{int rf2=rename(path2.c_str(),path.c_str());
if(rf2!=0){throw 911;}}
//2nd catch in case you ain't noticed
catch(int x){
cout<<"Error "<<x<<" : Failed to rename to \\preferences.txt\\";}
//end of 2nd catch
//ending process
cs();out("done !");}//end of main try
//main catch
catch(int x){
cout<<"Error "<<x<<" : failed to open file \\preferences.txt\\";}
//end of main catch
return 0;
} //end of main
{/ alt ohne whiny}
{für whiny (PS: Ja, ich interessiere mich nicht über Bibliotheken wie Sie kümmern sich nicht um Respekt) (wie kommen: unleserlich, nicht-parsable wie ... Brille bekommen und Chill-out)}
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <stdexcept>
#include <limits>
#include <Lmcons.h>
#include <fstream>
#include <stdio.h>
using namespace std;
void out(string x)
{
x+="\n";
cout<<x;
}
void outn()
{
out("");
}
void delay(int x)
{
Sleep(x);
}
void delayS(int x)
{
Sleep(x*1000);
}
void cs()
{
std::system("cls");
}
void UserName(string *x)
{
char username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName(username, &size);
string transition(username);
*x=transition;
}
//use this syntax in main : string username;UserName(&username);
int main()
{
string username;
UserName(&username);
/*out(username);*/
const string namefile="preferences.txt";
string path;
const string beginning="C:\\Users\\";
const string ending="\\AppData\\Roaming\\Processing\\";
path+=beginning;
path+=username;
path+=ending;
string path2;
path2=path;
path2+="new.txt";
string filename2="new.txt";
path+=namefile;
string line76="proxy.http.host=proxy-eple.in.ac-nantes.fr";
string line77="3128";
string line78="proxy.https.host=proxy-eple.in.ac-nantes.fr";
string line79="3128";
string line80="proxy.socks.host=proxy-eple.in.ac-nantes.fr";
string line81="3128";
fstream file;
//fstream newfile;
ofstream newfile(path2.c_str());
try
{
file.open(path.c_str(),ios::in);
if(!file.is_open())
{
throw 404;
}
//procédure de modif
else
{
file.close();
file.open(path.c_str());
try
{
newfile.open(path2.c_str(),ios::out|ios::trunc);
if(!newfile.is_open())
{
throw 404;
}
}
catch(int x)
{
cout<<"Error "<<x<<" : failed to open file \\new.txt\\";
}
for (unsigned int i = 1; i < 76; i++)
{
std::string text;
getline(file, text);
newfile << text <<endl;
}
newfile << line76 <<endl;
newfile << line77 <<endl;
newfile << line78 <<endl;
newfile << line79 <<endl;
newfile << line80 <<endl;
newfile << line81 <<endl;
for (unsigned int i = 82; i < 97; i++)
{
std::string text;
std::getline(file, text);
newfile << text <<endl;
}
newfile << "";
}
file.close();
newfile.close();
string old1 = beginning+username+ending+"old_pref.txt";
try
{
int rf=rename(path.c_str(),old1.c_str());
if(rf!=0)
{
throw 911;
}
}
catch(int x)
{
cout<<"Error "<<x<<" : Failed to rename to \\old_pref.txt\\";
}
try
{
int rf2=rename(path2.c_str(),path.c_str());
if(rf2!=0)
{
throw 911;
}
}
catch(int x)
{
cout<<"Error "<<x<<" : Failed to rename to \\preferences.txt\\";
}
cs();
out("done !");
}
catch(int x)
{
cout<<"Error "<<x<<" : failed to open file \\preferences.txt\\";
}
return 0;
}
{/ for whiny (PS: denken Sie, ich Vertiefung noch Pflege ?)}
In Ordnung, hier ist mein Problem: Es öffnet die Datei, es erstellt die neue.txt und öffnet es, es ist nicht füllen Sie die new.txt und dann zum Umbenennen Prozess.
Warum schreibt es nicht auf die neue Datei? Wie soll ich das nochmal machen, damit es auf die neue Datei schreibt?
EDIT: Ich benutze Codeblöcke 16,01
einrücken Bitte geben Sie die richtig angezeigten Code. Die zufällige Ad-hoc-Einrückung macht diesen Code völlig unangreifbar. –
Willkommen bei Stack Overflow. Es ist äußerst wichtig, dass Sie lernen, ein Codierungsproblem auf ein [minimales vollständiges Beispiel] zu reduzieren (http://stackoverflow.com/help/mcve). Erstens hilft es Ihnen, das Problem zu finden. Zweitens reduziert es die Menge an Arbeit, die wir tun müssen, bevor wir Ihnen helfen können. Drittens erhöht es die Wahrscheinlichkeit, dass die Ursache des Problems in Ihrer Frage gefunden werden kann. Wenn das Problem darin besteht, dass Ihr Code keinen Text in die neue Datei schreibt, ** schreiben Sie ein Programm, das versucht, Text (z. B. "Hallo") in eine neue Datei zu schreiben, und sonst nichts. ** – Beta
Haben Sie versucht, zu debuggen? Es hilft wirklich wirklich – buld0zzr