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

Can the current file position indicator ever be greater the actual file size?

Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file? And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks
Nov 13 '05 #1
7 2223
nrk
Andrew wrote:
Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file?
Size of a file is vague (as in there's no such thing in standard C). It
is not possible to advance the file position indicator once the end of
file indicator is set.
And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks


A conforming implementation of fgetc will advance the file position
indicator only if the end of file indicator is not set, and it seems
unlikely that it will modify the underlying stream. Hence, not possible.

-nrk.

Nov 13 '05 #2
an***********@ca.com (Andrew) wrote in message news:<65**************************@posting.google. com>...
Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file? And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks


Sorry, Let me try to re-phrase this with a simple example. The
following code writes the letter A and an additional 10 bytes (1 for
each call to fgetc) to the output file . I am wondering why this
happens and if there is a way I can avoid it. I don't believe fflush
is suppose to be used on this type of stream? Although, I tried
fflush(of) and it worked, but I suspect it would have wiped out any
previous information which the file contained that I might want to
preserve. Thanks for any advice.

#include <stdio.h>

int main() {

FILE *of;
int i;

if((of = fopen("c:\\problem.txt", "w")) == NULL) {
if((of = fopen("c:\\problem.txt", "r+")) == NULL) {
printf("\nFile Error");
}
}

/* Without a fputc call before the loop the problem does
not exist */

fputc(65,of);

/* tried fflush(of) here but I think this is undefined?
and it seems to clear any previous contents
stored before program execution */

/* 10 can be replaced with any number larger than the
input file size */
for(i = 0; i < 10; i++)
fgetc(of);

fclose(of);

return 0;

}
Nov 13 '05 #3
Andrew wrote:
Sorry, Let me try to re-phrase this with a simple example. The
following code writes the letter A and an additional 10 bytes (1 for
each call to fgetc) to the output file . I am wondering why this
happens and if there is a way I can avoid it. I don't believe fflush
is suppose to be used on this type of stream? Although, I tried
fflush(of) and it worked, but I suspect it would have wiped out any
previous information which the file contained that I might want to
preserve. Thanks for any advice.

#include <stdio.h>

int main() {

FILE *of;
int i;

if((of = fopen("c:\\problem.txt", "w")) == NULL) {
if((of = fopen("c:\\problem.txt", "r+")) == NULL) {
This looks a bit scary, but it basically says "if I can't create it, I want
to open it read-write instead". If that's what you want, then fair enough.
printf("\nFile Error");
}
}

/* Without a fputc call before the loop the problem does
not exist */

fputc(65,of);
This doesn't write 'A', by the way - it writes whatever character happens to
have the value 65 on the system you're running it on. (Let me guess - 'A'
on your system, right? But on, say, an IBM mainframe, you'd get a different
result.)

fputc('A', of); conveys your intent more clearly /and/ is portable.

/* tried fflush(of) here but I think this is undefined?
No, you're fine with fflush() on streams open for output or update.
and it seems to clear any previous contents
stored before program execution */
That's not fflush()'s fault. That's your "w" mode talking (remember those
fopen() calls above?). Swap it around with your "r+".

/* 10 can be replaced with any number larger than the
input file size */
for(i = 0; i < 10; i++)
fgetc(of);


If the file was successfully created ("w" mode), there's nothing to read, is
there? So fgetc() will return EOF, to indicate that it couldn't fulfil your
request. I note that your program is not checking the return value of
fgetc. If you add a check, you will find that fgetc is returning EOF on
each call.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #4
On 30 Sep 2003 15:47:19 -0700, an***********@ca.com (Andrew) wrote in
comp.lang.c:
an***********@ca.com (Andrew) wrote in message news:<65**************************@posting.google. com>...
Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file? And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks


Sorry, Let me try to re-phrase this with a simple example. The
following code writes the letter A and an additional 10 bytes (1 for
each call to fgetc) to the output file . I am wondering why this
happens and if there is a way I can avoid it. I don't believe fflush
is suppose to be used on this type of stream? Although, I tried
fflush(of) and it worked, but I suspect it would have wiped out any
previous information which the file contained that I might want to
preserve. Thanks for any advice.

#include <stdio.h>

int main() {

FILE *of;
int i;

if((of = fopen("c:\\problem.txt", "w")) == NULL) {
if((of = fopen("c:\\problem.txt", "r+")) == NULL) {
printf("\nFile Error");
}
}

/* Without a fputc call before the loop the problem does
not exist */

fputc(65,of);

/* tried fflush(of) here but I think this is undefined?
and it seems to clear any previous contents
stored before program execution */

/* 10 can be replaced with any number larger than the
input file size */
for(i = 0; i < 10; i++)
fgetc(of);

fclose(of);

return 0;

}


