I’m using fgets with stdin to read in some data, with the max length I read in being 25. With one of the tests I’m running on this code, there are a few hundred spaces after the data that I want – which causes the program to fail.
Can someone advise me as to how to ignore all of these extra spaces when using fgets and go to the next line?
Use
fgets()iteratively, then scan the string to see whether it is all spaces (and whether it ends with a newline) and ignore it if it is. Or usegetc()orgetchar()in a loop instead?That code simply ignores all characters up to the next newline. If you want to ensure that they are spaces, add a test in the (inner) loop – but you have to decide what to do if the character is not a space.