C++ C++;|用那些文件再看一遍

C++ C++;|用那些文件再看一遍,c++,file,writing,C++,File,Writing,我又来了,伙计们,沉浸在复杂的文件工作中。这是我的节目: {老掉牙不发牢骚} #include <iostream> #include <string> #include <cstdlib> #include <windows.h> #include <stdexcept> #include <limits> #include <Lmcons.h> #include <fstream> #includ

我又来了,伙计们,沉浸在复杂的文件工作中。这是我的节目:

{老掉牙不发牢骚}

#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
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
作废(字符串x){x+=“\n”;cout
在此处创建新文件。然后,稍后:

newfile.open(path2.c_str(),ios::out|ios::trunc);
在这里,第二次尝试创建相同的文件。该文件已创建,并且已打开。尝试打开已打开的相同
std::ofstream
对象是错误的,
std::ofstream
对象现在处于错误状态

对象处于错误状态时,所有后续写入流对象的尝试都将失败


解决方案:创建文件一次,而不是两次。

以下是为感兴趣的人更正的代码:

#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="proxy.http.port=3128";
    string line78="proxy.https.host=proxy-eple.in.ac-nantes.fr";
    string line79="proxy.https.port=3128";
    string line80="proxy.socks.host=proxy-eple.in.ac-nantes.fr";
    string line81="proxy.socks.port=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;

            //because we still need it to roll through the file before copying
            for(unsigned int i=76; i<82; i++)
            {
                std::string text;
                std::getline(file,text);
            }

            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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
作废(字符串x)
{
x+=“\n”;

coutPlease正确缩进显示的代码。随机的、特殊的缩进使此代码完全不可解析。欢迎使用堆栈溢出。学习将编码问题简化为一个问题非常重要。首先,它帮助您找到问题;其次,它减少了我们在帮助您之前必须完成的工作量;第三,它增加了在您的问题中找到问题原因的可能性。如果问题是您的代码没有将文本写入新文件,请编写一个程序,尝试将文本(例如“hello”)写入新文件,而不执行其他操作。您是否尝试过调试?这确实有助于使用
SHGetKnownFolderPath
查找
“AppData”
folder。或者使用
SHGetSpecialFolderPath
如果它应该与WinXP兼容。现在“无眼镜”先生满意了吗?他在“无法打开文件\\new.txt\\\”上有一个错误。它不会停止程序执行,但这是因为
处于打开状态()
仍然成功。文件已打开。但流对象处于错误状态。我不明白,我不是用ofstream创建它并用open打开它吗?创建时它已经打开了吗?我知道正在进行一些调试,我替换了:
ofstream newfile(path2.c_str());
使用:
流新文件;
并对我留下的一些错误进行了更正。现在它可以正常工作了!编辑:版主现在可以关闭它:)
newfile.open(path2.c_str(),ios::out|ios::trunc);
#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="proxy.http.port=3128";
    string line78="proxy.https.host=proxy-eple.in.ac-nantes.fr";
    string line79="proxy.https.port=3128";
    string line80="proxy.socks.host=proxy-eple.in.ac-nantes.fr";
    string line81="proxy.socks.port=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;

            //because we still need it to roll through the file before copying
            for(unsigned int i=76; i<82; i++)
            {
                std::string text;
                std::getline(file,text);
            }

            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;
}