Replacing fgets Problem solved.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FireHead

    Replacing fgets Problem solved.

    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(F ILE &rSource,cha r** content,int
    default_length, signed int non_wanted_line feeds);
    extern "C" short readLine(FILE* pSource,char **content);
    //using namespace std;

    #endif

    short readLPostLine(F ILE &rSource,cha r** content,int
    default_length, signed int non_wanted_line feeds)
    {
    short ret=EXIT_SUCCES S;
    int return_length;r eturn_length = 0;
    char* currentline;cur rentline = 0;
    char* returnline;retu rnline = 0;
    char* duplicate;dupli cate = 0;
    signed int offset;offset = 0;
    int temp_length = 0;
    // allocate default sized buffer
    currentline = (char*)malloc(d efault_length);
    currentline = strcpy(currentl ine,"");
    do{
    //read the the line first
    if((currentline =fgets(currentl ine,default_len gth,&rSource))! =NULL)
    {
    //allocate memory temporarily
    returnline = (char*)malloc(r eturn_length+de fault_length);
    returnline = strcpy(returnli ne,"");
    offset = (strlen(current line) 0 ?
    currentline[strlen(currentl ine)-1]: 0 );
    if(offset != non_wanted_line feeds && offset!=EOF &&offset!
    =NULL)
    {
    temp_length = strlen(returnli ne);
    if(temp_length == (return_length+ default_length) ||
    temp_length == return_length )
    {
    return_length = return_length+d efault_length;
    duplicate =(char*)malloc( return_length+d efault_length);
    duplicate = strcpy(duplicat e,returnline);
    free(returnline );
    returnline=0;
    return_length = return_length+d efault_length;
    returnline = (char*)malloc(r eturn_length+de fault_length);
    returnline = strcpy(returnli ne,duplicate);
    free(duplicate) ;
    duplicate=0;
    }

    if(temp_length == 0)
    returnline = strcpy(returnli ne,currentline) ;
    else
    returnline = strcat(returnli ne,currentline) ;
    }else
    {
    returnline =
    strncat(returnl ine,currentline ,strlen(current line)-1);
    ret = EXIT_SUCCESS;
    }
    } //Main IF
    else {ret = EXIT_FAILURE;
    }
    }while( ret==EXIT_FAILU RE && offset!=NULL && offset!=EOF);

    if(ret==EXIT_SU CCESS && currentline!="" )
    {
    //what is the current size?
    if(*content!=NU LL && content!=NULL )
    {
    if(strlen(retur nline) strlen(*content ) )
    {
    if(*content!=NU LL){free(conten t);}
    if(content!=NUL L)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!=NU LL)free(*conten t);
    if(content!=NUL L)free(content) ;
    ret = EXIT_FAILURE;
    }

    if(returnline!= NULL) { free(returnline );returnline=0; }
    if(currentline! =NULL){free(cur rentline); 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=arg v[argc-1];
    pSourceFile = fopen(ATOMIC_NA ME,"rb");
    if(pSourceFile= =NULL)
    {
    printf("File not found\n");retur n EXIT_FAILURE; }
    else
    {
    fseek(pSourceFi le,SEEK_SET,SEE K_SET);
    pOutputFile = fopen("Output.t xt","wb+");
    fseek(pOutputFi le,SEEK_SET,SEE K_SET);

    while(readLine( pSourceFile,&re t)==EXIT_SUCCES S)
    {
    //if(ret[strlen(ret)]=='\n')
    fputs(ret,pOutp utFile);
    //printf("%p = ",ret);
    }

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

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

    The above code is fully functional.

    Thanks for the help.
  • Ian Collins

    #2
    Re: Replacing fgets Problem solved.

    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.

    Comment

    • Peter Nilsson

      #3
      Re: Replacing fgets Problem solved.

      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>Ironicall y, 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

      Comment

      • Peter Nilsson

        #4
        Re: Replacing fgets Problem solved.

        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

        Comment

        Working...