Thread: [Dev-C++] 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-07 18:33:13
|
Well ...the OpenFile() doesn=B4t work for me....why?=0ABecouse the OpenFile= () function returns a HFILE, and GetFileSize() needs a HANDLE, 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 FILE *=0A=0ADoes any= one know anything about it..or could just tell me what C Ansi function coul= d solve my problem by giving me the size of the file ?????????=0A=0AKrownix= =0A=0A=0A----- Mensagem original ----=0ADe: KrowniX ()--() <krownix@yahoo.c= om.br>=0APara: dev...@li...=0AEnviadas: Quinta-feira= , 7 de Dezembro de 2006 15:17:03=0AAssunto: [Dev-C++] Res: error while usin= g the GetFileSize() funtion=0A=0A=0AHi,=0A =0AI think i got it;; =0AThe pro= blem is becouse GetFileSize() works 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 =0ABut;...until now I haven=B4t found an good example on how to use = OpenFile() so..if anyone knows how...could give me a help....ok!?!=0A =0ATh= anks=0A =0AKrownix=0A=0A=0A----- Mensagem original ----=0ADe: KrowniX ()--(= ) <kr...@ya...>=0APara: dev...@li...=0AEnvi= adas: Quinta-feira, 7 de Dezembro de 2006 12:43:15=0AAssunto: [Dev-C++] err= or while using the GetFileSize() funtion=0A=0A=0AHello..=0A =0AI got a new = situation here ...now I have to verify every 5 minuts the size of a file, b= ut I am using the GetFileSize() function but it=B4s result it isn=B4t like = de DIR command results, ex:=0A =0AMy code:=0Avoid TestSizeLog() {=0A FI= LE *fp; //ponteiro para o arquivo criado=0A DWORD sizeLog; //armazena a= rquivo=0A=0A if(!(fp=3Dfopen("c:\\arq.dat", "r"))) {=0A std::c= out << "Erro ao abrir arquivo de log" << std::endl;=0A exit(1);=0A= }=0A sizeLog=3DGetFileSize(fp, NULL); =0A sizeLog/=3D(1024*102= 4); //transforma bytes em megabytes=0A std::cout << "File Size: " << si= zeLog << std::endl;=0A} =0A =0Athe result of my code:=0AC:\>"\Documents = and Settings\p550826\Desktop\progs\EventViewer\testFile.exe"=0AFile Size: 4= 095=0A =0Athe result of a DIR command:=0A07/12/2006 10:13 33.166.87= 4 arq.dat=0A 1 arquivo(s) 33.166.874 bytes=0A =0ASo.....w= here is my error?? =0ADoes anyone has any idea??=0A =0AThanks=0AKrownix=0A= =0A=0A=0AYahoo! Search=0AM=FAsica para ver e ouvir: You're Beautiful, do Ja= mes Blunt =0A--------------------------------------------------------------= -----------=0ATake Surveys. Earn Cash. Influence the Future of IT=0AJoin So= urceForge.net's Techsay panel and you'll get the chance to share your=0Aopi= nions on IT & business topics through brief surveys - and earn cash=0Ahttp:= //www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV= =0A_______________________________________________=0ADev-cpp-users mailing = list=0AD...@li...=0ATO UNSUBSCRIBE: http://www23.b= rinkster.com/noicys/devcpp/ub.htm=0Ahttps://lists.sourceforge.net/lists/lis= tinfo/dev-cpp-users=0A=0A=0A=0A=0A=0AVoc=EA quer respostas para suas pergun= tas? Ou voc=EA sabe muito e quer compartilhar seu conhecimento? Experimente= o Yahoo! Respostas!=0A----------------------------------------------------= ---------------------=0ATake Surveys. Earn Cash. 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 through brief surveys - and earn ca= sh=0Ahttp://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID= =3DDEVDEV=0A_______________________________________________=0ADev-cpp-users= mailing list=0AD...@li...=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 no Yahoo! Mail: receba alertas de no= vas mensagens no seu celular. Registre seu aparelho agora! =0Ahttp://br.mob= ile.yahoo.com/mailalertas/ =0A =0A |
From: Per W. <pw...@ia...> - 2006-12-08 15:22:02
|
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 size 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() needs = a HANDLE, 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 th= e FILE * > > Does anyone know anything about it..or could just tell me what C Ansi fun= ction 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 using = a FILE * ...so I think that if I use the OpenFile() function I can solve m= y problem... > > But;...until now I haven=B4t found an good example on how to use OpenFile= () 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 siz= e 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.e= xe" > 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 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 > > > > > > Voc=EA quer respostas para suas perguntas? Ou voc=EA sabe muito e quer co= mpartilhar 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 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/ > > |