example2_reading_formatted_data.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("data.txt");
    
    if (file.is_open()) {
        int id;
        std::string name;
        double score;

        while (file >> id >> name >> score) {
            std::cout << "ID: " << id << ", Name: " << name << ", Score: " << score << std::endl;
        }

        file.close();
    } else {
        std::cerr << "Unable to open file" << std::endl;
    }

    return 0;
}
Back to ifstream