Silentkiller
Technical User
Hi,
I need some help with storing the Data of Pgm file into Buffer using Strcat;
Here is my source code:
/* fread example: read a complete file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
FILE * pFile;
long lSize;
char * buffer;
char filename[80];
int ch;
int type;
int i;
printf("Please enter the file name : "
scanf("%s", &filename);
pFile = fopen (filename, "r" );
if (pFile==NULL) exit (1);
ch = getc(pFile);
if (ch != 'P')
{
printf("ERROR(1) : Not Valid PGM File type\n"
exit(1);
}
printf("\n The filename is %s", filename);
ch = getc(pFile);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
}
printf("\n The value is %ld\n", type);
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
printf("\n The Filesize = %ld\n ", lSize);
// copy the file into the buffer.
for (i=0; i<lSize; i++)
{
strcat(buffer, filename);
}
/*** the whole file is loaded in the buffer. ***/
// terminate
fclose (pFile);
return 0;
}
your help will be greatly appreciated
I need some help with storing the Data of Pgm file into Buffer using Strcat;
Here is my source code:
/* fread example: read a complete file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
FILE * pFile;
long lSize;
char * buffer;
char filename[80];
int ch;
int type;
int i;
printf("Please enter the file name : "
scanf("%s", &filename);
pFile = fopen (filename, "r" );
if (pFile==NULL) exit (1);
ch = getc(pFile);
if (ch != 'P')
{
printf("ERROR(1) : Not Valid PGM File type\n"
exit(1);
}
printf("\n The filename is %s", filename);
ch = getc(pFile);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
}
printf("\n The value is %ld\n", type);
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
printf("\n The Filesize = %ld\n ", lSize);
// copy the file into the buffer.
for (i=0; i<lSize; i++)
{
strcat(buffer, filename);
}
/*** the whole file is loaded in the buffer. ***/
// terminate
fclose (pFile);
return 0;
}
your help will be greatly appreciated