Posted on 2007-07-25
Last Modified: 2010-04-01
Hi, I need to read text file,
format of file is following,
There is pipe delimeter, there may be data in between pipe or may not.
I have written following code,
if((fp = fopen(temp_filename,"r+")) == NULL)
            printf("Errno = %d\n",errno);
            printf("file cannot be opened. ErrNo = %d \n",errno);
            return -1;
            if(fgets(s_data,100,fp) != NULL)

but this doesnt seems to be working when any data is missing.

How can i handle this.
Please suggest.
Thanks in advance.

Question by:vjabhi
    LVL 53

    Expert Comment

    >> but this doesnt seems to be working when any data is missing.

    Yes, consecutive delimiters are treated as one delimiter by strtok.

    You could write your own strtok function that doesn't do that. Something like :

        char *my_strtok(char *str, char *delimiters) {
            static char *cur = 0;
            char *ret = 0;
            if (str) cur = str;
            if (cur) {
                size_t token_len = strcspn(cur, delimiters);
                ret = cur;
                cur += token_len;
                if (*cur) {
                    *cur++ = '\0';
                else {
                    cur = 0;
            return ret;

    This code is untested, but it should be ok ...
    LVL 53

    Accepted Solution

    >> This code is untested, but it should be ok ...

    just tested it too ;)

    Author Comment

    its working.
    Thank you very much...
    LVL 53

    Expert Comment

    No problem ... glad to be of assistance :)

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now