Hi...
getdelim function is posing an "undeclared identifier" problem during execution .so i included #define _GNU_SOURCE even though it is posing the same problem .
i am executing the c++ program in visual studio in windows platform .
how to add GNU extensions in Visual studio ?

Here is the source. Might not be too much trouble to adapt it.

I hav run the code specified in the link.
It is showing error as "ssize_t is an undeclared identifier"
what content shouls v write in "getdelim.h" header file?

Here is the source. Might not be too much trouble to adapt it.

I'm not 100% on either question, so I'd set out on your own for a bit.

FWIW:
ssize_t is going to have to be one of those things you have to iron out. It's a typedef for sure but it most probably varies from system to system. Look on this page where it gives the Windows header in which it is defined (this information may be out of date).

As far as the second question goes, open up some headers and take a look. I would assume you'd want the function prototype and any standard header files your particular function requires. I've never built a library from scratch on Windows.

I ran into this code on a search out of pure curiosity, I just figured I would share it with you.

I hav run the code specified in the link.
It is showing error as "ssize_t is an undeclared identifier"
what content shouls v write in "getdelim.h" header file?

It is misspelled it -- it should be size_t

I just tried to compile that with VC++ 2008 Express and found out that there are several g++ specific functions in it, such as __validfp() and __set_errno(). Also the FILE structure is different, g++'s version of FILE has members that VC++ 2008 Express does not. It would appear to be easier to just write your own function than try to bastardize that one.

Here is a version that compiles with VC++ 2008 Express, and seems to work ok with the file I tested it with (which is the source code of this program). I have no clue what the second parameter to that function is supposed to do, so this program just ignores it.

The function allocates new memory for lineptr every time it is called, which is probably a waste of time. It might be better if the calling function allocated the memory once and passed the pointer to the allocated memory. That would be a great deal more efficient.

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#pragma warning(disable: 4996)

/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
   (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
   NULL), pointing to *N characters of space.  It is realloc'd as
   necessary.  Returns the number of characters read (not including the
   null terminator), or -1 on error or EOF.  */
static __inline void __set_errno(unsigned int no)
{
    errno = no;
}

size_t __getdelim (char** lineptr, size_t* n, int terminator,FILE* stream)
{
    int c;
    size_t len = 0;
    size_t linesize = 0;
    const size_t BLOCKSIZE = 255;
    if( lineptr == NULL || stream == NULL || n == NULL)
    {
        _set_errno(EINVAL);
        return -1;
    }
    linesize = BLOCKSIZE;
    *lineptr = malloc(BLOCKSIZE);
    while( (c = fgetc(stream)) != EOF && c != terminator)
    {
        if( (len+1) == linesize)
        {
            linesize += BLOCKSIZE;
            *lineptr = realloc(*lineptr, linesize);
        }
        (*lineptr)[len++] = c;
    }
    if( len == 0 && c != terminator) // check for blank lines
    {
        _set_errno(EINVAL);
        free(*lineptr);
        *lineptr = NULL;
        return -1;
    }
    (*lineptr)[len] = 0; // null-terminate the string
        
    return len;
}

int main()
{
    char* lineptr = 0;
    unsigned int n = 255;
    FILE* fp = fopen("test3.c", "r");
    if( fp != NULL)
    {
        while(__getdelim(&lineptr, &n, '\n', fp) != -1 )
        {
            printf("%s\n", lineptr);
            free(lineptr);
            lineptr = NULL;
        }
        fclose(fp);
    }

}

Also see the code snippet I just posted in the C forum (afterall its a C program, not C++).

while executing it is showing the follwing error :
error C2440: '=' : cannot convert from 'void *' to 'char *'
in the lines
*lineptr = malloc(BLOCKSIZE);
and
*lineptr = realloc(*lineptr, linesize);

i have fixed the problem by doing..
*lineptr=(char *)mallaoc(BLOCKSIZE);
*lineptr =(char *) realloc(*lineptr, linesize);
but now i am getting nothing displayed in the outputscreen...
i have sent a text document as input to the file

I got the output ...
earlier i wrote
FILE* fp = fopen("C:\Documents and Settings\MyDocuments\doit.txt", "r");
now i wrote...
FILE* fp = fopen("C:\\Documents and Settings\\MyDocuments\\doit.txt", "r");


Thanku Very much i Got the Output..........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.