c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file.
and then storing them in to diffrent variables ?

text file like 0:
abc.txt

aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass

==============

:confused:
I have a question as I am unable to follow your thread. Do you want each of the values separated by the pipe symbol "|" to be stored into a separate variable and what do you want to do with them afterwards?

Attached is a basic idea if I'm getting you right. Untested and using GNU format string extensions. YMMV.

This is what i actually need :

eg:

aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass
aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass

my c program should read the first line and
then store
aaaaaaa in some variable x
11111111111 in some variable y and so on .......

program will identify the strings that are written b/w two delimiters
i.e '|'
store in variable x till i reach the next delimiter than to the next delimiter is stored in variable y.

u can use 'strtok' function

Thank u all .....
its done .......:slight_smile:

Hi can u post the program ...so that i can get an idea of ur implementation

thanks

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int
main ()
{
    FILE * f;
    char buf[1024];
    char * saveptr;
    char * token;
    char hash[7][100];
    int offset;

    f = fopen ("list", "r");

    while (fgets (buf, 1024, f))
    {  
        if (strchr(buf, '\n'))
            *strchr(buf, '\n') = '\0';
        for (offset=0, token = buf; offset < 7 ; token = NULL, offset++)
        {  
            token = strtok_r(token, "|", &saveptr);
            if (token == NULL)
                break;
            printf ("%s\n", token);
    //      strncpy(hash[offset], token, 100);
        }
    }

    fclose (f);
}

Here is an example of strtok_r() (thread safe). I commented the strncpy() line where every separated value is stored in "hash" variable. If you wish to store everything in memory, you could use linked lists to link multiple structs which have 'hash' variables.