Replacing fgets Problem solved.

Discussion in 'C Programming' started by FireHead, Apr 14, 2008.

  1. FireHead

    FireHead Guest

    Hello comp.lang.c

    Couple of years ago I created a topic as to how I can use the function
    fgets so that I can read a line of text from a source of data.

    After a long time I have finally got all of my initial questions
    answered accordingly.

    The code below is hereby allowed to be used anybody and modified
    without any legal strings attached... Simple.

    #ifndef _READLINES_
    #define _READLINES_

    #include <stdio>
    //#include <iostream>
    #include <stdlib>
    #include <string>

    extern "C" short readLPostLine(FILE &rSource,char** content,int
    default_length,signed int non_wanted_linefeeds);
    extern "C" short readLine(FILE* pSource,char **content);
    //using namespace std;

    #endif

    short readLPostLine(FILE &rSource,char** content,int
    default_length,signed int non_wanted_linefeeds)
    {
    short ret=EXIT_SUCCESS;
    int return_length;return_length = 0;
    char* currentline;currentline = 0;
    char* returnline;returnline = 0;
    char* duplicate;duplicate = 0;
    signed int offset;offset = 0;
    int temp_length = 0;
    // allocate default sized buffer
    currentline = (char*)malloc(default_length);
    currentline = strcpy(currentline,"");
    do{
    //read the the line first
    if((currentline=fgets(currentline,default_length,&rSource))!=NULL)
    {
    //allocate memory temporarily
    returnline = (char*)malloc(return_length+default_length);
    returnline = strcpy(returnline,"");
    offset = (strlen(currentline) > 0 ?
    currentline[strlen(currentline)-1]: 0 );
    if(offset != non_wanted_linefeeds && offset!=EOF &&offset!
    =NULL)
    {
    temp_length = strlen(returnline);
    if(temp_length == (return_length+default_length) ||
    temp_length == return_length )
    {
    return_length = return_length+default_length;
    duplicate =(char*)malloc(return_length+default_length);
    duplicate = strcpy(duplicate,returnline);
    free(returnline);
    returnline=0;
    return_length = return_length+default_length;
    returnline = (char*)malloc(return_length+default_length);
    returnline = strcpy(returnline,duplicate);
    free(duplicate);
    duplicate=0;
    }

    if(temp_length == 0)
    returnline = strcpy(returnline,currentline);
    else
    returnline = strcat(returnline,currentline);
    }else
    {
    returnline =
    strncat(returnline,currentline,strlen(currentline)-1);
    ret = EXIT_SUCCESS;
    }
    } //Main IF
    else {ret = EXIT_FAILURE;
    }
    }while( ret==EXIT_FAILURE && offset!=NULL && offset!=EOF);

    if(ret==EXIT_SUCCESS && currentline!="")
    {
    //what is the current size?
    if(*content!=NULL && content!=NULL )
    {
    if(strlen(returnline) > strlen(*content) )
    {
    if(*content!=NULL){free(content);}
    if(content!=NULL)free(content);
    *content = (char *)malloc(strlen(returnline)+1);
    }
    }else{
    *content = (char *)malloc(strlen(returnline)+1);
    }

    *content = strcpy(*content,"\0");
    *content = strcpy(*content,returnline);
    *content = strcat(*content,"\0");
    }
    else{
    if(*content!=NULL)free(*content);
    if(content!=NULL)free(content);
    ret = EXIT_FAILURE;
    }

    if(returnline!=NULL) { free(returnline);returnline=0;}
    if(currentline!=NULL){free(currentline); currentline=0;}

    return ret;

    }

    short readLine(FILE* pSource,char **content){
    return readLPostLine(*pSource,content,512,0x0D); /* default UNIX
    length*/
    }

    //-------------------------------------------------------------------------------------------------------------------------//
    int main(int argc,const char* argv[])
    {
    FILE* pSourceFile;
    FILE* pOutputFile;
    const char* ATOMIC_NAME;
    char* ret = 0;

    if(argv[0] != argv[argc-1]) {

    ATOMIC_NAME=argv[argc-1];
    pSourceFile = fopen(ATOMIC_NAME,"rb");
    if(pSourceFile==NULL)
    {
    printf("File not found\n");return EXIT_FAILURE; }
    else
    {
    fseek(pSourceFile,SEEK_SET,SEEK_SET);
    pOutputFile = fopen("Output.txt","wb+");
    fseek(pOutputFile,SEEK_SET,SEEK_SET);

    while(readLine(pSourceFile,&ret)==EXIT_SUCCESS)
    {
    //if(ret[strlen(ret)]=='\n')
    fputs(ret,pOutputFile);
    //printf("%p = ",ret);
    }

    fclose(pOutputFile);
    if(ret!=NULL||*ret==NULL){ free(ret);ret=0;}
    }//ELSE

    fclose(pSourceFile);
    }else printf("Please specify a file name");
    }
    //----------------------------------------------------------------------------------------------------------------------//

    The above code is fully functional.

    Thanks for the help.
    FireHead, Apr 14, 2008
    #1
    1. Advertising

  2. FireHead

    Ian Collins Guest

    FireHead wrote:
    > Hello comp.lang.c
    >
    > Couple of years ago I created a topic as to how I can use the function
    > fgets so that I can read a line of text from a source of data.
    >
    > After a long time I have finally got all of my initial questions
    > answered accordingly.
    >
    > The code below is hereby allowed to be used anybody and modified
    > without any legal strings attached... Simple.
    >
    > #ifndef _READLINES_
    > #define _READLINES_
    >

    Don't use include guards starting with an underscore and a capital
    letter, they are reserved.

    > #include <stdio>
    > //#include <iostream>
    > #include <stdlib>
    > #include <string>
    >

    These are C++ headers.

    > The above code is fully functional.
    >

    It won't compile...

    --
    Ian Collins.
    Ian Collins, Apr 14, 2008
    #2
    1. Advertising

  3. FireHead wrote:
    > Hello comp.lang.c


    Did you mean to post in comp.lang.c++ instead?

    > Couple of years ago I created a topic as to how I can use
    > the function fgets so that I can read a line of text from a
    > source of data.
    >
    > After a long time I have finally got all of my initial questions
    > answered accordingly.
    >
    > The code below is hereby allowed to be used anybody
    > and modified without any legal strings attached... Simple.


    Thanks, but no thanks.

    > #ifndef _READLINES_
    > #define _READLINES_


    That identifier is reserved for the implementation in both C
    and C++.

    > #include <stdio>


    There is no such header in C (or C++.)

    > //#include <iostream>


    <OT>Ironically, this is the most appropriate header.</OT>

    > #include <stdlib>
    > #include <string>


    There are no such headers in C (or C++.)

    > extern "C"...


    Syntax error.

    <snip>
    > The above code is fully functional.


    This is obviously some new meaning of the term I wasn't
    previously aware of.

    --
    Peter
    Peter Nilsson, Apr 14, 2008
    #3
  4. Peter Nilsson wrote:
    > FireHead wrote:
    > > ...
    > > #include <stdlib>
    > > #include <string>

    >
    > There are no such headers in C (or C++.)


    I tell a lie. One of them is a C++ header.

    --
    Peter
    Peter Nilsson, Apr 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newbie fgets problem

    , Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    568
    Old Wolf
    Nov 2, 2004
  2. Emerson

    problem with fgets

    Emerson, Dec 29, 2004, in forum: C Programming
    Replies:
    7
    Views:
    322
  3. FireHead

    Replacing fgets

    FireHead, Sep 17, 2006, in forum: C Programming
    Replies:
    32
    Views:
    1,096
    Barry Schwarz
    Oct 3, 2006
  4. Michael Bacarella
    Replies:
    26
    Views:
    1,341
    harri
    Nov 20, 2007
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    274
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page