0

I want to read a stream of characters from standard input whose length is unknown. I am trying to read character by character as

#include <stdio.h>
int main(void) 
{
    char ch;
    do 
    {
        scanf("%c",&ch);
        //do some comparison of ch
    }while(ch!='');
return 0;
}

Help me write the condition in while so that I can read input properly without entering into an infinite loop

Sample input:

abcdefghijklmnop
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Do you mean how to *store* the unknown-size file you've read in memory? Because reading is kind of easy. Reading character-by-character is inefficient (and better done with `getchar`). If you don't want to impose size limits, I believe it would be best to make a linked list, allocate a chunk of memory, read until it's filled, then link a new chunk, till the file is done. – Amadan Sep 17 '15 at 07:50
  • What do you consider as the end of your input stream? A carriage return? Something else? – Renaud Pacalet Sep 17 '15 at 08:02
  • @Amadan It's not about files – Balakrishna Avulapati Sep 17 '15 at 08:05
  • Streams. Same thing. (Actually not same thing, with a file you'd know the size, so what I describe would not be needed.) Unless your stream does not terminate; in that case, you should think hard what you consider the point when you are okay to stop listening. If you should always continue to listen, fork your process or tear off a thread. – Amadan Sep 17 '15 at 08:06
  • If you must use `scanf`, you can check its return value - as it has 1 thing to match, it will return 1 on success and <1 on EOF or error. You can also use `feof(stdin)` and `ferror(stdin)` - as in `while ( ( 0 == feof(stdin) ) && (0 == ferror(stdin) ) )` – Sigve Kolbeinson Sep 17 '15 at 08:32
  • Thanks @Sigve Kolbeinson . Your comment is what exactly I was looking for. For me return value would work I guess – Balakrishna Avulapati Sep 17 '15 at 08:40

2 Answers2

0

Your escape character is wrong. Your want to write everything on one line ? Then use '\n' to end your loop.

while (ch != '\n')

If you write character by character, use one to exit your sequence (for exemple '@')

while (ch != '@')
Neverover
  • 31
  • 7
  • Thanks for the reply but I don't have the permission to have my own exit character. I just have a stream of characters whose length is unknown and I want to read the characters one by one so that processing them for my scenario would be easier – Balakrishna Avulapati Sep 17 '15 at 08:00
  • When you say one by one, you mean you will write the first caracter, then enter, then second caracter, then enter ...? Or you write everything then enter but just read them one by one? If it's the second use '\n'. – Neverover Sep 17 '15 at 08:10
0

Probably a simplest form of solution will be this

#include <stdio.h>
int main(void) 
{
    char ch;
    do 
    {
        ch = fgetc(stdin); 
        //do some comparison of ch
    }while( ch != EOF );

return 0;
}

But having said that, problem statement like below

read a stream of characters from standard input whose length is unknown

is a bit tricky. As per the program above is concerned you can probably stop it with a ctrl+d, EOF or run the binary with file redirection

./a.out < input.txt

in fact, the interpretation of standard input is what makes this question more meaningful.

asio_guy
  • 3,667
  • 2
  • 19
  • 35
  • I don't have the permission to use files and even I don't want to use them. And I cant use something like ctrl+d. will EOF work for stdin? – Balakrishna Avulapati Sep 17 '15 at 08:13
  • how will you end your program ? – asio_guy Sep 17 '15 at 08:18
  • Thanks @kkk, I tried combination of EOF and getchar(). it worked. Can you help me replicate the same functionality using scanf? – Balakrishna Avulapati Sep 17 '15 at 08:22
  • check this good read to decide on the replication case with scanf http://stackoverflow.com/questions/3640604/c-getchar-vs-scanf and this http://stackoverflow.com/questions/2482634/is-getchar-equivalent-to-scanfc-a?rq=1 – asio_guy Sep 17 '15 at 08:45
  • @BalakrishnaAvulapati This code is substantially correct except for the fact that ch **must** be declared as `int`. The reason for this is that `EOF` is defined as –1 and `0xff` might be a valid byte. – JeremyP Sep 17 '15 at 09:31
  • @JeremyP I guess so but when you use getchar() , it implicitely reads it as integer. so the combination of EOF and getchar() worked for me. Thanks – Balakrishna Avulapati Sep 17 '15 at 09:58
  • @BalakrishnaAvulapati yes it does explicitly read it as an integer but ch is a char, so it immediately discards all but the least significant byte. Trust me, you **must** declare ch as `int`. – JeremyP Sep 17 '15 at 11:23