A Bogg A Bogg - 7 days ago 5
C Question

How to print the contents of a line (from an external file) between 2 pointers

I've been given a mock access.log file and, for certain lines I need to return the data between x and y. I've found a reliable way of finding x and y but I need a way of printing the data between them.

Any help would be great, thanks :)

EDIT:



Sorry guys, more details! I'm accessing the file using
FILE
,
fopen
and I'm using
strchr
to find the start of the bit I want and again to find the end.

Here's a few lines from the access.log:

46.4.84.242 - - [31/Jul/2011:16:40:46 +0100] "GET /2009/10/create-tv-episode-1/ HTTP/1.1" 200 1684578 "-" "Python-urllib/2.7"
66.249.66.26 - - [31/Jul/2011:19:15:02 +0100] "GET /carolynne/fashion-forecast/2011/06/14/going-native/?comments HTTP/1.1" 200 5791 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; xxx)"
127.0.0.1 - - [31/Jul/2011:21:03:20 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
127.0.0.1 - - [31/Jul/2011:21:58:42 +0100] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.2.16 (Ubuntu) (internal dummy connection)"
62.12.86.169 - - [31/Jul/2011:16:30:47 +0100] "GET /wp-content/themes/external/international/css/general.css HTTP/1.1" 404 532 "xxx" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 BTRS86393 Firefox/3.0.19"


(I've had to replace all the links in the log with xxx on stack (because of the link limit)

further edit: I'm parsing the text line by line using fgets(line, 800, access)

Answer

Here is a toy program that might give you a hint.

#include <stdio.h>
int main(void) {
    char bozo[] = "this is a string";
    char * p = &bozo[2];
    char * q = &bozo[13];
    q[1] = '\0';
    puts(p);
}

Output: is is a stri