0

I am using the windows API in C++ and I want to get the content of a specific txt file. I contemplate using the ReadFile function but I don' t know what I should use in place of HANDLE or, in other words, how I should pass as parameter the name of the txt file. What is the best way generally to get the content of a txt file using windows API.

arjacsoh
  • 8,002
  • 23
  • 99
  • 158

3 Answers3

2

First, you must invoke CreateFile ("Creates or opens a file or I/O device"). It returns a handle which you subsequently pass to ReadFile.

When you are done, don't forget to call CloseHandle.

Robᵩ
  • 143,876
  • 16
  • 205
  • 276
2

Use CreateFile(), supplying GENERIC_READ for the dwDesiredAccess argument and OPEN_EXISTING for the dwCreationDisposition argument, to obtain a HANDLE to pass to ReadFile().

Or, simpler, just use std::ifstream:

#include <fstream>
#include <vector>
#include <string>

...

std::vector<std::sting> lines;
std::ifstream in("input.txt");
if (in.is_open())
{
    std::string line;
    while (std::getline(in, line)) lines.push_back(line);
}
hmjd
  • 113,589
  • 17
  • 194
  • 245
0

You can create the HANDLE with CreateFile function.

SingerOfTheFall
  • 27,171
  • 7
  • 60
  • 100