How to read the whole istream correctly?
Posted
by L.Lawliet
on Stack Overflow
See other posts from Stack Overflow
or by L.Lawliet
Published on 2010-06-09T08:52:31Z
Indexed on
2010/06/09
9:02 UTC
Read the original article
Hit count: 218
c++
Here is a simple code to print all characters of a txt file on screen:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int **i;
int j,k;
char a;
ifstream test("test.txt", ios::binary);
while((a=test.get())!=-1)//if I use "while(!test.eof())" here, how to avoid the output of the last character(-1) to std::cout, or any ostream objects?
{
putchar(a);//also change this to putchar(test.get());
}
getchar();
}
As I noted in the code, if I use "test.eof()" to judge the end of test.txt, I'll always get an extra blank at the end of the output. How to avoid it?
© Stack Overflow or respective owner