File I/O

m mistakes. When you are presented with a painful experience, take the time to think about how you can avoid it in the future. This is an example of a lesson learned.*




Last edited on
Well, the only way I know how to do this, is with <windows.h>.
If you are willing to use that, then, here I go:

Win32's function GetFileSizeEx() gets the number of bytes in a file, and, in case you didn't know, one character is one byte. You will have to replace some stuff since I don't think this function works with <fstream> though.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
char fileName[] = "thing.txt";
int fileSize;

//Creates file
HANDLE hFile = CreateFileA(fileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);

//Get file Size
GetFileSizeEx(hFile, fileSize);

cout << "Number of "<< search << "in file is " << count;

CloseHandle(hFile);



I don't know how fond you are of the WinAPI, so here are a couple links for file I/O:

CreateFile: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

ReadFile: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx

GetFileSizeEx: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364957(v=vs.85).aspx

Last edited on
Topic archived. No new replies allowed.