Remove junk characters after loading a file to buffer..(urgent)

[b][red]This message was edited by Ionline at 2003-6-29 4:29:34[/red][/b][hr]
Hi,
I try to load .SQL file into char buffer.But am getting zunk characters
to the end along with EOF.Hence the command fails when its executed..Below is the code for it..
char * TestFile()
{
FILE *inp;
long fsize;
char *parkdat; /* ptr to buffer */

inp = fopen( "D:\MyDownLoads\Sql.sql", "r" );

/* get the file size */
fseek( inp, 0l, SEEK_END );
fsize = ftell( inp );

/* allocate buffer for whole file */
if ( (parkdat = (char*)malloc( fsize )) == NULL )
{
exit( 7 );
}

/* back to byte 0, read whole thing */
fseek( inp, 0l, SEEK_SET );
fread( parkdat, 1, fsize, inp );
fclose( inp );
//printf( "%s", parkdat );
return parkdat;

}
I want to remove all the junk char plus the eof character from the buffer..

Any help is highly appreciated..


Comments

  • : [b][red]This message was edited by Ionline at 2003-6-29 4:29:34[/red][/b][hr]
    : Hi,
    : I try to load .SQL file into char buffer.But am getting zunk characters
    : to the end along with EOF.Hence the command fails when its executed..Below is the code for it..


    Maybe the problem is because you are only allocating enough space for the text in the file, leaving no room for a terminating NULL character? Maybe try malloc()ing fsize + 1, and then dont forget to set the last character to ''
  • [blue]Open the file in binary mode as shown in [red]RED[/red] below. The default is text mode.[/blue]

    : inp = fopen( "D:\MyDownLoads\Sql.sql", "r[red]b[/red]" );
    :
    : /* get the file size */

  • [b][red]This message was edited by Ionline at 2003-6-29 20:21:23[/red][/b][hr]
    : [blue]Open the file in binary mode as shown in [red]RED[/red] below. The default is text mode.[/blue]
    :
    : : inp = fopen( "D:\MyDownLoads\Sql.sql", "r[red]b[/red]" );
    : :
    : : /* get the file size */
    :
    : Thanks all..Opening the file in binary mode and allocating '' works great!!



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories