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

Recommended Answers

All 5 Replies

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?

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

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. ;)

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 :)

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.