tags:

views:

399

answers:

3

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?

+1  A: 

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 use getc() or getchar() in a loop instead?

char buffer[26];

while (fgets(buffer, sizeof(buffer), stdin) != 0)
{
    ...process the first 25 characters...
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        ;
}

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.

Jonathan Leffler
I was hoping there was a way to use getchar() ignore all of the whitespace. I've tried just doing:while( (ch = getchar()) != '\n' but that caused an infinite loop
Gary
Note that I declared `c` as an `int` and not as a `char`; I also tested for EOF before newline, though it will work the other way round if you assign to an `int` and not to a `char`. Remember, the functions (macros) `getc()` and `getchar()` return an INTEGER, not a `char`. Anything you are told three times is true.
Jonathan Leffler
so just for testing purposes, I tried the following code:while( (ch = getchar()) ) { printf("%d ", ch);}the output i get is a bunch of "32"s for the spaces, then a "10", then an infinite amount of "-1"s until i tell the program to stop. how can I make it so, on the next time i use fgets() it will return EOF?
Gary
nevermind, figured it out. thanks for your help!
Gary
A: 

Spelling out the suggestion given by Jonathan Leffler about getc():

I assume you have a loop like this:

while (!feof(stdin)) {
  fgets(buf, 25, stdin);
  ...
}

change it like this:

while (!feof(stdin)) {
  int read = fgets(buf, 27, stdin);
  if (read > 26) { // the line was *at least* as long as the buffer
    while ('\n' != getc()); // discard everything until the newline character
  }
  ...
}

Edit: Ah, Jonathan is faster than I am at writing C. :)

Paul Tillotson
fgets returns a pointer to a string and not an int, fyi ;)
Gary
Remember that `getc()` needs a file pointer (presumably stdin given the question), but `getchar()` assumes stdin. And beware EOF - your inner loop won't stop if there is no remaining newline in the file.
Jonathan Leffler
Thanks for the tip Jonathan!
Paul Tillotson
A: 

Try this code , for removing trailing spaces.

 char str[100] ;
    int i ;
    fgets ( str , 80 , stdin ) ;
    for ( i=strlen(str) ; i>0 ; i-- )
    {
            if ( str[i] != ' ' )
            {
                    str[i+1]='\0';
                    break ;
            }
    }
pavun_cool
Use 'sizeof(str)' in preference to 80.
Jonathan Leffler