2 questions about working with files
-
Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!
-
Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!
1. To get the size of a file, take a look at the
_stat
function, and thest_size
member of the structure it populates. 2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem. 3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable. -
1. To get the size of a file, take a look at the
_stat
function, and thest_size
member of the structure it populates. 2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem. 3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable. -
Hi!, i hope you guys can help me out. 1.- to get the size of a file (i dont use mfc), i use something like: HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL); if(hfile) { DWORD fsize=GetFileSize(hfile, NULL); CloseHandle(hfile); } is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable) 2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible thanks!
another way to get the file size (and it's portable too) is to use the ftell function: FILE *f = fopen("foo.dat","rb"); long lFileSize = 0; if (f != NULL) { fseek(f,0,SEEK_END); lFileSize = ftell(f); fclose(f); }