You are breaking a rule of the C library, and so you are causing
undefined behavior:

"When a file is opened with update mode ('+' as the second or third
character in the above list of mode argument values), both input and
output may be performed on the associated stream. However, output
shall not be directly followed by input without an intervening call to
the fflush function or to a file positioning function (fseek,
fsetpos, or rewind), and input shall not be directly followed by
output without an intervening call to a file positioning function,
unless the input operation encounters endof-file."

When you use fputc() and then use fgetc() without calling a
file-seeking function in between, you are violating a "shall" clause
outside of a constraint section. The result is undefined behavior
which can be anything, as far as C is concerned, including putting
things in your file that you do not want.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #5
On 30 Sep 2003 15:47:19 -0700, an***********@ca.com (Andrew) wrote in
comp.lang.c:
an***********@ca.com (Andrew) wrote in message news:<65**************************@posting.google. com>...
Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file? And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks


Sorry, Let me try to re-phrase this with a simple example. The
following code writes the letter A and an additional 10 bytes (1 for
each call to fgetc) to the output file . I am wondering why this
happens and if there is a way I can avoid it. I don't believe fflush
is suppose to be used on this type of stream? Although, I tried
fflush(of) and it worked, but I suspect it would have wiped out any
previous information which the file contained that I might want to
preserve. Thanks for any advice.

#include <stdio.h>

int main() {

FILE *of;
int i;

if((of = fopen("c:\\problem.txt", "w")) == NULL) {
if((of = fopen("c:\\problem.txt", "r+")) == NULL) {
printf("\nFile Error");
}
}

/* Without a fputc call before the loop the problem does
not exist */

fputc(65,of);

/* tried fflush(of) here but I think this is undefined?
and it seems to clear any previous contents
stored before program execution */

/* 10 can be replaced with any number larger than the
input file size */
for(i = 0; i < 10; i++)
fgetc(of);

fclose(of);

return 0;

}


You are breaking a rule of the C library, and so you are causing
undefined behavior:

"When a file is opened with update mode ('+' as the second or third
character in the above list of mode argument values), both input and
output may be performed on the associated stream. However, output
shall not be directly followed by input without an intervening call to
the fflush function or to a file positioning function (fseek,
fsetpos, or rewind), and input shall not be directly followed by
output without an intervening call to a file positioning function,
unless the input operation encounters endof-file."

When you use fputc() and then use fgetc() without calling a
file-seeking function in between, you are violating a "shall" clause
outside of a constraint section. The result is undefined behavior
which can be anything, as far as C is concerned, including putting
things in your file that you do not want.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
an***********@ca.com (Andrew) wrote in message news:<65**************************@posting.google. com>...
Hello,

Is it possible for a file position indicator of an input stream to be
greater than the size of a file? And if so could this cause fgetc to
somehow write to the file? For example is it possible to continually
call fgetc past the size of the file and cause some type of undefined
behavior? Thanks


If the end-of-file indicator for the input stream pointed to by stream
is not set and a next byte is present, the fgetc() function shall
obtain the next byte as an unsigned char converted to an int, from the
input stream pointed to by stream, and advance the associated file
position indicator for the stream (if defined). Since fgetc() operates
on bytes, reading a character consisting of multiple bytes (or "a
multi-byte character") may require multiple calls to fgetc().
Nov 13 '05 #7
Thanks everyone for your advice. I was not aware of the consequences
involved with omitting a file seeking call in between these types of
file operations. I apologize for posting a question that could have
easily found out from further reading, but I think I had been looking
in the wrong direction for the root of the problem. This advice along
with clarification on the open file mode I was using helped alot.
Thanks again.
Nov 13 '05 #8

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

Similar topics

8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
35
by: munish.nr | last post by:
Hi All, I want to know the size of file (txt,img or any other file). i knoe only file name. how i can acheive this. does anybody is having idea about that. plz help. rgrds, Munish Nayyar
26
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
11
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in...
14
by: googler | last post by:
Is there any C library function that returns the size of a given file? Otherwise, is there a way in which file size can be determined in a C program? I need to get this for both Linux and Windows...
3
by: J055 | last post by:
Hi How do I tell the user he has tried to upload a file which is too big... 1. when the httpRuntime.maxRequestLength has been exceeded and 2. when the uploaded file is under then...
7
by: asvini | last post by:
Hi I want to read a file which has data and null characters in between... But my program terminates on encountering NULL characters...is there any other way to read the whole file with the NULL...
3
by: nospam | last post by:
I am trying to monitor the process of a file being copied, but I cannot find a function that will return the actual file size, not the total file size, as reported by fileinfo.length. Has anyone...
31
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I modify the current browser window?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.