473,765 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading a file using fread()

hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?
Nov 14 '05 #1
3 3512
On 23 Feb 2004 11:55:25 -0800, sa*****@yahoo.c om.hk (syntax) wrote:
hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?


It seems that you wouldn't face any problem even if you don't read your
data in "equal chunks". fread() returns the number of items successfully
read; just look at that value to find out how much you actually got.

Depending on the nature of your data, there's probably one "natural" way to
read that data in. If it is an ASCII file you're reading, you'd be better
off using text-based library functions such as fgets.

If it is binary data you're reading, you probably know something about the
structure of that data (so that the natural chunk size will be something
like the sizeof a struct used to contain the data).

No matter what, just check the return values of your input function calls
and you should be all set.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
sa*****@yahoo.c om.hk (syntax) writes:
hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?


If the file size is unknown, you can't do that. Are you actually
asking how to find out the size of a file? If so, that's
addressed in the C FAQ. But keep in mind that, in general,
there's nothing keeping the size of a file from changing
(increasing or decreasing) while you read it.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3
sa*****@yahoo.c om.hk (syntax) wrote:
let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?


Nice conundrum: in what size chunks do you divide a file which is
104917093 bytes large?

IYAM, a better strategy would be to use a natural-sized chunk and be
prepared to handle the final smaller bit. More solid, and,
off-topically, more likely to be efficient.

Richard
Nov 14 '05 #4

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

Similar topics

19
10374
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
2
16237
by: JS | last post by:
Hello all! I have come on to a problem for which I am not able to find a solution searching the web. What I am trying to do is reading a log-file with a size of 1.3 GB. When reading it using fread() or the Visual C specific(?) read() they return that the number of bytes read equals the number of bytes requested, however all of the bytes read turn out to have a value of zero (the file contains other values). When using the exact same...
8
9532
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
4
5984
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read the data in. However, it takes about 20 seconds to complete and I wonder if there is a faster way. For example, I found that I could use 'fread' to read the data into a string that looks like this:
21
6391
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...):
6
5272
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
4595
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I am trying to read in a file and then parse it to assign the value to variables. I have read that fread is more is better for this then fscanf, so I tried the following code with the input file and resulting output:
16
3751
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have two questions.. 1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1, sizeof(struct bmp), 1, fin); i have a Segment violation ??
6
4231
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
0
10007
jinu1996
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...
1
9951
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,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
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...
1
7378
isladogs
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...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.