Re: using fread
From: Billy Patton (bpatton_at_ti.com)
Date: 05/09/05
- Next message: Chuck Dillon: "Re: Assigning privs to a program?"
- Previous message: Ralf Fassel: "Re: using fread"
- In reply to: Ralf Fassel: "Re: using fread"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 9 May 2005 14:57:17 -0500
On Mon, 9 May 2005, Ralf Fassel wrote:
> * Billy Patton <bpatton@ti.com>
> | I'm trying to parse a prioriety ascii file.
> | I want to read it in chunks instead of a char at a time to allow me to
> | lookahead to tokenize the words in the input file.
>
> You're working too hard on this. I'd suggest to get rid of those
> gazillions of global variables, and work with a local buffer instead:
>
> const size_t buflen = 1024;
> char buffer[buflen];
> while (true) {
> // read one less than the buffer length, so we can
> // zero-terminate it:
> size_t read = fread(buffer,1,buflen-1,fp);
> if (!read) break; // nothing more to read
> // make sure buffer is zero-terminated if you want to use
> // strlen() on it
> buffer[read] = '\0';
> // invoke parser with the fresh stuff in 'buffer' of length 'read'
> parse_buffer(buffer, read);
> }
>
> In parse_buffer(), do all the necessary copying, probably using a
> std::string instead of memcpy(), since you're using C++ anyway.
>
> R'
>
Well, I finallly got it:
static void read_more(void)
{
int len = bufend - bptr;
if (len) memcpy(buffer,bptr,len);
bptr = buffer;
long read = fread(buffer+len,1,BUFSIZE-len,fp);
buflen = len + read;
bufend = buffer + buflen;
}
Now if someone would be so kind as to tell my how I can use mmap I would be
very appreciative.
This program will be used to parse multi gig files. It would probably be much
faster if I could use mmap and not read anything into memory.
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, b-patton@ti.com
- Next message: Chuck Dillon: "Re: Assigning privs to a program?"
- Previous message: Ralf Fassel: "Re: using fread"
- In reply to: Ralf Fassel: "Re: using fread"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|