473,320 Members | 2,029 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,320 software developers and data experts.

printf and stat problem

#include <sys/types.h>
#include <sys/stat.h>
#include "ctype.h"
#include <stdio.h>
int estimateLen(struct stat buf, FILE *fp)
{

size_t _size = buf.st_size / 4;
printf("%d %d\n",buf.st_size,_size); //result is wrong,_size is zero?
printf("%d %d\n",_size,buf.st_size); //result is correct
}

int main(int argc,char*argv[])
{
FILE *fp;
struct stat s;
if (stat(argv[argc-1],&s))
printf("stat error\n");

estimateLen(s,fp);

}
I don't know _size show zero in the first printf function

Is it a bug?

--
Origin: ¤¤¥¿¤j¾Ç£»GAIS¤ý´Â  gais.twbbs.org 
Author: diadia ±q gais5.cs.ccu.edu.tw µoªí
Nov 14 '05 #1
5 3122
In article <4I********@gais.twbbs.org>,
diadia <di********@gais.twbbs.org> wrote:
#include <sys/types.h>
That's not standard C. :(
size_t _size = buf.st_size / 4;
Variables names starting with underscore followed by a lower-case
character are, if I recall correctly, reserved for the implementation.
_size could potentially be a macro or expression defined in one of
the non-standard header files you included.
printf("%d %d\n",buf.st_size,_size); //result is wrong,_size is zero?
printf("%d %d\n",_size,buf.st_size); //result is correct


If _size is defined as something, just about any result could show up.
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
Nov 14 '05 #2
diadia <di********@gais.twbbs.org> wrote:
#include <sys/types.h>
#include <sys/stat.h>
These are non-standard headers.
#include "ctype.h"
#include <stdio.h> int estimateLen(struct stat buf, FILE *fp)
{
size_t _size = buf.st_size / 4;
Identifiers starting with an underscore are reserved for the implemen-
tation and shouldn't (or may not) be used by normal programs. What
happens if you rename it to e.g. 'size'?
printf("%d %d\n",buf.st_size,_size); //result is wrong,_size is zero?
'size_t' is (normally not an int) but some unsigned integer. Better
cast it to unsigned long and then use "%lu" as the format specifier
(unless you have a C99 compliant compiler and can use "%zu").
printf("%d %d\n",_size,buf.st_size); //result is correct
}
Where's the return statement? You promised to return an int.
int main(int argc,char*argv[])
{
FILE *fp;
struct stat s; if (stat(argv[argc-1],&s))
printf("stat error\n");
Shouldn't you bail out in that situation instead of calling your
function with a structure that's probably uninitialized?
estimateLen(s,fp);
}
And, again, you need to return something here.
I don't know _size show zero in the first printf function
Is it a bug?


On my system exists a stat() function that perhaps has the same
purpose as the one on your system (assuming you're using some
kind of UNIX - there is no stat() function in standard C and on
my system the 'st_size' member of the stat structure is of type
off_t, which may or may not make a difference) and I can't repro-
duce the error you seem to get.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
diadia wrote:
#include <sys/types.h>
#include <sys/stat.h>
In spite of the fact that the above are not standard C (normally a red
flag indicating that you have posted to the wrong newsgroup), they are
not relevant to your problem.
#include "ctype.h"
Note that <ctype.h> is a standard header; using the form above may cause
you a headache.
#include <stdio.h>
int estimateLen(struct stat buf, FILE *fp)
{

size_t _size = buf.st_size / 4;
Do not use identifiers beginning with underscores. In this case you
would survive, but it is best not to do things that require you to
remember which exceptional cases are safe. Until you learn a lot more C
than you know now, treat all identifiers beginning with an underscore as
being in the namespace reserved for the implementation.
printf("%d %d\n",buf.st_size,_size); //result is wrong,_size is zero?
printf("%d %d\n",_size,buf.st_size); //result is correct [... despite your subject line, the non-standard function stat() has
nothing to do with your problem] I don't know _size show zero in the first printf function
Is it a bug?
It is a bug in your program. %d is the specifier for an int. A size_t
is unsigned (int is signed) and is often larger than an int. Unless you
have a C99 implementation of printf, where a specifier for size_t
exists, you want something like the following, listing in decreasing
safety ('Size' replacing '_size' below) printf("%llu\n",(long long unsigned)Size); or printf("%lu\n",(long unsigned)Size); or printf("%u\n",(unsigned)Size); or printf("%d\n",(int)Size);


