简体   繁体   中英

Get the content of a txt file

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.

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 .

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);
}

您可以使用CreateFile函数创建HANDLE。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM