gpt4 book ai didi

c++ - 没有运算符匹配这些操作数 ofstream >> string

转载 作者:行者123 更新时间:2023-11-30 01:41:48 29 4
gpt4 key购买 nike

在写入文本后尝试计算文本文件中相似项目的数量,但我得到一个 No operator << matches these operands operand type are std::ofstream >> std::string .代码有效,但是当添加 while 循环时我得到错误 textfile >> item .与文本文件的ofstream有关系吗?

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include<string>
using namespace std;

int main()
{

string accord[6];

ofstream textfile;
textfile.open("C:\\temp\\1.txt");

cout << "Enter a 6 cylinder car : " << endl;

for (int x = 0; x < 6; x++) {
getline(cin, accord[x]);
}
for (int x = 0; x < 6; x++) {

textfile << accord[x] << endl;
}
int count = 0;
string item;
while (!textfile.eof()) {
textfile >> item;
if (item == "6") {
count++;
}
}

cout << count << "found!" << endl;



textfile.close();
return 0;
}

最佳答案

可能是因为,正如错误提示的那样,类 std::ofstream 没有那个运算符。

Ofstream: Output stream class to operate on files.

正如您可以自己想象的那样,输出流是一个设计用于写入(输出)的对象。所以你不允许输入操作。

std::fstream 是文件的输出和输入流。它支持operator<< operator>> .

关于c++ - 没有运算符匹配这些操作数 ofstream >> string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820277/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com