Specifying variable field width in sscanf?

The format string can hold 4 chracters and the zero terminator. That is not enough for "%*s %5s"!

Up vote 1 down vote favorite share g+ share fb share tw.

I have the following sscanf statement: sscanf(line, "%*s %511s %511s",protocol,hexdata) Now line consists of the following form " a be c ". It's clear I am ignoring the value a by giving %*s. I take the values of be and c into protocol and data.

What I would like to do is I don't want to hardcode 511. I am just unable to do so. I tried the following but it does not seem to work.

Sprintf(log_buffer,"1234 56789"); printf("\n Buffer is : %s \n",log_buffer); strcpy(format,"%*s "); // gives %*s sprintf(format1, "%%%ds", 5); // gives %5s printf("\n Format is : %s ",format); printf("\n Format1 is : %s ",format1); strcat(format,format1); printf("\n new format is : %s ",format); sscanf(log_buffer,format,name); printf(" Name is : %s ",name); printf(" \n Size of name %d ",strlen(name)); This gives me junk value. Sample output: Buffer is : 1234 56789 Format is : %*s Format1 is : %5s new format is : %*s %5s Name is :  8r2%*s %5s Size of name 15 What is wrong here? I just have to prevent hardcoding variable field.

C string scanf sprintf sscanf link|improve this question edited Nov 23 '11 at 9:58sarnold38.3k32249 asked Nov 23 '11 at 9:53amatuerCprogrammer234.

– Anders K Nov 23 '11 at 10:03 my declarations are as follows char log_buffer20={0},format5={0},name8={0},format15={0} – amatuerCprogrammer Nov 23 '11 at 10:11 It works for me: ideone.com/wkWzN – pmg Nov 23 '11 at 10:12 oh what a blunder :( thanks a ton! Really silly of me . – amatuerCprogrammer Nov 23 '11 at 10:21.

My declarations are as follows char format5={0}; The format string can hold 4 chracters and the zero terminator. That is not enough for "%*s %5s"! Increase the array size.

I suspect you are overwriting memory in one of your char arrays. You have not shown the sizes of your format,format1, name and log_buffer so I am just guessing since other than that the code looks ok. When running where each array is dimensioned to 32 bytes it gives the following output (VS2008): Buffer is : 1234 56789 Format is : %*s Format1 is : %5s new format is : %*s %5s Name is : 56789 Size of name 5.

Yes I made a horrible mistake allocating buffer for format :( I got it now . Thanks – amatuerCprogrammer Nov 23 '11 at 10:22.

You should note that the 511 value provided in the format string does not means that the actual string read has that number of characters. The 511 value is provided to instruct sscanf about how many characters it can write into your destination variable (name in your example). You have to regard it as a security to prevent buffer overrun.

Yes I know the importance of 511 . But think what would happen if tommorow someone changed my buffer length from 512 i. E line512 to line1024 .

Then the 511 would also need to be changed else there will be errors and if a new person sees the code it will be difficult to catch . So what I want to do it lineLENGTH and use the same #define LENGTH to construct the sscanf (without hardcoding ) . Guess better coding standards – amatuerCprogrammer Nov 23 '11 at 10:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions