What is the proper way of finding an end of file condition while reading
a file ?
How does feof() detects that end of file is reached ?
Does this require support from OS ?
Thanx in advance for any help ... 18 24202
"junky_fell ow" <ju**********@y ahoo.co.in> wrote in message
news:8c******** *************** **@posting.goog le.com... What is the proper way of finding an end of file condition while reading a file ?
How does feof() detects that end of file is reached ?
feof() does not detect that end of file is reached, it reports that end of file
was reached during a previous read operation.
Does this require support from OS ?
if files are implemented with the OS, feof() will rely on the OS reporting such
a condition for read operations.
if files are implemented within the C library, the OS is not an issue.
It is important to notice that feof() is counterintuitiv e:
- Whether a read operation that reaches the end of a file set the condition is
not specified : fgets() reading the last line will probably not set it, while
fscanf() eating trailing white space might.
- If the file is being written by another function (or process where that makes
sense), feof() may return non 0 even after more data becomes available for
reading (clearerr() or fseek() may be needed before reading).
As a rule of thumb, only use feof() to disambiguate end of file from read error
after a read operation fails.
Idioms such as while (!feof(f)) { ... } and do { ... } while (!feof(f)); are
bogus and must be avoided.
--
Chqrlie.
PS: I know about 7.19.6.2 example 3 verse 19. This code snipplet is bogus and
useless. AAMOF most do/while loops are bogus anyway.
junky_fellow wrote: What is the proper way of finding an end of file condition while reading a file ?
How does feof() detects that end of file is reached ? Does this require support from OS ?
You can do it yourself without feof(), by doing
struct stat statstruct;
stat("filename" , &statstruct) ;
statstruct.st_s ize is the number of bytes in the file.
Thanx in advance for any help ...
Using feof() is easier though.
FILE *fp = fopen(...);
while (!feof(fp)) {
... read stuff
}
> Using feof() is easier though. FILE *fp = fopen(...);
while (!feof(fp)) { ... read stuff }
And of course as the previous poster said, you may have a short count or
even an error, so take that into account.
while (!feof(fp)) {
/* Short count ? */
n = fread(buffer, ..., fp);
if (n != whatever) {
if (ferror())
... some kind of error
if (feof())
... end of file
}
}
Johnathan Doe <No-spam-here-johnathan_doe@! !!nospamthanks! !!fastmail.com. au> scribbled the following: junky_fellow wrote: What is the proper way of finding an end of file condition while reading a file ?
How does feof() detects that end of file is reached ? Does this require support from OS ?
You can do it yourself without feof(), by doing
struct stat statstruct;
stat("filename" , &statstruct) ;
statstruct.st_s ize is the number of bytes in the file.
No you can't. There is no struct stat or stat() in the C language.
--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
"Johnathan Doe" <No-spam-here-johnathan_doe@! !!NOSPAMTHANKS! !!fastmail.com. au>
wrote in message
news:41******** *************** @per-qv1-newsreader-01.iinet.net.au ... Using feof() is easier though.
FILE *fp = fopen(...);
while (!feof(fp)) { ... read stuff } And of course as the previous poster said, you may have a short count or even an error, so take that into account.
The previous poster said to avoid this stupid bogus idiom !
while (!feof(fp)) {
/* Short count ? */ n = fread(buffer, ..., fp);
if (n != whatever) { if (ferror()) ... some kind of error if (feof()) ... end of file } }
Instead do this :
for (;;) {
n = fread(buffer, sizeof(type), count, fp);
if (n != count) {
... handle partial read.
if (ferror(f)) {
... handle error and break or return
break;
}
if (feof(f)) {
.. handle end of file and break or return
break;
}
} else {
... handle normal read
}
}
--
Chqrlie.
Joona I Palaste wrote: No you can't. There is no struct stat or stat() in the C language.
Assuming you're working on 99% of OSes out there, yes you can.
Johnathan Doe <No-spam-here-johnathan_doe@! !!nospamthanks! !!fastmail.com. au> scribbled the following: Joona I Palaste wrote: No you can't. There is no struct stat or stat() in the C language.
Assuming you're working on 99% of OSes out there, yes you can.
Here we discuss 100% of the OSes out there.
--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon
Johnathan Doe wrote: Joona I Palaste wrote: No you can't. There is no struct stat or stat() in the C language.
Assuming you're working on 99% of OSes out there, yes you can.
Read the welcome message. This group deals with the language as
defined by the ISO standards, not some imaginary uncontrolled
offshoot. Reference to non-standard features is off-topic. Also
use of feof for any purpose other than to disambiguae a read error
is almost always an error. You would know all this is you had
bothered to read this group for a period before breaking in with
misinformation.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
cuz he is using a file pointer can he seek using fseek to the end of th
file, and use ftell to tell him the filesize.
for example:
i assume cuz he is using fgets, feof...ftell and fseek are defined. o
they should be defined.
Code
-------------------
long filesize( FILE *fp )
{
if (fp==NULL) return 0L;
fseek( fp, 0, SEEK_END );
return ftell(fp);
}
-------------------
-
MarcSmit
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
----------------------------------------------------------------------- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hokieghal99 |
last post by:
import os, string
print " "
setpath = raw_input("Enter the path: ")
def find_replace(setpath):
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),
'rb').read(), 'THIS')
print find
|
by: Peter Hansen |
last post by:
Greetings.
Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I should
be able to write in the command line:
python name of my program / the libary to search and what kind of file it
is example a .pdf file
So if my program name was test.py and the library name was library1 and the
test type i wanted to find was, a .pdf file
I should write python...
|
by: Xah Lee |
last post by:
suppose you want to do find & replace of string of all files in a
directory.
here's the code:
©# -*- coding: utf-8 -*-
©# Python
©
©import os,sys
©
©mydir= '/Users/t/web'
|
by: Pierre-Yves |
last post by:
Hello,
I have to loop recursively in directories to build a tree. Based on the
directory name, I know I can skip some (i.e: the BACKUP ones) to improve the
performances that are currently very bad :
At the moment I use find(&listFolders, $entrypoint)
In the sub "listFolders" I do $File::Find::name if -d && !/^.$/;
It works but but it's terribly slow and since it's used in a CGI, I can't
let the users wait several minutes each times.
|
by: Dan Jones |
last post by:
I'm writing a script to process a directory tree of images.  In each
directory, I need to process each image and generate an HTML file listing
all of the images and links to the subdirectories.
Just about every source I can find on the 'net for processing subdirectories
points you at Find::Find.  However, I'm trying to do something like this:
enter directory
open INDEX, ".\index.html"
print INDEX HTMLheader
| |
by: Tim Eliot |
last post by:
Just wondering if anyone has hit the following issue and how you might
have sorted it out.
I am using the command:
DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True
after setting stDataSource and stFileName to the desired values. Most
of the time it works, but occasionally, typically as code changes are
being made to the module, the following message appears:
|
by: Michael R. Pierotti |
last post by:
Has anyone seen this error before when trying to make the install on a
program.
------ Starting pre-build validation for project 'HafaSMPPInstall' ------
WARNING: Unable to find dependency 'mscorlib' (Signature='B77A5C561934E089'
Version='1.0.5000.0') of assembly 'Devshock.Protocol.SmppClient.DLL'
WARNING: Unable to find dependency 'mscorlib' (Signature='B77A5C561934E089'
Version='1.0.5000.0') of assembly 'System.dll'
WARNING: Unable to...
|
by: Xah Lee |
last post by:
Interactive Find and Replace String Patterns on Multiple Files
Xah Lee, 2006-06
Suppose you need to do find and replace of a string pattern, for all
files in a directory. However, you do not want to replace all of them.
You need to look at it in a case-by-case basis. What can you do?
Answer: emacs.
|
by: peter |
last post by:
Hello all,
I'm looking for an advice.
Example (one block in ascii file):
$------------------------
NAME='ALFA'
CODE='x'
$------------------------
|
by: mouac01 |
last post by:
Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the same length
as Replace string. Here's what I have so far. I spent many hours
googling for sample code but couldn't find much. Thanks...
public static void FindReplace(string OldFile, string NewFile)
{
string sFind = "ABC"; //I probably need...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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: 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: 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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |