Попытка подсчета количества похожих элементов в текстовом файле после написания текста, но я получаю No operator << matches these operands operand type are std::ofstream >> std::string
. Код работает, но при добавлении цикла while я получаю сообщение об ошибке textfile >> item
. Имеет ли это какое-либо отношение к потоку текстового файла?no operator соответствует этим операндам ofstream >> string
#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;
}
'ofstream' означает выходной поток файла. Вы не можете читать и писать одновременно. Используйте 'ifstream' для чтения – Stefan
(https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Biffen