Hello,
The C++ pgm below opens a file, reads it and displays what is read. However at the end of the output it also appends some crazy looking characters. Pls correct it for me.
Thanks,
Prads
<code>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main ()
{
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "C:\\t1.txt" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
// terminate
cout<<buffer<<endl; //check this
fclose (pFile);
free (buffer);
getchar();
return 0;
}
Also if I want to move the starting point from the beginning of file offset by 5, if i use the following, will it work....
fseek(pFile,5,SEEK_SET);