Re: Replacing fgets



On 2 Oct 2006 00:14:11 -0700, "FireHead"
<satish.athreya@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello All,

My original topic should have been " using/replace fgets".

I think i have finally solved and used fgets in my program.
But I see somewhere bug/memory leak I just am not able pin the memory
leak.
Could anybody assist me in this issue please.

Your code does not compile cleanly. It is riddled with constraint
violations, undefined behavior, and obvious logical errors. In the
spirit of learning to walk before running, I suggest you start with
simpler projects.


The this the code that I have managed to get it working.

If it doesn't compile cleanly, working is a forlorn hope.

//-------------------------------------------------------------------------------------------------------------------------------------------------//
/*
Will BE MULTITHREADED
open a original file (source file)
Look for the existing custom folder in the HOME directory
If the HOME directory does not exist
Then see if the specs exists in the /usr/local/share (Default)
If the /usr/local/share does not exist then create a template
create a temp
*/

#include <cliargs.h>

A non-standard header. We need to see it if we are going to help.

short readPreLine(FILE* pSource,char **content);
short readPostLine(FILE* pSource,char **content);
short readLPostLine(FILE *pSource,char** content,int default_length);
short readLPreLine(FILE *pSource,char** content,int default_length);


char* get_Copy(char* pStr);

char* get_Copy(char* pStr){
u_int8_t *temp =0;
u_int8_t *final =0;
u_int8_t byte = 0;
u_int8_t dummy = 0;
temp = pStr;

Didn't your compiler complain about incompatible pointer types here?
Even if u_int8_t is a typedef for unsigned char, pStr is not a pointer
to this type.

int pos = 0;
final = &dummy;
/*initilize the contents with new address*/
do{
byte = *temp;printf("%c",byte);
final++;

At each iteration of this loop, the pointer final is incremented to
point some number of bytes further beyond the end of dummy.

temp++;
}while(byte!='\0');
*final = '\0';

Since final no longer points to dummy or to any memory you have the
right to write to, this invokes undefined behavior. Since final
points to an integer type, why are you using character notation?


temp = "";

You went to some trouble in the preceding while loop to have temp
point one beyond the '\0' that terminates pStr. You have now lost
that address and replaced it with the address of a string literal.

temp = pStr;

You obviously don't care about the string literal either. temp now
points to the start of the input string.

/*clean up everything*/

char *results = "";

Those of us with C89 compilers can't help you if you define objects
after executable statements. This is a C99 feature.

results = strcpy( final,temp);

This also invokes undefined behavior. Even if you hadn't altered
final in the while loop, it would still not point to an area large
enough to receive a string of length greater than 0.


return results;
}


/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);

Didn't your compiler complain here. The second argument to fgetpos
must have type fpos_t*. Your code has type fpos_t**.

//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);

What is the purpose of the first statement? offset is changed
immediately. offset needs to be an int (see below).

} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&
offset!=0xFF);

offset is a char. NULL could be defined as (void*)0. If so, this is
another constraint violation. As your code stands now, there is no
guarantee that EOF can be represented in a char.


//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);

More undefined behavior. %u is for unsigned int. Your argument has
type fpos_t*.

//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))

buflen is a size_t. There is no guarantee that either EXIT_FAILURE or
EXIT_SUCCESS can be represented as a size_t. Furthermore, you seem to
think these values have some significance in relation to length of a
buffer. They don't; though it might appear that way on your system.

ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);

Another constraint violation.

//allocate memory for the new address register
buffer = (char *)malloc(buflen);

Don't cast the return from malloc. It can't help and could cause the
compiler to suppress a diagnostic you would really want to see.

//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));

More undefined behavior. strlen returns a size_t which will be
unsigned but not necessarily int. %d requires a signed int.


//free the contents
free(buffer);

ret=EXIT_SUCCESS;
}//ELSE

buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;

Why are you bothering to assign to automatic variables that will
disappear after the next statement.

return ret;
}*/


Sorry, there didn't seem to be any point to going beyond these two
functions.


Remove del for email
.



Relevant Pages

  • Re: How to use a C++ class in .NET
    ... > absolutely compiler dependant. ... > public ref class MyClass ... > int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
    (microsoft.public.dotnet.framework)
  • Re: PCI device driver file operations question
    ... through LDD 3rd Ed to see how to make the init function for a char ... unsigned long offset; ... I wrote a PCI device driver and a userspace ... int test_fopsopen{ ...
    (comp.os.linux.development.system)
  • Re: How to use a C++ class in .NET
    ... absolutely compiler dependant. ... public ref class MyClass ... int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
    (microsoft.public.dotnet.framework)
  • Re: querry related to structure padding
    ... char B; ... The compiler is still free to insert padding between B and C, ...
    (comp.lang.c)
  • Re: Error in Passing char pointer
    ... int main ... Always ensure that the compiler sees a prototype for each function before calling it. ... Why is ea unsigned char when everything else is signed char? ... Also your pointer types disagree with the pointers you are passing, unsigned char* and char* are not the same. ...
    (comp.lang.c)