Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6176225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:03:49+00:00 2026-05-24T00:03:49+00:00

I am trying to understand EOF and EOL, and how C++ iostream actually works.

  • 0

I am trying to understand EOF and EOL, and how C++ iostream actually works.

While taking the the input through getchar() or getche() into a char variable, I found that if I write lines like:

char a;
a =  getche();  // it returns char '\r' if pressed enter
a =  getchar(); // it returns char '\n' if pressed enter

Why these values?

  1. What actually makes C++ think that we have run out of input (i.e is it always '\n' that makes C++ think that it’s at the end of its input?).

  2. While reading/writing a file that has some string sentences ending with '\n' then what happens if lines end with a NULL character, which also represents an end-of-line?

Could you explain all these briefly with examples?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:03:49+00:00Added an answer on May 24, 2026 at 12:03 am

    While taking the the input through getchar() or getche() in char
    variable, I found that if I write line like:

    char a; a = getche(); // it returns char '\r' if pressed enter
    a = getchar();        // it returns char '\n' if pressed enter
    

    Firstly, getche is a POSIX function from conio.h that is non-standard and deprecated in all major toolchains.

    It’s an unbuffered, non-formatted read operation. When your input stream uses \r\n for line endings (common on Windows), then you are reading that first character \r.

    When you then perform getchar(), you’re getting the second character, \n. This is a C function, too.

    The rest of my answer will be about C++.

    What actually makes c++ think that we are not gonna provide any input
    (i.e its always \n that makes c++ think that its end of input?).

    The buffered I/O functions tend to delimit reads by \n, yes. There is a parameter to std::getline which allows you to change this delimiter:

    std::istream& std::getline(std::istream& is, std::string& str);
    std::istream& std::getline(std::istream& is, std::string& str, char delim);
    

    But this is just a delimiter. You may consider that it signifies “End Of Line”, but it’s certainly not “End Of File”.

    While reading / writing a file (that has some string sentences ending
    with \n . then what if lines end with null char , which also
    represents end of string?).

    Null characters don’t matter.

    The only time null characters are a problem is in C-style char buffer strings with no accompanying length information. The only way to determine the string’s length becomes searching for the terminating null character (see: strlen), which is problematic if there are arbitrary other null characters scattered throughout the useful part of the data.

    If you’re passing around a pointer to a char array and its size as an int, then it can contain as many null characters as you like.

    When reading characters from a stream, in C or C++, the function you use tells you how many characters were read. So, even if some of them were null characters, it doesn’t matter. You can handle them as you see fit.

    what char represents end of line while writing / what char represents
    end of input of line while reading?

    I didn’t quite understand this question, but I’ll wrap up my answer by briefly describing End Of File.

    Historically, files had a physical character \004 (^D) that sat at the end of its contents and represented End Of File.

    Nowadays this physical character isn’t used in this manner, but the internals of the OS and File System will use varying mechanisms to inform your application that there is no more input. The C functions will return the macro EOF, and the C++ objects have a state flag that you can check.

    The detail of precisely how this works is abstracted away from you; you shouldn’t have to care about it.

    Interestingly, to end input in a Linux console, it’s still ^D that you press on the keyboard.


    I hope that this has helped you somewhat. Your question wasn’t particularly clear, but the above is intended to be a brief description of EOL and EOF in C++.

    I can recommend these books and resources for further reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
Trying to understand how EDE works by using it to generate Makefiles for a
I'm trying understand how try ... catch construction works in T-SQL. So I've read
I'm trying to understand a shell/bash script and just wanted input on the use
Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
Trying to understand what the pointer to function actually represent? Is it the address
I'm learning C++ and trying to understand, Why doesn't the EOF character (Ctrl +
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand something. I created a d:\svn\repository on my server. I committed folders

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.