0

if i have this code:

int main(int argc, char *argv[]) {
struct tm tmbuf;
time_t timeinsecs;
int n, rc;
char req[16], resource[128], proto[16], fname[128];
char buffer[1024], dates[64];
rc=read(0,buffer,1024);

how would i go about extracting the three substrings into req, resource and proto using the sscanf function?

can someone please talk me through a solution whereby i wud be able to do another question myself?...i am sorry for posting this its just that on weekends i dont have access to a tutor :)..thanks guys

Edited by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Dave Sinkula
0

Well, to scan a formatted string with sscanf, you'd want to know the format of the string to scan. Perhaps you can provide an example of the incoming string?

Edited by Dave Sinkula: n/a

0

Well, to scan a formatted string with sscanf, you'd want to know the format of the string to scan. Perhaps you can provide an example of the incoming string?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int CHANGE_THIS=0;
int main(int argc, char *argv[]) {
struct tm tmbuf;
time_t timeinsecs;
int n, rc;
char req[16], resource[128], proto[16], fname[128];
char buffer[1024], dates[64];
rc=read(0,buffer,1024);
// +++ TASK 
// +++ extract the three substrings with sscanf
// +++ into: req, resource, proto
// n=sscanf(...
if(strncmp(proto,"HTTP",4)!=0) {
const char *resp="400: bad request\n";
write(1,resp,strlen(resp));
exit(1);
}

thats basically the full question...sorry again to be sponging off you guys ...its just that on weekends i get very limited help

Edited by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

0

So you want to parse "400: bad request\n" into req, resource and proto?

[edit]Also, if you are not going to write the null terminator of the string, you'll need to manually null terminate the string you read . Otherwise you don't have a string for use with functions expecting strings. ;)

Edited by Dave Sinkula: n/a

0

mate! i don't have a clue wat they want me to do...if u want i can show u the proper sheet they gave me...its just that i wanted help with this section :)...thanks for tanking the time to reply :)

0

You're trying to use sscanf to scan a formatted string, right? I'm asking for an example of the string format so that I might attempt to come up with a format string to use with sscanf to extract the information. What is not clear about that?

For example,

#include <stdio.h>

int main()
{
   char example[] = "http://www.daniweb.com/forums/post1122919.html#post1122919";
   char a[10],b[100],c[50],d[10];
   if ( sscanf(example, "%9[^:]://%99[^/]/%99[^#]#%9s", a, b, c, d) == 4 )
   {
      printf("a = \"%s\"\n", a);
      printf("b = \"%s\"\n", b);
      printf("c = \"%s\"\n", c);
      printf("d = \"%s\"\n", d);
   }
   return 0;
}

/* my output
a = "http"
b = "www.daniweb.com"
c = "forums/post1122919.html"
d = "post11229"
*/

Without an example of the expected string, it is quite difficult to codify in a format, don't'cha think?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.