Using CreateFile Function

hi, I'm currently doing an activity where I'm going to open a file and read the content of it by using CreateFile Function.
This is the code that I made just to open the file:

int main()
{
HANDLE hfile;
hfile = CreateFile(
L"C:\\Users\\John.Doe\\Documents\\test.txt",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

if (hfile == INVALID_HANDLE_VALUE) {

cout << "CreateFile Error " << GetLastError() << endl;

}
else {
cout << "create file success" << endl;
}

CloseHandle(hfile);


return 0;
}

Is there a way that I can get the value or the data inside of the test.txt file?
Just like in using fstream?
sample code that I made using fstream.

char myname[50];
int age;

//opening the text file.
ifstream filecontent;
filecontent.open("test.txt");

cout << "Reading from the file" << endl;
filecontent >> myname >> age;

// write the data at the screen.
cout << myname << age;

Any advice? Thank you!
I suggest you use the C++ standard library functions. The windows system functions are lower-level (I think), probably more like the C++ stream's read and write member functions (unformatted input).

But if you want to try it then I think you use ReadFile to read from the file handle.
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-readfile
Last edited on
Topic archived. No new replies allowed.