#define specialstr "anystring"
#define _1M 1024*1024
char filedata[2*_1M];
void main(void,void)
{
FILE * fp;
char * spstrpos;
fp=fopen("path:file","rb");
fread(filedata,2*_1M,1,fp);
spstrpos=strstr(filedata,specialstr);
}
Any problem in it?
Thanks in advance!
----------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _1M 1024*1024
#define INSERT_POS (_1M + 32768)
char filedata[2*_1M];
const char filename[] = "bigtemp.tmp";
const char specialstr[] = "anystring";
int main()
{
FILE *fp;
char *spstrpos;
int i;
printf("Creating file ...");
fp = fopen(filename, "wb");
if (!fp)
{
printf("Could not open file\n");
return 0;
}
for (i = 0; i < (_1M + _1M); ++i)
{
if (i == INSERT_POS)
{
printf("\nInserting string at position %d ...", INSERT_POS);
fputs(specialstr, fp);
}
else
{
fputc((rand() % 27) + 'A', fp);
}
}
printf("\n");
fclose(fp);
fp = fopen(filename, "rb");
if (!fp)
{
printf("Could not open file\n");
return 0;
}
printf("\nReading file ...");
fread(filedata, sizeof(filedata), 1, fp);
spstrpos = strstr(filedata, specialstr);
if (!spstrpos)
{
printf("\nString not found\n");
}
else
{
printf("\nString found at position %d\n", (spstrpos - filedata));
}
fclose(fp);
return 0;
}
----------------------------------
. Ed
> Lincon Xue wrote in message
> news:3fc6ae6e$1...@newsgroups.borland.com...
You should note that strstr() will not search past NULL
characters so if the file does has NULL characters you'll run
into a problem. Additionally, you should ensure that the file
in memory is NULL terminated to prevent a potential infinate
loop.
~ JD
Lincon Xue wrote:
> #define specialstr "anystring"
> #define _1M 1024*1024
> char filedata[2*_1M];
You should not use "_1M". Everything beginning with an underscore "_" is
reserved for C++. However a trailing underscore is ok, like "MB_".
#define MBYTE 1024*1024
Frank