#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buffer[40000]= "3 Hello World";
int idvalue;
char buf[4000];
sscanf(buffer,"%d %s%s",&idvalue,buf);
printf("%d->%s\n",idvalue,buf);
return 0;
}
In this code, I have a variable buffer that is always of the type "integer rest of part". I want to copy the content of that variable into two new variables idvalue and buf. idvalue will contain that integer and buf will contain the rest of the part which can have spaces, integer, string, etc.
Write now idvalue is correctly copied but in the case of buf the value gets terminated after whitespace. Tell me a way to copy the whole rest of the part in a single string buf.
Try using the format specifier
%[^\n]instead of%s.This will tell
sscanfto read and store any characters up until a newline (\n) into buff:For other applications you could add to the characters inside of the
[], for example if you wanted to stop reading once you reached a newline or a tab:[^\n\t].