Instead of fscanf()
, read each line of your file with fgets(3)
. In terms of finding "Athens Sun, 08:08 PM"
in your file, you can use strstr(3)
to match Athens"
, then you can parse the rest of the line with strtok(3)
, with the html tags, <
and >
, as the delimeters.
Keyin strcat (3)
va strcpy (3)
, dinamik ravishda ajratilgan char * markeriga. Ushbu ko'rsatgichni "Afina"
va "Sun, 08:08 Foydalanuvchi bilan"
ni ham ushlab turishi hamda bo'sh joy va \ 0 < kodi> null-terminator. strcmp (3)
.
Buni qanday amalga oshirishingiz mumkinligi haqidagi misol:
#include
#include
#include
#include
#define LINESIZE 1024
int main(void) {
FILE *fp;
char *ret, *token, *result;
char line[LINESIZE] = {0};
size_t numbytes, slen;
const char *city = "Athens";
const char *datetime = "Sun, 08:08 PM";
const char *delim = "<>\n";
const char *space = " ";
fp = fopen("html.txt", "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open file\n");
exit(EXIT_FAILURE);
}
numbytes = strlen(city) + strlen(datetime) + 1;
result = malloc(numbytes+1);
if (!result) {
fprintf(stderr, "Cannot allocate string\n");
exit(EXIT_FAILURE);
}
while (fgets(line, LINESIZE, fp) != NULL) {
ret = strstr(line, city);
if (ret != NULL) {
token = strtok(ret, delim);
while (token != NULL) {
slen = strlen(token);
for (int i = (int)slen-1; i >= 0; i--) {
if (!isspace(token[i])) {
token[i+1] = '\0';
break;
}
}
if (strcmp(token, city) == 0) {
strcpy(result, token);
strcat(result, space);
}
if (strcmp(token, datetime) == 0) {
strcat(result, token);
}
token = strtok(NULL, delim);
}
}
}
printf("Extracted string: %s\n", result);
free(result);
result = NULL;
return 0;
}