简体   繁体   English

错误:&#39;operator&lt;&lt;&#39; 不匹配(操作数类型是 &#39;std::ifstream&#39; {aka &#39;std::basic_ifstream<char> &#39;} 和 &#39;CHAR [39]&#39; {aka &#39;char [39]&#39;})|

[英]error: no match for 'operator<<' (operand types are 'std::ifstream' {aka 'std::basic_ifstream<char>'} and 'CHAR [39]' {aka 'char [39]'})|

i am trying to look if a file name hardware id txt exist, if not then make it and write the hardware id in it, however i keep getting this error that claims that there is no much to an operator.我正在尝试查看文件名硬件 id txt 是否存在,如果不存在,则创建它并在其中写入硬件 id,但是我不断收到此错误,声称对操作员没有太多帮助。

#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    HW_PROFILE_INFO hwProfileInfo; // unless we have a very good reason, this should
                                   // not be global
    if(GetCurrentHwProfile(&hwProfileInfo) != NULL)
    { // update info was a success. NOW we have a GUID and can do stuff with
      // hwProfileInfo
        printf("Hardware GUID: %s\n",     hwProfileInfo.szHwProfileGuid);
        printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
        std::ifstream hwidfile;
        hwidfile.open("hardwareid2.txt");
        if(hwidfile) {
            printf("File exist");
            return 0;
        } else{
            std::ofstream hwidfile { "hardwareid2.txt" };
        }

        if (!(hwidfile << hwProfileInfo.szHwProfileGuid))
        { // will fail if can't write for any reason, like file didn't open
            std::cout << "File write failed\n";
            return -1;
        }


    }
    else
    {
        std::cout << "GetCurrentHwProfile failed\n";
        return -1;
    }

    getchar();
}

You are declaring two separate variables, one std::ifstream and one std::ofstream , with the same name hwidfile .您正在声明两个单独的变量,一个std::ifstream和一个std::ofstream ,名称相同hwidfile That won't work in your case.这在你的情况下不起作用。 You are declaring the ofstream in a nested scope that destroyed the ofstream before you try to write to it.您在尝试写入之前破坏了ofstream的嵌套范围内声明了ofstream You can't change a variable's type by re-declaring the same name with a different type.您不能通过使用不同类型重新声明相同名称来更改变量的类型。

You are actually calling operator<< on the ifstream , not the ofstream , hence the compiler error.您实际上是在ifstream上调用operator<< ,而不是ofstream ,因此编译器错误。 The ofstream is out of scope by that time.到那时, ofstream已经超出了范围。

If you need a file stream that can be used for both input and output, use a single std::fstream variable instead, eg:如果您需要一个可用于输入和输出的文件流,请改用单个std::fstream变量,例如:

std::fstream hwidfile;
hwidfile.open("hardwareid2.txt", std::ios_base::in);
if (hwidfile.is_open()) {
    std::cout << "File exists\n";
    return 0;
}
hwidfile.open("hardwareid2.txt", std::ios_base::out);
if (!hwidfile.is_open()) {
    std::cout << "File create failed\n";
    return 0;
}
if (!(hwidfile << hwProfileInfo.szHwProfileGuid))
{
    std::cout << "File write failed\n";
    return -1;
}

Otherwise, give each file stream variable different names, eg:否则,给每个文件流变量不同的名称,例如:

std::ifstream hwidfileIn("hardwareid2.txt");
if (hwidfileIn.is_open()) {
    std::cout << "File exists\n";
    return 0;
}
std::ofstream hwidfileOut("hardwareid2.txt");
if (!hwidfileOut.is_open()) {
    std::cout << "File create failed\n";
    return 0;
}
if (!(hwidfileOut << hwProfileInfo.szHwProfileGuid))
{
    std::cout << "File write failed\n";
    return -1;
}

Or, simply get rid of the ifstream and use std::filesystem::exists() or equivalent instead, eg:或者,简单地摆脱ifstream并使用std::filesystem::exists()或等价物,例如:

if (std::filesystem::exists("hardwareid2.txt")) {
    std::cout << "File exists\n";
    return 0;
}
std::ofstream hwidfile("hardwareid2.txt");
if (!hwidfileOut.is_open()) {
    std::cout << "File create failed\n";
    return 0;
}
if (!(hwidfile << hwProfileInfo.szHwProfileGuid))
{
    std::cout << "File write failed\n";
    return -1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法转换 'std::ifstream {aka std::basic_ifstream<char> }' to 'char**' for getline() function</char> - error: cannot convert ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘char**’ for getline() function 无法转换 'std::ifstream' {aka 'std::basic_ifstream<char> '} 到 'bool' 作为回报</char> - Cannot convert 'std::ifstream' {aka 'std::basic_ifstream<char>'} to 'bool' in return 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:与“ operator &gt;&gt;”不匹配(操作数类型为“ std :: istream {aka std :: basic_istream <char> }和&#39;std :: vector <double> &#39;) - Error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'std::vector<double>') 错误:'operator&gt;&gt;' 不匹配(操作数类型是 'std::istream' {aka 'std::basic_istream<char> '} 和 'const int')|</char> - error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'const int')| 错误:“operator>>”不匹配(操作数类型为“std::istream”{又名“std::basic_istream”<char> '} 和 '歌剧')</char> - error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Oper') 错误:“ operator ==”不匹配(操作数类型为“ Seat”和“ std :: string {aka std :: basic_string <char> }&#39;) - error: no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}') 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> - error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List') 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM