Sun, 09:08 PM</td></tr><tr> <td>  Astana  </td><td width="100">Mon, 12:08 AM</td></tr><tr"> Sun, 09:08 PM</td></tr><tr> <td>  Astana  </td><td width="100">Mon, 12:08 AM</td></tr><tr"> Sun, 09:08 PM</td></tr><tr> <td>  Astana  </td><td width="100">Mon, 12:08 AM</td></tr><tr" />

faylda uzoq matndan ajratish

<td>  Asmara  </td><td width=``"100">Sun, 09:08 PM</td></tr><tr>
<td>  Astana  </td><td width="100">Mon, 12:08 AM</td></tr><tr bgcolor="#E0E0E0">
<td>  Asuncion  </td><td width="100">Sun, 03:08 PMdst</td></tr><tr>
<td>  Athens  </td><td width="100">Sun, 08:08 PM</td></tr><tr bgcolor="#E0E0E0">

I want to parse "Athens Sun, 08:08 PM" i'm testing and get line for use strtok for parsing day an clock but return segmentation fault Thanx

while( fscanf(fp,"%s",word) != EOF){
    if (strstr(word,"Athens") != NULL)
        strcpy(p,word);
    }
4
Maslahat: agar fscanf o'rniga fgets dan foydalaning. Bu sizga ko'p muammolarni xalos etadi. Va iltimos, Minimal, to'liq va tasdiqlanadigan misol ni ko'rsating.
qo'shib qo'ydi muallif Jabberwocky, manba
Maslahat: agar fscanf o'rniga fgets dan foydalaning. Bu sizga ko'p muammolarni xalos etadi. Va iltimos, Minimal, to'liq va tasdiqlanadigan misol ni ko'rsating.
qo'shib qo'ydi muallif Jabberwocky, manba
P nima? va so'z ? Hozirgi muammolaringiz qanday? Bundan tashqari: fd qanday olinadi?
qo'shib qo'ydi muallif LPs, manba
P nima? va so'z ? Hozirgi muammolaringiz qanday? Bundan tashqari: fd qanday olinadi?
qo'shib qo'ydi muallif LPs, manba
O'zingizning savolingizni tahrirlang: sharhlar yordamida qo'shimcha narsalarni yubormang.
qo'shib qo'ydi muallif LPs, manba
O'zingizning savolingizni tahrirlang: sharhlar yordamida qo'shimcha narsalarni yubormang.
qo'shib qo'ydi muallif LPs, manba
va fayldagi matn
qo'shib qo'ydi muallif mohd faiez, manba
va fayldagi matn
qo'shib qo'ydi muallif mohd faiez, manba

6 javoblar

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;
}
0
qo'shib qo'ydi

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;
}
0
qo'shib qo'ydi

You could use strstr() to get a pointer to the begining of the Athens in your string and then loop through the characters and trimming all the characters between '<' and '>', including themselves and compose a new string. This will leave you with the desired output.

0
qo'shib qo'ydi

You could use strstr() to get a pointer to the begining of the Athens in your string and then loop through the characters and trimming all the characters between '<' and '>', including themselves and compose a new string. This will leave you with the desired output.

0
qo'shib qo'ydi

Ehtimol, bu sizga ba'zi g'oyalarni beradi. Sizning segmentatsiya xatolaringiz, null ko'rsatkichni (p o'zgaruvchisi deb o'ylayman) bo'shatish uchun kichikroq tamponni olib tashlashdan kelib chiqadi. Albatta, agar sizning formati sizning parchadan chetga chiqsa, kod foydasiz bo'ladi. C'de bir oz oldinga o'tib, siz ham eksat kutubxonasiga qarashingiz mumkin. Bu chiziqlarni kichik xml hujjatiga aylantirishni talab qiladi. Aminmanki, HTML uchun C kutubxonalari mavjud, lekin ularni sinab ko'rmadim.

Eng kamida ushbu dasturning natijasi quyidagicha edi: Afina Sun, 08:08

#include 
#include 
#include 

int main()
{       
   char buf[1024];
   FILE *fp = fopen("the-data-file.txt","r");
   if(!fp){ /* error handling */ }

   while(fgets(buf, sizeof(buf), fp)){
      char* city = strstr(buf,"Athens");
      char* td = city ? strstr(city, "<td") : NULL; 
      char* greater_than = td ? strstr(td, ">") : NULL; 
      char* less_than = greater_than ? strstr(greater_than, "<") : NULL; 
      if(less_than){
         while(*city && isalpha(*city)){
            printf("%c", *city++);
         }       
         printf(" ");
         while(++greater_than < less_than){
            printf("%c", *greater_than);
         }       
         printf("\n");
      }       
   }       


   fclose(fp);

} 
0
qo'shib qo'ydi
Oh, aniq izohni unutganman. -G bilan kompilyatsiya qilishni sinab ko'ring va disk raskadrovka qurilmasida ishlayotganini aniqlash uchun qaerda bo'ladi. Omad.
qo'shib qo'ydi muallif John Griffin, manba

Ehtimol, bu sizga ba'zi g'oyalarni beradi. Sizning segmentatsiya xatolaringiz, null ko'rsatkichni (p o'zgaruvchisi deb o'ylayman) bo'shatish uchun kichikroq tamponni olib tashlashdan kelib chiqadi. Albatta, agar sizning formati sizning parchadan chetga chiqsa, kod foydasiz bo'ladi. C'de bir oz oldinga o'tib, siz ham eksat kutubxonasiga qarashingiz mumkin. Bu chiziqlarni kichik xml hujjatiga aylantirishni talab qiladi. Aminmanki, HTML uchun C kutubxonalari mavjud, lekin ularni sinab ko'rmadim.

Eng kamida ushbu dasturning natijasi quyidagicha edi: Afina Sun, 08:08

#include 
#include 
#include 

int main()
{       
   char buf[1024];
   FILE *fp = fopen("the-data-file.txt","r");
   if(!fp){ /* error handling */ }

   while(fgets(buf, sizeof(buf), fp)){
      char* city = strstr(buf,"Athens");
      char* td = city ? strstr(city, "<td") : NULL; 
      char* greater_than = td ? strstr(td, ">") : NULL; 
      char* less_than = greater_than ? strstr(greater_than, "<") : NULL; 
      if(less_than){
         while(*city && isalpha(*city)){
            printf("%c", *city++);
         }       
         printf(" ");
         while(++greater_than < less_than){
            printf("%c", *greater_than);
         }       
         printf("\n");
      }       
   }       


   fclose(fp);

} 
0
qo'shib qo'ydi
Oh, aniq izohni unutganman. -G bilan kompilyatsiya qilishni sinab ko'ring va disk raskadrovka qurilmasida ishlayotganini aniqlash uchun qaerda bo'ladi. Omad.
qo'shib qo'ydi muallif John Griffin, manba