473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

reading the buffer in chunks

Hi,
I have a variable declared as the following:

char * buf;

I then get a size of a file and allocate memory and point the buf to
it. Here is where I am having a problem. I want to know how I can read
the buf in chunks... so here is a description of what i am looking
for.:

while the end of the buffer hasn't been reached{
send 100 characters of the buf to some function
then send the next 100 ...
}repeat this until the end of the buf

I was originally trying to do:
while (!feof(filename)){
read(filename, buf, 100,0);
process(buf);
}

but for some reason the look never ended. I work with both binary and
ascii files so I am not sure if that has anything to do with.

Any suggestions for the buffer problem??

Thanks

J

Feb 1 '07 #1
5 6266
Your pseudocode looks fine, at least. Could you post some of your real
code?

This is how I would do it in C:

FILE* fp = fopen("file.txt","rt");

while (!feof(fp)) {
char* buffer = new char[100];
size_t count = fread(buffer,sizeof(char),100,fp);

//if count < 100, then the end of file was reached, for sure.
process(buffer);
delete buffer;
}

It could be that the order of your parameters are wrong. Another
technique is to see if the file pointer has reached the end of the
file using ftell() and comparing that to the calculated file size:

fseek(fp,0,SEEK_END);
file_size = ftell(fp);
//then rewind to go back to the beginning of the file
rewind(fp);
Cheers,
Henry

--------
http://hamath.blogspot.com - Science, technology, and interesting
stuff Blog

Feb 1 '07 #2
In article <11*********************@q2g2000cwa.googlegroups.c om>,
ac*******@hotmail.com says...
Hi,
I have a variable declared as the following:

char * buf;

I then get a size of a file and allocate memory and point the buf to
it. Here is where I am having a problem. I want to know how I can read
the buf in chunks... so here is a description of what i am looking
for.:
If you're going to read chunks of fixed size, you might as well define
buf as an array so you don't have to allocate and free the memory
manually.
while the end of the buffer hasn't been reached{
send 100 characters of the buf to some function
then send the next 100 ...
}repeat this until the end of the buf

I was originally trying to do:
while (!feof(filename)){
A loop like 'while (!feof...' is almost always a mistake -- this should
be covered in the FAQ. Your use of "filename" implies that you're
passing the name of the file. When you're working with a file, you
almost always need to open the file, then work with the opened file
instead of the file name. You can use either an ifstream or use fopen to
open the file (it returns a FILE *).
read(filename, buf, 100,0);
process(buf);
This part looks perfectly reasonable.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 1 '07 #3
On Feb 1, 5:03 am, "gamedia...@gmail.com" <gamedia...@gmail.com>
wrote:
Your pseudocode looks fine, at least. Could you post some of your real
code?

This is how I would do it in C:

FILE* fp = fopen("file.txt","rt");

while (!feof(fp)) {
char* buffer = new char[100];
size_t count = fread(buffer,sizeof(char),100,fp);

//if count < 100, then the end of file was reached, for sure.
process(buffer);
You might want to change this to 'process(buffer, count);' to make
sure you don't process invalid data. While the buffer will always be
of the same size it might only be the first byte that is valid, the
rest is junk left from the last read.
delete buffer;

}
--
Erik Wikström

Feb 1 '07 #4
Jerry Coffin schrieb:
In article <11*********************@q2g2000cwa.googlegroups.c om>,
ac*******@hotmail.com says...
[...]
>while the end of the buffer hasn't been reached{
send 100 characters of the buf to some function
then send the next 100 ...
}repeat this until the end of the buf

I was originally trying to do:
while (!feof(filename)){

A loop like 'while (!feof...' is almost always a mistake -- this should
be covered in the FAQ.
It is:
http://www.parashift.com/c++-faq-lit....html#faq-15.5

while (!std::cin.eof()) // wrong way
{
std::cin >x;
// Work with x ...
}

The problem is, that the eof state is set only after trying to read past
the end of file.

I guess it's the same with the C functions.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Feb 1 '07 #5
Thomas J. Gritzan wrote:
Jerry Coffin schrieb:
In article <11*********************@q2g2000cwa.googlegroups.c om>,
ac*******@hotmail.com says...
[...]
while the end of the buffer hasn't been reached{
send 100 characters of the buf to some function
then send the next 100 ...
}repeat this until the end of the buf
>
I was originally trying to do:
while (!feof(filename)){
A loop like 'while (!feof...' is almost always a mistake -- this
should be covered in the FAQ.

It is:
http://www.parashift.com/c++-faq-lit....html#faq-15.5

while (!std::cin.eof()) // wrong way
{
std::cin >x;
// Work with x ...
}

The problem is, that the eof state is set only after trying to read
past the end of file.

I guess it's the same with the C functions.
Yes, here's the corresponding C FAQ entry:

<http://c-faq.com/stdio/feof.html>


Brian
Feb 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
6
by: Einar Høst | last post by:
Hi I'm trying to learn a bit about performance, hope someone can help me out I have a text file with 8-bit characters in it. In order to improve performance, I'm using a BinaryReader instead of...
3
by: Brad | last post by:
I'm working on a web app which will display LARGE tiff image files (e.g files 10-20+ mb). Files are hidden from users direct access. For other, smaller image files I have used FileStream to read...
4
by: jesuraj | last post by:
Hi, how can i read input from a data file which contains binary or hex values. I have to use the exact binary data for further processing.only limited number of bits are taken form the file(64...
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
0
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
17
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.