Sure, here's an example C++ program that reads a text file line by line and counts the…
- User
Sure, here's an example C++ program that reads a text file line by line and counts the number of occurrences of a specific word in the file:
#include
#include
#include
using namespace std;
int main()
{
// Specify the name of the text file you want to read string filename = "example.txt"; // Open the file in binary mode for reading ifstream file(filename, ios::binary); // Set the string to search for string search_string = "example"; // Initialize a counter to keep track of the number of occurrences of the search string int counter = 0; // Start reading the file line by line string line; while (getline(file, line)) { // Check if the line contains the search string if (line.find(search_string)!= string::npos) { // Increment the counter if the line contains the search string counter++; } } // Close the file file.close(); // Print the number of occurrences of the search string cout << "Number of occurrences of search string: " << counter << endl; return 0;}
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.