[英]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.