473,387 Members | 1,512 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,387 software developers and data experts.

Is this a correct way to use ferror() ?

/* ... */
f = fopen("some-file" , "r") ;
if ( f == NULL ) {
/* Exits */
}
clearerr(f) ;
while ( ( a=getc(f) ) != EOF ) {
/* Do stuff */
}
if ( ferror(f) ) {
/* Exits */
}

Is the call to clearerr() necessary ? If ferror() returns 0
can one be reasonably certain that the whole file has been
read ?
Nov 19 '07 #1
1 1348
In article
<e9**********************************@b32g2000hsa. googlegroups.com>,
Spiros Bousbouras <sp****@gmail.comwrote on Monday 19 Nov 2007 8:52
pm:
/* ... */
f = fopen("some-file" , "r") ;
if ( f == NULL ) {
/* Exits */
}
clearerr(f) ;
while ( ( a=getc(f) ) != EOF ) {
/* Do stuff */
}
if ( ferror(f) ) {
/* Exits */
}

Is the call to clearerr() necessary ?
I don't believe so.
If ferror() returns 0 can one be reasonably certain that the whole
file has been read ?
Within the constraints of Standard C, I think that might be a reasonable
conclusion.

I generally test explicitly with feof() as well, even if ferror()
returns false. Just a habit.

Nov 19 '07 #2

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

Similar topics

1
by: Mark | last post by:
hello! in the interests of performing robust error checking, i'm trying to write file code along the following lines: $file = @fopen( ... ); if ($file === NULL) { // show error mesage here....
1
by: Richard Golebiowski | last post by:
I have been trying to figure this out for quite some time and cannot find any examples in VB.Net or in VB that work correctly. I am working on an application where I want the user to be able to...
36
by: Stephen Howe | last post by:
Hi, If I attempt to read past the end of a file, feof() will return a non-zero value. But can I guarantee that ferror() is 0? In short, will the error indicator be set in some implementations...
2
by: thisis | last post by:
Hi All, I need the PUBS.mdb for pulling images: PUBS.mdb must have the table: pub_info tbl_pub_info : has 3 fields Data_Type : ok Data_Type : ok
8
by: rCs | last post by:
Which of the following two approaches is preferred (and why)? int c; do { ... c = getchar(); } while (c != EOF); - or -
11
by: Victor Lagerkvist | last post by:
Greetings, I know it's usually a bad habit to include several questions in a single message, but they are quite related. I have declared and defined a function named readin, whose purpose is to...
3
lee123
by: lee123 | last post by:
I have a problem getting the correct to count +1 every time I get an answer right and the incorrect is the same. I have two lbl's named number1 and number2 which produces a Rnd# in each lbl. ...
10
by: onetruelove | last post by:
I want to creat a post like this blog: http://onlinetoefltest.blogspot.com/2007/08/level-c-lesson-1.html When you chose all the answers and click show answer a msg box will appear and tells how...
8
by: subramanian100in | last post by:
int ferror(FILE *stream) The function ferror tests the error indicator for the stream pointed to by stream, returning non-zero if it is set. Under what circumstances error indicator will be set....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.