Mail Archives: djgpp/1998/07/22/14:46:28
Hi,
I'm trying to read a binary file that contains some null-terminated strings.
The program is designed to edit these strings. Here's my code:
int readthefile (char *filename) {
FILE *fp;
char *buffer;
char *offset;
long filesize;
OFF_LIST *cur;
fp = fopen (filename, "rb+"); // binary read/write
if (!fp) {
return F_READERROR;
}
filesize = getfsize (fp);
buffer = offset = (char *)safemalloc (sizeof(char) * filesize);
fread (buffer, sizeof(char), filesize, fp);
cur = &first; // point to the first item in the list
while (offset = (char *)memchr (buffer, (int)"myst", filesize)) // returns
NULL on failure
{
if (strncmp (offset, "mystring", 12)) {
cur = add_to_list (cur, offset, strlen(offset));
}
offset++;
}
if (cur == &first) { // if so, there are no occurences of 'staredit'
return F_NOSTRINGS;
}
}
long getfsize (FILE *fp) {
fseek (fp, 0, SEEK_END);
return ftell (fp);
}
// adds a new item to the list and returns the new item
OFF_LIST *add_to_list (OFF_LIST *item, char *offset, int len) {
OFF_LIST *newlist;
item->next = newlist = (OFF_LIST *)safemalloc (sizeof(OFF_LIST));
newlist->offset = offset;
newlist->maxlen = len;
newlist->next = NULL;
return newlist;
}
For some reason, this function always returns F_NOWAVS even if the file consists
completely of "mystringmystringmystringmysting...." What's wrong here?
Thanks for your help.
--
J Barlow
------------
Junk email will be forwarded to system
administrators.
------------
- Raw text -