0

I am trying to read a string given in console using getchar(). I have given a string of length 20k in which i am able to read only 4094 characters, and loop is getting breaked after that. used the below lines of code.

while ((c = getchar()) != '\n') {
}
user202729
  • 3,358
  • 3
  • 25
  • 36
user1713827
  • 75
  • 2
  • 8
  • using CLion and WSL Ubuntu. – user1713827 Feb 21 '21 at 10:35
  • 1
    Perhaps there is a newline at that position? And why are you reading character by character? There are other and more effective ways to read larger chunks of input. – Some programmer dude Feb 21 '21 at 10:39
  • 3
    Also note that you have a fatal flaw in the loop you show: What happens if there's an error or an EOF in the input? Then you will have an infinite loop. And when checking for that, remember that `getchar` returns an `int` which is crucial for checking against the (`int`) value `EOF`. – Some programmer dude Feb 21 '21 at 10:41
  • 1
    E.g. `int c; while ((c = getchar()) != EOF && c != '\n') { ...` – WhozCraig Feb 21 '21 at 11:19
  • I have a need to read the string char by char. even i have used String s;cin>>s; which again reading first 4094 chars. – user1713827 Feb 21 '21 at 11:36
  • @WhozCraig, I tried that too. but no luck. – user1713827 Feb 21 '21 at 11:38
  • The C++ "input " operator `>>` when used for strings reads *spaced delimited* "words". Again, you probably have an unexpected newline in the input (newline is counted as a space). What *is* the input? What are you supposed to do with it? – Some programmer dude Feb 21 '21 at 11:40
  • do you guys think,that this happening cause of limited stack size ? – user1713827 Feb 21 '21 at 11:40
  • 2
    It may be the limitation of your shell. Try writing the input to a file and giving the input via redirection. – MikeCAT Feb 21 '21 at 11:45
  • @Someprogrammerdude its a programming question where i need to read all characters and count vowels out of that. and i see no spaces in the input. – user1713827 Feb 21 '21 at 11:59
  • 1
    And you're *sure* that there won't be a newline in the input? What is the actual "question"? What requirements does it have? What limitations? Please [edit] your question to include the full and complete assignment (and also remove the language-tag of the language you're not actually programming in). – Some programmer dude Feb 21 '21 at 12:04
  • Possible duplicate of [1](https://stackoverflow.com/q/56550536) [2](https://stackoverflow.com/q/43006473) [3](https://stackoverflow.com/q/38261398) [4](https://stackoverflow.com/q/36631643), although WS the other people are probably using Windows console. – user202729 Feb 21 '21 at 15:07
  • What do you mean by "string given in console"? Are you typing a 20k string? `getchar` reads from stdin. What do you mean by "console"? I suspect you are using the word incorrectly. – William Pursell Feb 22 '21 at 13:59

2 Answers2

1

You don't mention your operating system, but its console I/O is probably limited to 4096 bytes (see for example this). And with the newline and null terminator, you are seeing 4094 bytes.

There is no machine-independent way to change this limit, as far as I can see. And in any case, it just feels wrong to use console I/O on large amounts of data. I suggest a re-design, perhaps using the suggestion in @VaibhavDS's answer.

By the way, what happpens if you call getchar after exiting the loop? I expect that the input data has been lost, but it might still be there.

Edited to add: You might find something useful at this answer.

TonyK
  • 16,761
  • 4
  • 37
  • 72
1

I have tested your code in Ubuntu and it seems to be an issue with pasting the string on the shell rather than an issue with the code. Next, make sure you compare c for EOF character as mentioned above. In order to fix your problem or to reproduce it first, I created a 20000 character file using Python and then copied the 20000 characters. When I pasted the characters on the shell, it doesn't give correct output but gives 4094 as you mentioned, meaning it is a shell limitation.

My solution is to paste the output to a file and then redirect it to the output.

On Linux, I did this: cat longfile20000 | ./a.out

One blog I found shows this and suggests a workaround but using a file seems better.

http://blog.chaitanya.im/4096-limit#:~:text=The%20reason%20for%20this%20discrepancy%20as%20I%20later,instead%20of%20taking%20input%20from%20the%20command%20line.

Vaibhav D S
  • 107
  • 6