Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to use the Win32 API ReadFile function to read data from a file in C

2 Answers

0 votes
#include <windows.h>
#include <stdio.h>

#define BUFFER_SIZE 1024

int main(void) {
    HANDLE hFile;
    DWORD bytesRead;
    char buffer[BUFFER_SIZE];

    // Open an existing file for reading
    hFile = CreateFileA(
        "data.txt",            // File name (ANSI version)
        GENERIC_READ,          // Desired access
        FILE_SHARE_READ,       // Share mode
        NULL,                  // Security attributes
        OPEN_EXISTING,         // Creation disposition
        FILE_ATTRIBUTE_NORMAL, // Flags and attributes
        NULL                   // Template file
    );

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD err = GetLastError();
        printf("Failed to open file. Error code: %lu\n", err);
        return 1;
    }

    // Read from the file
    BOOL success = ReadFile(
        hFile,                   // File handle
        buffer,                  // Buffer to store data
        BUFFER_SIZE - 1,         // Number of bytes to read
        &bytesRead,              // Number of bytes actually read
        NULL                     // Overlapped (NULL for synchronous)
    );

    if (!success) {
        DWORD err = GetLastError();
        printf("Failed to read file. Error code: %lu\n", err);
        CloseHandle(hFile);
        return 1;
    }

    buffer[bytesRead] = '\0';

    printf("Read %lu bytes from file:\n", bytesRead);
    printf("%s\n", buffer);

    CloseHandle(hFile);

    return 0;
}




/*
run:

Read 13 bytes from file:
abcd
efg
hi

*/

 



answered Jan 25 by avibootz
0 votes
#include <windows.h>
#include <stdio.h>

#define BUFFER_SIZE 1024

HANDLE openFile(const char* filename) {
    HANDLE hFile = CreateFileA(
        filename,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD err = GetLastError();
        printf("Failed to open file. Error code: %lu\n", err);
    }

    return hFile;
}

BOOL readFileData(HANDLE hFile, char* buffer, DWORD* bytesRead) {
    BOOL success = ReadFile(
        hFile,
        buffer,
        BUFFER_SIZE - 1,
        bytesRead,
        NULL
    );

    if (!success) {
        DWORD err = GetLastError();
        printf("Failed to read file. Error code: %lu\n", err);
    }

    return success;
}

void printBuffer(const char* buffer, DWORD bytesRead) {
    printf("Read %lu bytes from file:\n", bytesRead);
    printf("%s\n", buffer);
}

int main(void) {
    DWORD bytesRead;
    char buffer[BUFFER_SIZE];

    HANDLE hFile = openFile("data.txt");
    if (hFile == INVALID_HANDLE_VALUE) {
        return 1;
    }

    if (!readFileData(hFile, buffer, &bytesRead)) {
        CloseHandle(hFile);
        return 1;
    }

    buffer[bytesRead] = '\0';
    printBuffer(buffer, bytesRead);

    CloseHandle(hFile);

    return 0;
}



/*
run:

Read 13 bytes from file:
abcd
efg
hi

*/

 



answered Jan 25 by avibootz

Related questions

...