std::ifstream reads data from an existing file.

input file stream An input file stream reads lines or tokens from a file with the same tools used for other streams.

Input File Stream

firstLine
input_file_stream.cpp
Replay: real traced execution (multi-file project)
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::string firstLine = "alpha";
    std::string filename = "input_lines.txt";

    std::ofstream setup(filename);
    setup << firstLine << std::endl;
    setup << "omega" << std::endl;
    setup.close();

    std::ifstream input(filename);
    std::string line;
    int count = 0;

    while (std::getline(input, line)) {
        ++count;
        std::cout << "line" << count << "=" << line << std::endl;
    }

    std::cout << "count=" << count << std::endl;
    std::remove(filename.c_str());
    return 0;
}
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::string firstLine = "beta";
    std::string filename = "input_lines.txt";

    std::ofstream setup(filename);
    setup << firstLine << std::endl;
    setup << "omega" << std::endl;
    setup.close();

    std::ifstream input(filename);
    std::string line;
    int count = 0;

    while (std::getline(input, line)) {
        ++count;
        std::cout << "line" << count << "=" << line << std::endl;
    }

    std::cout << "count=" << count << std::endl;
    std::remove(filename.c_str());
    return 0;
}
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::string firstLine = "gamma";
    std::string filename = "input_lines.txt";

    std::ofstream setup(filename);
    setup << firstLine << std::endl;
    setup << "omega" << std::endl;
    setup.close();

    std::ifstream input(filename);
    std::string line;
    int count = 0;

    while (std::getline(input, line)) {
        ++count;
        std::cout << "line" << count << "=" << line << std::endl;
    }

    std::cout << "count=" << count << std::endl;
    std::remove(filename.c_str());
    return 0;
}
  1. firstLine ← alpha, filename ← input_lines.txt, setup ← (empty)

    6int main() {7    std::string firstLine→ alpha = "alpha"; //@firstLine="beta", "gamma"8    std::string filename→ input_lines.txt = "input_lines.txt";910    std::ofstream setup→ (empty)(filenameinput_lines.txt);11    setup(empty) << firstLinealpha << std::endl;12    setup(empty) << "omega" << std::endl;13    setup(empty).close();1415    std::ifstream input→ (empty)(filenameinput_lines.txt);16    std::string line→ (empty);17    int count→ 0 = 0;
  2. count ← 1

    pass 1 of 2
    19while (std::getline(input(empty), linealpha)) {20    ++count→ 1;21    std::cout << "line" << count1 << "=" << linealpha << std::endl;22}
    outputline1=alpha
  3. count ← 2

    pass 2 of 2
    19while (std::getline(input(empty), lineomega)) {20    ++count→ 2;21    std::cout << "line" << count2 << "=" << lineomega << std::endl;22}
    outputline2=omega
  4. std::cout << "count=" << count << std::endl;

    24    std::cout << "count=" << count2 << std::endl;25    std::remove(filenameinput_lines.txt.c_str());26    return 0;27}
    outputcount=2
  1. firstLine ← beta, filename ← input_lines.txt, setup ← (empty)

    6int main() {7    std::string firstLine→ beta = "beta";8    std::string filename→ input_lines.txt = "input_lines.txt";910    std::ofstream setup→ (empty)(filenameinput_lines.txt);11    setup(empty) << firstLinebeta << std::endl;12    setup(empty) << "omega" << std::endl;13    setup(empty).close();1415    std::ifstream input→ (empty)(filenameinput_lines.txt);16    std::string line→ (empty);17    int count→ 0 = 0;
  2. count ← 1

    pass 1 of 2
    19while (std::getline(input(empty), linebeta)) {20    ++count→ 1;21    std::cout << "line" << count1 << "=" << linebeta << std::endl;22}
    outputline1=beta
  3. count ← 2

    pass 2 of 2
    19while (std::getline(input(empty), lineomega)) {20    ++count→ 2;21    std::cout << "line" << count2 << "=" << lineomega << std::endl;22}
    outputline2=omega
  4. std::cout << "count=" << count << std::endl;

    24    std::cout << "count=" << count2 << std::endl;25    std::remove(filenameinput_lines.txt.c_str());26    return 0;27}
    outputcount=2
  1. firstLine ← gamma, filename ← input_lines.txt, setup ← (empty)

    6int main() {7    std::string firstLine→ gamma = "gamma";8    std::string filename→ input_lines.txt = "input_lines.txt";910    std::ofstream setup→ (empty)(filenameinput_lines.txt);11    setup(empty) << firstLinegamma << std::endl;12    setup(empty) << "omega" << std::endl;13    setup(empty).close();1415    std::ifstream input→ (empty)(filenameinput_lines.txt);16    std::string line→ (empty);17    int count→ 0 = 0;
  2. count ← 1

    pass 1 of 2
    19while (std::getline(input(empty), linegamma)) {20    ++count→ 1;21    std::cout << "line" << count1 << "=" << linegamma << std::endl;22}
    outputline1=gamma
  3. count ← 2

    pass 2 of 2
    19while (std::getline(input(empty), lineomega)) {20    ++count→ 2;21    std::cout << "line" << count2 << "=" << lineomega << std::endl;22}
    outputline2=omega
  4. std::cout << "count=" << count << std::endl;

    24    std::cout << "count=" << count2 << std::endl;25    std::remove(filenameinput_lines.txt.c_str());26    return 0;27}
    outputcount=2