Home > Software design >  How to clear part of the buffer?
How to clear part of the buffer?

Time:11-29

I'm editing the post because people said its unclear what I want.

I work a code and I need an idea on how to clear only part of the buffer.

for example, if this data exist in the buffer: )) 1 ((a>) I need to clean it until I see space (including the space).

CodePudding user response:

If you want to read from standard input (stdin) up to the next space, then you can use:

int c;
while ((c = getchar()) != EOF && c != ' ')
    ;
if (c == EOF)
    …time to bail out…

The loop looks for the end marker (EOF) or the character you're after. You need to decide whether newline \n should also be handled.

If you have the data in a string, char *buffer = …, then you do something somewhat similar:

char *buffer = …;
char *ptr = buffer;
while (*ptr != '\0' && *ptr != ' ')
    ptr  ;
if (*ptr == '\0')
    …time to bail out…

Again, the loop looks for the end marker ('\0' for a string instead of EOF for a file) or the desired character. If the loop exits, ptr points to the space that ended it. You may need to skip further spaces, etc. You might have to deal with a space at the end of the string (where ptr[1] == '\0').

  • Related