Inputting N number of lines into a buffer

May 23, 2008 at 4:39am
Hello,
I am trying to input 1000 lines from a file at a time into a buffer ,do some processing on it and then refill the buffer ,process again and so on until the full file has been scanned.
Is there an inbuilt function for this or any other way to implement this.

Thanks
May 23, 2008 at 7:19am
You will need to code the functionality yourself but it is not hard.
In pseudo code

Create_Buffer
While not end of file
{
Load 1000 lines into Buffer
Process Buffer
}


Where Load 1000 lines into buffer is itself a loop

Lines_read =0;
while ((Lines_read <1000) && (not end of file))
{
Read Line Into buffer
}
May 23, 2008 at 12:05pm
You can use read() for reading multiple lines of buffer. Try it out.
May 23, 2008 at 12:11pm
What I meant to say is that,

create a structure of single line(or array of chars) of a fixed length and use it with fread() like:

fread((struct buffer *) buffer, sizeof(struct buffer), 100, your_file);

where struct buffer has a single char[_MAX_LINE_LENGTH_] declared in.

You can use directory the character array too, in which case you would have to use char * in the fread() function.

Good luck ;)
May 23, 2008 at 12:38pm
Keep in mind that if you do a block read as satm2008 suggested, you will have to account for the following two possibilities:

1. The last line may not be completely read into the buffer.

2. Newline sequences may vary among: "\n" (unix), "\r\n" (windows), "\r" (mac).
Topic archived. No new replies allowed.