Using the wrong specifier not only can lead to errors in the
interpretation of a particular variable, but because different sizes
screw up references to subsequent arguments, it can lead to errors in
interpretation of other variables.
Nov 14 '05 #4
# int estimateLen(struct stat buf, FILE *fp)
# {
#
# size_t _size = buf.st_size / 4;
# printf("%d %d\n",buf.st_size,_size); //result is wrong,_size is zero?
# printf("%d %d\n",_size,buf.st_size); //result is correct
# }

%d is used to print int, or values widenned to int, like short and char.
If sizeof(buf.st_size)>sizeof(int) or sizeof(_size)>sizeof(int), you need
another format or cast the argument to int.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You face forward, or you face the possibility of shock and damage.
Nov 14 '05 #5
Je***********@physik.fu-berlin.de wrote:
diadia <di********@gais.twbbs.org> wrote:
int estimateLen(struct stat buf, FILE *fp)
{
size_t _size = buf.st_size / 4;


Identifiers starting with an underscore are reserved for the implemen-
tation and shouldn't (or may not) be used by normal programs.


Not quite. Identifiers starting with an underscore and a lower-case
letter are only reserved at file scope. This is such an identifier, and
it has block scope. This makes this declaration legal; it does not make
it a good idea.
My advise, for safety and ease of use, is to always write your own code
as if the rule Jens gives is true. but to be aware that it is a
simplification.

Richard
Nov 14 '05 #6

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

Similar topics

1
by: Ken Tucker | last post by:
Help! I'm recursing through 10,000 directories where some have as few as 5 files and others have 100's. I'm then performing a few things based on the modify date of the file. Needless to say,...
1
by: Tony | last post by:
Hello All, I'm having difficulty in using the glob function with stat. Here is a simple piece of code – ----------------------------------------------------------- #! /usr/bin/perl -w use...
3
by: Steven T. Hatton | last post by:
I found the following two statements in the file linked below: struct stat st; stat(fileName.c_str(), &st); http://websvn.kde.org/branches/work/kdevelop4-parser/main.cpp?rev=420247&view=markup...
3
by: David Bear | last post by:
I'm trying to use os.chmod and am refered to the stat module. Is there are explanation of: * S_ISUID * S_ISGID * S_ENFMT * S_ISVTX * S_IREAD * S_IWRITE * S_IEXEC
2
by: Michael Glassford | last post by:
The Python 2.5 News at http://www.python.org/download/releases/2.5/NEWS.txt states that Python 2.5 was changed to "Use Win32 API to implement os.stat/fstat. As a result, subsecond timestamps are...
6
by: Hansen | last post by:
Hi Group, I've made a statistic module for our framework, and have put it in a namespace called stat (which resides in a namespace called dao) My problem is that now the compiler thinks that it...
24
by: Joe Salmeri | last post by:
I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some unexpected behavior that appears to be a bug in the os.stat module. My OS is Windows XP SP2 + all updates. I have several...
2
by: Rolf =?UTF-8?B?S3LDvGdlcg==?= | last post by:
Hi I´m about learning C/C++ and after covering the language basics I´m now heading for my first "real" application where I need to use the POSIX stuff for directory operations. Here´s my...
3
by: Deniz Dogan | last post by:
Hello. I have a question regarding the st_mode field of the "stat" struct in sys/stat.h. I'd like to know how to use the S_IRWXU, S_IRWXG and S_IRWXO flags to mask the mode_t value into human...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.