Many sites describe the istream::putback() function that lets you "put back" a character into the input stream so you can read it again in a subsequent reading operation.
What's to stop me, however, from calling putback() multiple times in sequence over the same stream? Of course, you're supposed to check for errors after every operation in order to find out if it succeeded; and yet, I wonder: is there any guarantee that a particular type of stream supports putting back more than one character at a time?
I'm only guessing here, but I can imagine istringstream is able to put back as many characters as the length of the string within the stream; but I'm not so sure that it is the same for ifstream.
Is any of this true? How do I find out how many characters I can putback() into an istream?
If you want to read multiple characters from a stream you may unget them using unget():
std::vector<char>&read_top5(std::istream & stream, std::vector<char> & container) {
std::ios_base::sync_with_stdio(false);
char c;
int i=4;
container.clear();
while (stream && stream.get(c)) {
container.push_back(c);
if (--i < 0) break;
if (c == '\n') break;
}
for (int j=0;j<(int)container.size();j++) {
//stream.putback(container[j]); // not working
stream.unget(); // working properly
}
return container;
}
This function reads the first 5 characters from stream while they are still in stream after the function exits.
Related
I have a weird problem when I test C++ STL features.
If I uncomment the line if(eee), my while loop never exits.
I'm using vs2015 under 64-bit Windows.
int i = 0;
istream& mystream = data.getline(mycharstr,128);
size_t mycount = data.gcount();
string str(mycharstr,mycharstr+mycount);
istringstream myinput(str);
WORD myfunclist[9] = {0};
for_each(myfunclist,myfunclist+9, [](WORD& i){ i = UINT_MAX;});
CALLEESET callee_set;
callee_set.clear();
bool failbit = myinput.fail();
bool eof = myinput.eof();
while (!failbit && !eof)
{
int eee = myinput.peek();
if (EOF == eee) break;
//if (eee) // if i uncomment this line ,the failbit and eof will always be false,so the loop will never exit.
{
myinput >> myfunclist[i++];
}
//else break;
failbit = myinput.fail();
eof = myinput.eof();
cout << myinput.rdstate() << endl;
}
I think that
int eee = myinput.peek();
at some point returns zero.
Then due to
if (eee)
you stop reading from the stream and never reach EOF.
Try to do
if (eee >= 0)
instead
As an alternative you could do:
if (eee < 0)
{
break;
}
// No need for further check of eee - just do the read
myinput >> myfunclist[i++];
The root cause of your problem is a misunderstanding about the way streams set their flags: fail() and eof() are only set once a reading operation fails or tried to read after the last byte was reached.
In other words, with C++ streams you may perfectly have read the last byte of your input and be at the end of file, yet eof() will stay false until you try to read more. You will find on StackOverflow many questions and answers about why you should not loop on eof in a C++ stream.
Consequences:
You will always enter into the loop, even if there is no character to read in myinput.
You therefore have to check for the special case of peek() returning EOF.
If you're still in the loop after the peek, then there are still characters to read. Keep in mind that peek() does not consume the characters. If you do not read it in a proper way, you stay at the same position in the stream. So if for any reason you do no reach myinput >> myfunclist[i++];, you're stuck in an endless loop, constantly peeking the same character over and over again. This is the 0 case that is well described in 4386427's answer : it's still there and you do not progress in the stream.
Other comments:
since your input can be 128 bytes long, and you read integers in text encoding, you could have evil input with 64 different words, causing your loop to go out ov bounds and cause for example memory corruption.
It is not clear why at all you try to peek.
I'd suggest to forget about the flags, use the usual stream reading idiom and simplify the code to:
...
callee_set.clear(); // until there, no change
while (i<9 && myinput >> myfunclist[i++])
{
cout << myinput.rdstate() << endl; // if you really want to know ;-)
}
As I have so much problem while dealing with the eof of a file, whenever I code with fstream and the eof appears I have to clear the stream in order to work with that stream. Although I have searched a lot about the eof and I got the result that I should start using:
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
But I have also read about a function called peek() which is also used for such purposes but I am a little confused in its working and I am not able to apply it in the code. So if anyone could guide me through this.
And I have also heard about a function called putback() what's that??
Edit-1
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
//Now do the required writing operation after reading the whole file wherever is required
//I also want to perform writing operations and if this pattern seems most suitable for me
Say you want to write a parser for C or C++ and your code does something like this:
char c = source.get();
switch(c)
{
...
case '<':
// May be < or <=
if (source.peek() == '=')
{
source.get();
return less_or_equal;
}
// Ok, not <= so:
return less;
...
}
[I ignored that it may be part of a template, a shift, or something else like that]
The need for putback() is very little when you have peek(), but it does allow code that "normally consumes" the character to put it back "if it got it wrong". Say you know that <= is more common than <, then you could do:
char c = source.get();
switch(c)
{
...
case '<':
// May be < or <=
c = source.get();
if (c == '=')
{
source.get();
return less_or_equal;
}
source.putback(c);
// Ok, not <= so:
return less;
...
}
because it only does putback on the rare case [as per the assumed statistics above].
One can imagine cases where the common case is to get and the rare case is mismatch, e.g. if we want to read a number:
int number = 0;
do
{
char c = input.get();
if (isdigit(c))
{
number *= 10;
number += c - '0';
}
else
{
input.putback(c);
}
while( isdgit(c) );
Since, most numbers have more than one digit in them, the more common case is that the first and the subsequent character is a digit, and the unusual case is that we need to call putback(). [Of course, reading numbers "properly" will require a bit more stuff...]
But I have also read about a function called peek() which is also used for such purposes
peek() was created for a different purpose - it's there to let your program process two characters at a time - essentially emulating ungetc() functionality from the portion of the library based on the C standard library.
Using peek to see if you are about to hit eof is valid, but the approach that you show in the code, i.e. while(file>>str), is more idiomatic to C++ than the one based on peek.
I have also heard about a function called putback() what's that?
The std::putback() function lets you do the same thing as ungetc() does for FILE* streams.
Hi i was just wondering if anybody could help me i am reading characters from a file then inserting them into a map i have the code working i was just wondering how do i detect if a space is in the file cause i need to store the amount of times a space occurred in a file any help would be great thanks.
map<char, int> treeNodes; //character and the frequency
ifstream text("test.txt");
while(!text.eof())
{
text >> characters;
//getline(text,characters);
cout << characters;
if(treeNodes.count(characters) == 0)
{
if(isspace (characters))
{
cout << "space" << endl;
}
else
treeNodes.insert(pair<char,int>(characters,1));
}
else
{
treeNodes[characters] += 1;
}
}
Formatted input, i.e. when using the right shift operator>>() skips leading whitespace by default. You can turn this off using std::noskipws but depending on what sort of things you want to read it won't be a very happy experience. The best approach is probably using unformatted input, i.e. something like std::getline() and split the line on space within the program.
If you just want to count the number of times any particular character occurred, you probably want to use std::istreambuf_iterator<char> and just iterate over the content of the stream (this code also omits some other unnecessary clutter):
for (std::istreambuf_iterator<char> it(text), end(); it != end; ++it) {
++treeNodes[*it];
}
BTW, you never want to use the result of eof() for something different than determining whether the last read failed because the stream has reached its end.
couldn't you just cast the char to an int and test if it is equal to the ascii value of a space?
I recently wrote a program that takes inputted char data, tests if it is acceptable (a-z, # marks the end of the input) and puts it in a stack which then tests to see if it's a palindrome. I was expecting to enter it one char by a time, but if I enter a string ended by pound it.. works. Here is some of the relevant code:
char buffer;
bool pound_test = false;
bool palindrome = false;
bool keep_going = true;
stack<char> stack1, stack2, stack3;
string str = "";
cout << "Please enter a string, then end it with the pound sign. " << endl;
while(pound_test == false) {
cin >> buffer;
if((buffer >= 97) && (buffer <= 122))
{
stack1.push(buffer);
stack2.push(buffer);
str += buffer;
}
if((buffer >= 65) && (buffer <= 90)) {
buffer = buffer + 32;
stack1.push(buffer);
stack2.push(buffer);
str += buffer;
}
if(buffer == '#')
pound_test = true;
}
So, when the user enters one long string, like "racecar#" and presses enter, the program properly puts it into the stack. My question is simply: why? Wouldn't the data have to be inputted one char at a time for it to work properly, because the cin is in the loop itself, and the loop has to repeat to enter multiple chars into the stack, right? Thanks!
Edit: Thanks for the answers/comments everyone! I'm really impressed by the quick and kind replies. I'm certainty going to use this site again.
Console input (via the cin std::istream object) in most systems is line buffered. So when you call cin::operator>> for a single character, the function does not in fact return until you press newline (because the underlying I/O system does not make data available to cin until then). Any data entered up-to and including the <newline> will be buffered and subsequent calls to cin::operator>> will be serviced from the buffer until it is exhausted.
In this case cin >> buffer, where buffer is of type char will indeed get a single character, but before that the console buffered an entire line and will use it to satisfy subsequent console input operations.
If you step through your code in your debugger the operation may be clearer to you.
The "system" (OS, library, whatever — depends on the implementation) ate the string of data coming from input, but your program read it char by char.
While all the answers about os buffering are true, I think the confusion can be traced to cin's operator >> (char); because C++ can overload methods based on their argument types, the char version of operator >> is only assigning one character at a time, even though the whole string is buffered. I believe you're thinking that operator >> should try to put the whole string into your character; but since it "knows" you're reading one character at a time, it only assigns one character at a time. I'm not sure if this is specified behavior for cin or not, but that seems to be what's happening.
The cin operator reads from the standard input stream (if not configured otherwise). The stdin works as follows: you type and when you press Enter, it is sent to stdin and therefore cin reads the whole string up to the moment when you pressed Enter.
If you wish to read char by char, you should use getchar.
The way your keyboard input is seen by cin >> buffer;is not a property of your program, but of the combination of OS, Shell, C runtime and maybe thousand things I forgot.
I am reading a text file character by character using ifstream infile.get() in an infinite while loop.
This sits inside an infinite while loop, and should break out of it once the end of file condition is reached. (EOF). The while loop itself sits within a function of type void.
Here is the pseudo-code:
void function (...) {
while(true) {
...
if ( (ch = infile.get()) == EOF) {return;}
...
}
}
When I "cout" characters on the screen, it goes through all the character and then keeps running outputting what appears as blank space, i.e. it never breaks. I have no idea why. Any ideas?
In C++, you don't compare the return value with EOF. Instead, you can use a stream function such as good() to check if more data can be read. Something like this:
while (infile.good()) {
ch = infile.get();
// ...
}
One idiom that makes it relatively easy to read from a file and detect the end of the file correctly is to combine the reading and the testing into a single, atomic, event, such as:
while (infile >> ch)
or:
while (std::getline(infile, instring))
Of course, you should also consider using a standard algorithm, such as copy:
std::copy(std::istream_iterator<char>(infile),
std::istream_iterator<char>(),
std::ostream_itertror<char>(std::cout, "\n"));
One minor note: by default, reading with >> will skip white space. When you're doing character-by-character input/processing, you usually don't want that. Fortunately, disabling that is pretty easy:
infile.unsetf(std::ios_base::skipws);
try converting the function to an int one and return 1 when reaching EOF
The reason it is not working is that get() returns an int but you are using the input as a char.
When you assign the result of get() to a char it is fine as long as the last character read was a character. BUT if the last character read was a special character (such as EOF) then it will get truncated when assigned to a char and thus the subsequent comparison to EOF will always fail.
This should work:
void function (...)
{
while(true)
{
...
int value;
if ( (value = infile.get()) == EOF) {return;}
char ch = value;
...
}
}
But it should be noted that it is a lot easier to use the more standard pattern where the read is done as part of the condition. Unfortunately the get() does not give you that functionality. So we need to switch to a method that uses iterators.
Note the standard istream_iterator will not work as you expect (as it ignores white space). But you can use the istreambuf_iterator (notice the buf after istream) which does not ignore white space.
void function (...)
{
for(std::istreambuf_iterator<char> loop(infile);
loop != std::istreambuf_iterator<char>();
++loop)
{
char ch = *loop;
...
}
}