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 5 6344
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,si zeof(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
In article <11************ *********@q2g20 00cwa.googlegro ups.com>, ac*******@hotma il.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.
On Feb 1, 5:03 am, "gamedia...@gma il.com" <gamedia...@gma il.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,si zeof(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
Jerry Coffin schrieb:
In article <11************ *********@q2g20 00cwa.googlegro ups.com>, ac*******@hotma il.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
Thomas J. Gritzan wrote:
Jerry Coffin schrieb:
In article <11************ *********@q2g20 00cwa.googlegro ups.com>, ac*******@hotma il.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 -- the character
input is a different matter, at least if I want to remain within
the bounds of the standard library.
|
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) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.
|
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 a StreamReader. I've made two versions of my method, one which uses typesafe code, and one which uses unsafe code with pointers. I've read several places that direct pointer access will eliminate bounds-checking when accessing an array, and would...
|
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 in a file in a single
Read and so my quesitons are:
(1) What is a practical file size limit for reading using FileStream.Read
(reading the file in a single read)...especially on a web server where I
don't think I'd want to tax memory...
|
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 bits) for current
processing.Once the processing is done the next 64 bits must be
applied.Can this be done?
| |
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 this (it's only a test trying to achieve the goal...):
|
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 incoming string looking for the vbCrLf
indicator but it too gave me errors as the length of the stream was
not always 24 bits for some reason.
I have to slow the output down to reduce the amount of garbled data
being sent to the serial port.
|
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?
Is allocating more size to buffer and copy data that exceeds 32000 bytes at
tail the only way
or are there any other possibilities?
|
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>
#include <string>
using namespace std;
int main () {
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |