Re: [Dev-C++] Res: Res: Res: error while using the GetFileSize() funtion
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2006-12-08 17:56:06
|
Actually three lines - you need a close() too.
The C library have two separate sets o file functions. in io.h you have
the declarations for unbuffered I/O using file descriptors. It is common
to write int fd =3D open(...) to show that you are using a file descriptor,
as opposed to the windows code where the data type returned by
CreateFile() is called a handle.
In stdio.h you have the declarations for (normally) buffered FILE-based
I/O.
Note that some of the *stat() functions may retrieve the file size without
opening the file. The run-time library just retrieves the meta-data for
the file, and extracts the size without need to check if you have access
rights to the actual file data.
Are you building for a 32-bit or a 64-bit environment. In a 32-bit
environment, the standard filelength() or ftell() functions can normally
only report the file size of files less than 4GB large since they returns
the size as a long. Some OS have support for alternative functions
(ftell64(), *stat64(), ... with large-file support.
The file <dos.h> is an old include file introduced by some C compilers for
MS-DOS support. You do not want to use these functions. Either go for
functions defined in the ANSI standard, or if writing specifically for
Windows you might consider the Win32 functions.
#include <time.h>
time_t t;
struct tm tm;
t =3D time(NULL);
tm =3D *gmtime(&t);
If your library is reasonably new, you should use the thread-safe version,
where you supply the buffer for the tm struct:
#include <time.h>
time_t t;
struct tm tm;
t =3D time(NULL);
gmtime_r(&t,&tm);
Also, if you format the file name yourself, you should always generate the
numbers with leading zeroes, i.e.
snprintf(fname,sizeof(fname),"file_%04u%02u%02u.bu",tm.tm_year+1900,tm.tm_m=
on+1,tm.tm_mday);
Without leading zero, nov 1 and jan 11 would look the same:
file_2006111.bu
/Per W
On Fri, 8 Dec 2006, KrowniX ()--() wrote:
> Hi,,
>
> Thanks for the help...but i got it another way that if possible you could=
tell me what do you think about it:
>
> hFile=3Dopen(path, O_RDONLY);
> sizeLog=3Dfilelength(hFile);
>
> this function works incredible fine...just two lines solve my problems...=
and until now..every tests that i have made works 100% ....
>
>
> ...and i will do one more question..rs
>
> ...now i need the time and date info to make the name of the backup file =
that my program creates...so i use..the code below:
> #include <dos.h>
> #include <stdio.h>
>
> int main(void) {
> struct date d;
>
> getdate(&d);
>
> printf("%d%d%d", d.da_year,d.da_mon,d.da_day);
> return 0;
> }
>
> but the interesting thinhg it=B4s that using dev-c++ this code it won=B4t=
be compiled ...that=B4s the error message:
> main.cpp: In function `int main()':
> main.cpp:6: error: aggregate `date d' has incomplete type and cannot be d=
efined
> main.cpp:8: error: `getdate' undeclared (first use this function)
> main.cpp:8: error: (Each undeclared identifier is reported only once for =
each function it appears in.)
> make.exe: *** [main.o] Error 1
> Execution terminated
>
> ...but when i use the Turbo C/C++ 3.0 ,,,it works just fine..with no code=
modification ..
> well i am here tryng to find an API ..or make this function works but it =
will be good if anyone here could give me a direction. to get de data and t=
ime of the system using the dev-c++ , .
>
> Thanks for the help!!!
>
> Krownix
>
> ----- Mensagem original ----
> De: Per Westermark <pw...@ia...>
> Para: KrowniX ()--() <kr...@ya...>
> Cc: dev...@li...
> Enviadas: Sexta-feira, 8 de Dezembro de 2006 13:21:34
> Assunto: Re: [Dev-C++] Res: Res: error while using the GetFileSize() funt=
ion
>
>
> Hi!
>
> First of all, as you have probably already realized, you can't directly
> mix Win32 functions (they use capitalized names) with c standard-library
> functions (they use lower-case names), so fopen may not be mixed with
> GetFileSize().
>
> Second, OpenFile() is an old compatibility function for 16-bit Windows.
> Use CreateFile() instead. Despite it's name, it is used for opening
> existing files (and sockets, serial ports, ...) too.
>
> Sample code:
>
> #define WIN32_LEAN_AND_MEAN 1
>
> #include <windows.h>
>
> int main() {
> HANDLE f =3D CreateFile(
> "filesize.c",
> FILE_READ_DATA|FILE_READ_ATTRIBUTES,
> FILE_SHARE_READ,
> NULL,
> OPEN_EXISTING,
> 0,
> NULL);
> if (f !=3D INVALID_HANDLE_VALUE) {
> DWORD low,high;
> low =3D GetFileSize(f,&high);
> printf("Low =3D %u High =3D %u\n",low,high);
> CloseHandle(f);
> }
> return 0;
> }
>
> If you want to find a file size using just c-lib functions, you may open
> the file, and do a seek to the end of the file.
>
> FILE *f =3D fopen(...);
> if (f) {
> fseek(f,0,SEEK_END);
> long pos =3D ftell(f);
> fclose(f);
> }
>
> Note, that for 32-bit machine, this only works for files less than 4GB.
>
> You may also use any of the *stat() functions, which retrieves size,
> attributes etc for a file.
>
> #include <sys/types.h>
> #include <sys/stat.h>
>
> #if defined(WINDOWS)
> #define stat _stat
> #endif
>
> struct stat s;
> if (0=3D=3Dstat("calvin.txt",&s)) {
> printf("Size of file is %u\n",s.st_size);
> }
>
> Note that if you use a M$ compiler, they have decided to use non-standard
> names for the stat functions. I don't know if it is to their way of
> supporting Unicode, or if it is an attempt to make people avoiding them,
> or if they want to make sure that Windows programs should not be too easy
> to port to Unix and the reverse.
>
> One important thing to do: Always check return values. You get a file siz=
e
> of 4095 MB in your original code because the function returns -1, which
> for a 32-bit processir is normally the same as 0xffffffff. Your division
> then represents 20 shifts (1024*1024) to the right, and what remains is
> 0xfff, or 4095.
>
> Most file functions - and wuite a number of other too - may fail because
> of programming errors, lack of resources, lack of access rights etc.
>
> /Per W
>
> On Thu, 7 Dec 2006, KrowniX ()--() wrote:
>
> > Well ...the OpenFile() doesn=B4t work for me....why?
> > Becouse the OpenFile() function returns a HFILE, and GetFileSize() need=
s a HANDLE, well I solve this problem using the FindFirstFile() function an=
d that=B4s ok..but...the program give me the same results when I was using =
the FILE *
> >
> > Does anyone know anything about it..or could just tell me what C Ansi f=
unction could solve my problem by giving me the size of the file ?????????
> >
> > Krownix
> >
> >
> > ----- Mensagem original ----
> > De: KrowniX ()--() <kr...@ya...>
> > Para: dev...@li...
> > Enviadas: Quinta-feira, 7 de Dezembro de 2006 15:17:03
> > Assunto: [Dev-C++] Res: error while using the GetFileSize() funtion
> >
> >
> > Hi,
> >
> > I think i got it;;
> > The problem is becouse GetFileSize() works with a handle and I was usin=
g a FILE * ...so I think that if I use the OpenFile() function I can solve=
my problem...
> >
> > But;...until now I haven=B4t found an good example on how to use OpenFi=
le() so..if anyone knows how...could give me a help....ok!?!
> >
> > Thanks
> >
> > Krownix
> >
> >
> > ----- Mensagem original ----
> > De: KrowniX ()--() <kr...@ya...>
> > Para: dev...@li...
> > Enviadas: Quinta-feira, 7 de Dezembro de 2006 12:43:15
> > Assunto: [Dev-C++] error while using the GetFileSize() funtion
> >
> >
> > Hello..
> >
> > I got a new situation here ...now I have to verify every 5 minuts the s=
ize of a file, but I am using the GetFileSize() function but it=B4s result =
it isn=B4t like de DIR command results, ex:
> >
> > My code:
> > void TestSizeLog() {
> > FILE *fp; //ponteiro para o arquivo criado
> > DWORD sizeLog; //armazena arquivo
> >
> > if(!(fp=3Dfopen("c:\\arq.dat", "r"))) {
> > std::cout << "Erro ao abrir arquivo de log" << std::endl;
> > exit(1);
> > }
> > sizeLog=3DGetFileSize(fp, NULL);
> > sizeLog/=3D(1024*1024); //transforma bytes em megabytes
> > std::cout << "File Size: " << sizeLog << std::endl;
> > }
> >
> > the result of my code:
> > C:\>"\Documents and Settings\p550826\Desktop\progs\EventViewer\testFile=
=2Eexe"
> > File Size: 4095
> >
> > the result of a DIR command:
> > 07/12/2006 10:13 33.166.874 arq.dat
> > 1 arquivo(s) 33.166.874 bytes
> >
> > So.....where is my error??
> > Does anyone has any idea??
> >
> > Thanks
> > Krownix
> >
> >
> >
> > Yahoo! Search
> > M=FAsica para ver e ouvir: You're Beautiful, do James Blunt
> > -----------------------------------------------------------------------=
--
> > Take Surveys. Earn Cash. Influence the Future of IT
> > Join SourceForge.net's Techsay panel and you'll get the chance to share=
your
> > opinions on IT & business topics through brief surveys - and earn cash
> > http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=
=3DDEVDEV
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> >
> >
> >
> >
> > Voc=EA quer respostas para suas perguntas? Ou voc=EA sabe muito e quer =
compartilhar seu conhecimento? Experimente o Yahoo! Respostas!
> > -----------------------------------------------------------------------=
--
> > Take Surveys. Earn Cash. Influence the Future of IT
> > Join SourceForge.net's Techsay panel and you'll get the chance to share=
your
> > opinions on IT & business topics through brief surveys - and earn cash
> > http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=
=3DDEVDEV
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> >
> >
> > _______________________________________________________
> > Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celul=
ar. Registre seu aparelho agora!
> > http://br.mobile.yahoo.com/mailalertas/
> >
> >
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share y=
our
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3D=
DEVDEV
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
> _______________________________________________________
> Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular=
=2E Registre seu aparelho agora!
> http://br.mobile.yahoo.com/mailalertas/
>
>
|