www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/22/14:46:28

From: Jim Barlow <Jim_Barlow AT bc DOT sympatico DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: File I/O problem (newbie)
Date: Wed, 22 Jul 1998 11:33:55 -0700
Organization: BCTEL Advanced Communications
Lines: 70
Message-ID: <35B63092.87C8C431@bc.sympatico.ca>
Reply-To: Jim_Barlow AT bc DOT sympatico DOT ca
NNTP-Posting-Host: vcta01m06-156.bctel.ca
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019