[Dev-C++] Res: Res: Res: error while using the GetFileSize() funtion
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: KrowniX \(\)--\(\) <kr...@ya...> - 2006-12-08 17:23:25
|
Hi,,=0A=0AThanks for the help...but i got it another way that if possible y=
ou could tell me what do you think about it:=0A=0A hFile=3Dopen(path, O=
_RDONLY); =0A sizeLog=3Dfilelength(hFile);=0A=0Athis function works inc=
redible fine...just two lines solve my problems...and until now..every test=
s that i have made works 100% ....=0A=0A=0A...and i will do one more questi=
on..rs=0A=0A...now i need the time and date info to make the name of the ba=
ckup file that my program creates...so i use..the code below:=0A#include <d=
os.h>=0A#include <stdio.h>=0A =0Aint main(void) {=0A struct date =
d;=0A =0A getdate(&d);=0A =0A printf("%d%d%d", d.da_year,d.da_m=
on,d.da_day);=0A return 0;=0A}=0A=0Abut the interesting thinhg it=B4s th=
at using dev-c++ this code it won=B4t be compiled ...that=B4s the error mes=
sage:=0Amain.cpp: In function `int main()':=0Amain.cpp:6: error: aggregate =
`date d' has incomplete type and cannot be defined=0Amain.cpp:8: error: `ge=
tdate' undeclared (first use this function)=0Amain.cpp:8: error: (Each unde=
clared identifier is reported only once for each function it appears in.)=
=0Amake.exe: *** [main.o] Error 1=0AExecution terminated=0A=0A...but when i=
use the Turbo C/C++ 3.0 ,,,it works just fine..with no code modification .=
.=0Awell i am here tryng to find an API ..or make this function works but i=
t will be good if anyone here could give me a direction. to get de data and=
time of the system using the dev-c++ , .=0A=0AThanks for the help!!!=0A=0A=
Krownix=0A=0A----- Mensagem original ----=0ADe: Per Westermark <pwm@iapetus=
.neab.net>=0APara: KrowniX ()--() <kr...@ya...>=0ACc: dev-cpp-user=
s...@li...=0AEnviadas: Sexta-feira, 8 de Dezembro de 2006 13:2=
1:34=0AAssunto: Re: [Dev-C++] Res: Res: error while using the GetFileSize()=
funtion=0A=0A=0AHi!=0A=0AFirst of all, as you have probably already realiz=
ed, you can't directly=0Amix Win32 functions (they use capitalized names) w=
ith c standard-library=0Afunctions (they use lower-case names), so fopen ma=
y not be mixed with=0AGetFileSize().=0A=0ASecond, OpenFile() is an old comp=
atibility function for 16-bit Windows.=0AUse CreateFile() instead. Despite =
it's name, it is used for opening=0Aexisting files (and sockets, serial por=
ts, ...) too.=0A=0ASample code:=0A=0A#define WIN32_LEAN_AND_MEAN 1=0A=0A#in=
clude <windows.h>=0A=0Aint main() {=0A HANDLE f =3D CreateFile(=0A =
"filesize.c",=0A FILE_READ_DATA|FILE_READ_ATTRIBUTES,=0A FI=
LE_SHARE_READ,=0A NULL,=0A OPEN_EXISTING,=0A 0,=0A =
NULL);=0A if (f !=3D INVALID_HANDLE_VALUE) {=0A DWORD low,hig=
h;=0A low =3D GetFileSize(f,&high);=0A printf("Low =3D %u Hig=
h =3D %u\n",low,high);=0A CloseHandle(f);=0A }=0A return 0;=0A=
}=0A=0AIf you want to find a file size using just c-lib functions, you may =
open=0Athe file, and do a seek to the end of the file.=0A=0AFILE *f =3D fop=
en(...);=0Aif (f) {=0A fseek(f,0,SEEK_END);=0A long pos =3D ftell(f);=
=0A fclose(f);=0A}=0A=0ANote, that for 32-bit machine, this only works f=
or files less than 4GB.=0A=0AYou may also use any of the *stat() functions,=
which retrieves size,=0Aattributes etc for a file.=0A=0A#include <sys/type=
s.h>=0A#include <sys/stat.h>=0A=0A#if defined(WINDOWS)=0A#define stat _stat=
=0A#endif=0A=0Astruct stat s;=0Aif (0=3D=3Dstat("calvin.txt",&s)) {=0A p=
rintf("Size of file is %u\n",s.st_size);=0A}=0A=0ANote that if you use a M$=
compiler, they have decided to use non-standard=0Anames for the stat funct=
ions. I don't know if it is to their way of=0Asupporting Unicode, or if it =
is an attempt to make people avoiding them,=0Aor if they want to make sure =
that Windows programs should not be too easy=0Ato port to Unix and the reve=
rse.=0A=0AOne important thing to do: Always check return values. You get a =
file size=0Aof 4095 MB in your original code because the function returns -=
1, which=0Afor a 32-bit processir is normally the same as 0xffffffff. Your =
division=0Athen represents 20 shifts (1024*1024) to the right, and what rem=
ains is=0A0xfff, or 4095.=0A=0AMost file functions - and wuite a number of =
other too - may fail because=0Aof programming errors, lack of resources, la=
ck of access rights etc.=0A=0A/Per W=0A=0AOn Thu, 7 Dec 2006, KrowniX ()--(=
) wrote:=0A=0A> Well ...the OpenFile() doesn=B4t work for me....why?=0A> Be=
couse the OpenFile() function returns a HFILE, and GetFileSize() needs a HA=
NDLE, well I solve this problem using the FindFirstFile() function and that=
=B4s ok..but...the program give me the same results when I was using the FI=
LE *=0A>=0A> Does anyone know anything about it..or could just tell me what=
C Ansi function could solve my problem by giving me the size of the file ?=
????????=0A>=0A> Krownix=0A>=0A>=0A> ----- Mensagem original ----=0A> De: K=
rowniX ()--() <kr...@ya...>=0A> Para: dev...@li...=
rge.net=0A> Enviadas: Quinta-feira, 7 de Dezembro de 2006 15:17:03=0A> Assu=
nto: [Dev-C++] Res: error while using the GetFileSize() funtion=0A>=0A>=0A>=
Hi,=0A>=0A> I think i got it;;=0A> The problem is becouse GetFileSize() wo=
rks with a handle and I was using a FILE * ...so I think that if I use the=
OpenFile() function I can solve my problem...=0A>=0A> But;...until now I h=
aven=B4t found an good example on how to use OpenFile() so..if anyone knows=
how...could give me a help....ok!?!=0A>=0A> Thanks=0A>=0A> Krownix=0A>=0A>=
=0A> ----- Mensagem original ----=0A> De: KrowniX ()--() <kr...@ya...=
.br>=0A> Para: dev...@li...=0A> Enviadas: Quinta-fei=
ra, 7 de Dezembro de 2006 12:43:15=0A> Assunto: [Dev-C++] error while using=
the GetFileSize() funtion=0A>=0A>=0A> Hello..=0A>=0A> I got a new situatio=
n here ...now I have to verify every 5 minuts the size of a file, but I am =
using the GetFileSize() function but it=B4s result it isn=B4t like de DIR c=
ommand results, ex:=0A>=0A> My code:=0A> void TestSizeLog() {=0A> FILE=
*fp; //ponteiro para o arquivo criado=0A> DWORD sizeLog; //armazena a=
rquivo=0A>=0A> if(!(fp=3Dfopen("c:\\arq.dat", "r"))) {=0A> s=
td::cout << "Erro ao abrir arquivo de log" << std::endl;=0A> exit=
(1);=0A> }=0A> sizeLog=3DGetFileSize(fp, NULL);=0A> sizeLog/=
=3D(1024*1024); //transforma bytes em megabytes=0A> std::cout << "File=
Size: " << sizeLog << std::endl;=0A> }=0A>=0A> the result of my code:=0A> =
C:\>"\Documents and Settings\p550826\Desktop\progs\EventViewer\testFile.exe=
"=0A> File Size: 4095=0A>=0A> the result of a DIR command:=0A> 07/12/2006 =
10:13 33.166.874 arq.dat=0A> 1 arquivo(s) 33.166.=
874 bytes=0A>=0A> So.....where is my error??=0A> Does anyone has any idea??=
=0A>=0A> Thanks=0A> Krownix=0A>=0A>=0A>=0A> Yahoo! Search=0A> M=FAsica para=
ver e ouvir: You're Beautiful, do James Blunt=0A> ------------------------=
-------------------------------------------------=0A> Take Surveys. Earn Ca=
sh. Influence the Future of IT=0A> Join SourceForge.net's Techsay panel and=
you'll get the chance to share your=0A> opinions on IT & business topics t=
hrough brief surveys - and earn cash=0A> http://www.techsay.com/default.php=
?page=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A> _________________________=
______________________=0A> Dev-cpp-users mailing list=0A> Dev-cpp-users@lis=
ts.sourceforge.net=0A> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/de=
vcpp/ub.htm=0A> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=
=0A>=0A>=0A>=0A>=0A>=0A> Voc=EA quer respostas para suas perguntas? Ou voc=
=EA sabe muito e quer compartilhar seu conhecimento? Experimente o Yahoo! R=
espostas!=0A> -------------------------------------------------------------=
------------=0A> Take Surveys. Earn Cash. Influence the Future of IT=0A> Jo=
in SourceForge.net's Techsay panel and you'll get the chance to share your=
=0A> opinions on IT & business topics through brief surveys - and earn cash=
=0A> http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=
=3DDEVDEV=0A> _______________________________________________=0A> Dev-cpp-u=
sers mailing list=0A> Dev...@li...=0A> TO UNSUBSCRIB=
E: http://www23.brinkster.com/noicys/devcpp/ub.htm=0A> https://lists.source=
forge.net/lists/listinfo/dev-cpp-users=0A>=0A>=0A>=0A> ____________________=
___________________________________=0A> Novidade no Yahoo! Mail: receba ale=
rtas de novas mensagens no seu celular. Registre seu aparelho agora!=0A> ht=
tp://br.mobile.yahoo.com/mailalertas/=0A>=0A>=0A=0A=0A---------------------=
----------------------------------------------------=0ATake Surveys. Earn C=
ash. Influence the Future of IT=0AJoin SourceForge.net's Techsay panel and =
you'll get the chance to share your=0Aopinions on IT & business topics thro=
ugh brief surveys - and earn cash=0Ahttp://www.techsay.com/default.php?page=
=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A________________________________=
_______________=0ADev-cpp-users mailing lis...@li...=
rge.net=0ATO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm=
=0Ahttps://lists.sourceforge.net/lists/listinfo/dev-cpp-users=0A=0A=0A=09=
=09=0A_______________________________________________________ =0ANovidade n=
o Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre s=
eu aparelho agora! =0Ahttp://br.mobile.yahoo.com/mailalertas/ =0A =0A |