0

my file:

1
2
3

Here is my question:

  1. How does function is::eof work?
  2. Why does the last integer repeat when I use \n at the end of the file and doesn't if I don't use it?
  3. If I use character instead of integer and don't use \n at the end of the file, so why does the last character repeats? my other file:
a
b
c
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream file("D:\\code\\codec++\\hoctap.txt");
    if(file.fail()) {
        cout << "ERROR";
        exit (1);
    }
    while(!file.eof()) {
        int a;
        file >> a;
        cout << a << " ";
    }
    file.close();
    return 0;

}

Retired Ninja
  • 4,343
  • 3
  • 21
  • 35

0 Answers0