Rayan Alkhelaiwi Rayan Alkhelaiwi - 5 days ago 5
C++ Question

How to convert this segment of Java code to C++?

I have a difficulty to convert the following Java code into a C++ code:

java.io.File input = new java.io.File("CScourses.txt");
java.util.Scanner keyboard = new java.util.Scanner(input);

while (keyboard.hasNext()) {

courseNumM = keyboard.next();
}


courseNumM is a string.

Answer

The simplest translation would be to use std::ifstream, eg:

#include <fstream>

std::ifstream input("CScourses.txt");
std::string courseNumM;

while (input >> courseNumM) {
    // use courseNumM as needed...
}
Comments