C++ 是否可以使用wfstream和fstream打开同一个文件

C++ 是否可以使用wfstream和fstream打开同一个文件,c++,file,C++,File,实际上,我有一个要求,我需要在代码的一部分使用wfstream文件实例打开同一个文件,在代码的另一部分使用fstream实例打开它。我需要访问一个文件,其中用户名类型为std::wstring,密码类型为std::string。如何在代码的同一部分中获取两个变量的值? 正如您在下面看到的,我需要从文件中获取用户名和密码的值,并将其分配给变量。 无法进行类型转换。请不要给出那个解决方案。 ……file.txt 用户名amritha 密码rajeevan 代码编写如下: #include <i

实际上,我有一个要求,我需要在代码的一部分使用wfstream文件实例打开同一个文件,在代码的另一部分使用fstream实例打开它。我需要访问一个文件,其中用户名类型为std::wstring,密码类型为std::string。如何在代码的同一部分中获取两个变量的值? 正如您在下面看到的,我需要从文件中获取用户名和密码的值,并将其分配给变量。 无法进行类型转换。请不要给出那个解决方案。 ……file.txt

用户名amritha 密码rajeevan

代码编写如下:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


int main()
{


std::string y;
unsigned int l;
std::wstring username;
std::wstring x=L"username";
std::wstring q;
std::string password;
std::string a="password";



std::cout<<"enter the username:";
std::wcin>>username;
std::cout<<"enter the password:";
std::cin>>password;
std::wfstream fpp("/home/aricent/Documents/testing.txt",std::ios::in | std::ios::out );

std::getline(fpp,q);
if (q.find(x, 0) !=  std::string::npos) {

        std::wstring z=q.substr(q.find(L"-") + 1) ;
        std::wcout<<"the username is:"<<z; 
        fpp.seekg( 0, std::ios::beg ); 
            fpp<<q.replace(x.length()+1, z.length(), username); 



}

    fpp.close();
    std::fstream fp("/home/aricent/Documents/testing.txt",std::ios::in | std::ios::out );
    std::getline(fp,y);

if (y.find(a, 0) !=  std::string::npos)
 {

        unsigned int len=x.length()+1;
        unsigned int leng=username.length();
        l=len+leng;
        fp.seekg(l+1); 
        std::string b=y.substr(y.find("-") + 1) ;            
        fp<<y.replace(a.length()+1, b.length(), password);     

    }
    fp.close();
}
#包括
#包括
#包括
#包括
int main()
{
std::字符串y;
无符号整数l;
std::wstring用户名;
std::wstring x=L“用户名”;
std::wstring q;
std::字符串密码;
std::string a=“密码”;
std::coutusername;
std::coutpassword;
std::wfstream fpp(“/home/aricent/Documents/testing.txt”,std::ios::in | std::ios::out);
标准::getline(fpp,q);
如果(q.find(x,0)!=std::string::npos){
std::wstring z=q.substr(q.find(L“-”)加1);
std::wcout是指同时向同一个文件打开多个流。另一方面,如果不写入文件,而只读取(因此,将使用
ifstream
wifstream
),则可能是安全的

或者,您只需打开
wfstream
,读取用户名,关闭流,打开
fstream
并读取密码

如果您可以选择,请完全避免混合编码文件。

这是为了同时打开同一文件的多个流。另一方面,如果您不写入文件,而只读取(因此,将使用
ifstream
wifstream
),这可能是安全的

或者,您只需打开
wfstream
,读取用户名,关闭流,打开
fstream
并读取密码


如果您可以选择,请完全避免混合编码文件。

您不应尝试使用两个描述符打开同一文件。即使它工作(例如只读模式),两个描述符也不会同步,因此您将在一个描述符上读取第一个字符,在第二个描述符上读取下一个相同的字符

因此,恕我直言,您应该坚持使用单一解决方案。我的建议是使用字符流处理文件,并在需要时使用
codevt
将窄
字符串转换为宽
wstring

示例转换函数可以是(ref:):


您不应该尝试用两个描述符打开同一个文件。即使它工作(例如只读模式),两个描述符也不会同步,因此您将在一个描述符上读取第一个字符,在第二个描述符上读取下一个相同的字符

因此,恕我直言,您应该坚持使用单一解决方案。我的建议是使用字符流处理文件,并在需要时使用
codevt
将窄
字符串转换为宽
wstring

示例转换函数可以是(ref:):


我尝试过做上述操作,但无法做到。此外,我需要根据用户输入替换文件中的密码值。这是可能的。请您编写一个小代码来执行同样的操作。然后使用我的建议,不要同时打开流。如果您遇到问题,请向我们展示您尝试了什么,我们可以告诉您什么没错。@user3803112在你的代码中,你从来没有同时打开过两个流,所以这是正确的。我尝试了上面的操作,但是做不到。此外,我需要根据用户的输入替换文件中的密码值。这样做是可能的。你能写一个小代码来做同样的操作吗?然后根据我的建议不要打开streams。如果您有问题,请告诉我们您尝试了什么,我们可以告诉您出了什么问题。@user3803112在您的代码中,您从来没有同时打开过两个流,所以这是正确的。将宽字符数据读取为字节并将其转换为宽字符可能更容易,其中包含在标准库中H C++标准是否支持你的库,在哪些字符编码中你需要转换?一个有效的响应可能是:编译器和标准库的宽字符编码的任意默认系统位置。我将尝试写一个基于此的答案。对标准库中包含的宽字符进行限制。您的库遵循哪个C++标准,哪些字符编码需要转换?有效的响应可能是:编译器和标准库的宽字符编码的任意默认系统区域。你能在我的程序中使用它并演示给我看吗?我仍然不清楚。你能在我的程序中使用它并演示给我看吗?我仍然不清楚。
std::wstring wconv(const std::string& str, const std::locale mylocale) {
    // define a codecvt facet for the locale
    typedef std::codecvt<wchar_t,char,std::mbstate_t> facet_type;
    const facet_type& myfacet = std::use_facet<facet_type>(mylocale);

    // define a mbstate to use in codecvt::in
    std::mbstate_t mystate = std::mbstate_t();

    size_t l = str.length();

    const char * ix = str.data(), *next; // narrow character pointers
    wchar_t *wc, *wnext;   // wide character pointers

    // use a wide char array of same length than the narrow char array to convert
    wc = new wchar_t[str.length() + 1];

    // conversion call
    facet_type::result result =  myfacet.in(mystate, ix, ix + l,
        next, wc, wc + l, wnext);
    // should test for error conditions

    *wnext = 0; // ensure the wide char array is properly null terminated

    std::wstring wstr(wc); // store it in a wstring
    delete[] wc;           // destroy the char array
    return wstr;
}
...
#include <locale>
...
std::cin>>password;
std::locale mylocale;
std::fstream fp("/home/aricent/Documents/testing.txt",std::ios::in | std::ios::out );

std::getline(fp,y);
q = wconv(y, mylocale);
...
    fp<<nconv(q.replace(x.length()+1, z.length(), username));
}
std::getline(fp, y);
...