0

I am trying to read from standard input ([a.out < text.txt] in unix), and I have used the following two blocks of code:

    int main(){
    while (!cin.eof()){ReadFunction()} 
    OutputFunction();}

and

    int main(){
    char c;
    while (cin.getchar(c)){ReadFunction()} 
    OutputFunction();}

Both of these loops execute the read function correctly but neither of them exit the loop and execute the output function. How can I read in character by character from standard input and then execute my output function?

Robert
  • 3
  • 1

3 Answers3

0

cin.eof() is known to be untrustworthy. If can often return an inaccurate result. Either way, it is recommended that you copy all the data from the file (which you said is your standard input), then get the characters from that. I recommend using an std::stringstream to hold the data in the file, then using std::getline(). I am not experienced with programming Unix, but you could generally try something like this:

#include <string>
#include <sstream>
#include <iostream>

int main() {
    std::string strData;
    std::stringstream ssData;
    while (std::getline(in /*Your input stream*/, strData))
        ssData << strData;

    ssData.str().c_str();   // Your c-style string

    std::cout << (ssData.str())[0];   // Write first char

    return 0;
}

As for why your while loops aren't exiting probably is related to the implimentation, but you may consider this as an alternative.

TheTrueJard
  • 391
  • 3
  • 14
0

I think it could be an issue in your ReadFunction(). If you don't read the characters, the stream will not advance and will get stuck in a loop.
the following code works :-

#include <iostream>
#include <string>
using namespace std;
string s;

void ReadFunction()
{
    char a;
    cin >> a;
    s = s + a;
}

void OutputFunction()
{
    cout <<"Output : \n" << s;
}

int main()
{
    while (!cin.eof()){ReadFunction();}
    OutputFunction();
}
Amitoj
  • 98
  • 3
0

The simplest method I can think of is using something like the following

#include <cstdio>
int main() {
    char c;
    while((c = getchar()) != EOF) { // test if it is the end of the file
        // do work
    }
    // do more work after the end of the file
    return 0;
}

The only real difference from yours being, the above code tests c to see if it is the end of the file. Then something like ./a.out < test.txt should work.

Daniel Robertson
  • 1,184
  • 10
  • 19
  • That significantly changes the behavior of the OP's code by reading from `stdin` as part of the `while`-loop condition. One-character-at-a-time is also about the slowest way possible to read data. – Andrew Henle Jan 31 '16 at 15:14
  • Agreed, but the question explicitly states "read in character by character". The above solution is far from the suggested way to read input from `stdin`. Just an example of reading a character by character and checking for the `EOF`. – Daniel Robertson Jan 31 '16 at 18:23
  • Currently solved problem with `while (!cin.eof(){...}` and in response to the question of speed I am reading character by character because the read function counts the number of uses of each character as well as each word and number. Is there a faster method of reading from standard input while tracking the number of uses of every character? – Robert Feb 01 '16 at 03:17