N
N
nkorobkov2017-06-05 17:14:32
C++ / C#
nkorobkov, 2017-06-05 17:14:32

WinAPI: Write Access Violation. How to fix?

Task:
1. Check if the current directory contains the file Example1.txt /
2. if so, copy it to the file Mycopy.txt before copying, check that there is space on the disk for this file.
3. Display this file in portions of 2-4Kb, considering this file as a text file.
My clumsy code:

#include "windows.h"
#include <iostream>

using namespace std;

int WINAPI WinMain(HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR lpCmdLine,
  int nCmdShow)
{
  setlocale(LC_ALL, "Russian");
  char buffer[100];
  HANDLE myfile;
  bool isSucceed;

  myfile = CreateFile(L"myfile.txt", GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL, NULL);

  isSucceed = ReadFile(myfile, buffer, 10, 0, NULL);

  CloseHandle(myfile);
  if (!isSucceed) {
    return NULL;
  }
  else {
    HANDLE copy;
    copy = CreateFile(L"copy.txt", GENERIC_READ | GENERIC_WRITE,
      FILE_SHARE_READ | FILE_SHARE_WRITE,
      NULL, CREATE_ALWAYS,
      FILE_ATTRIBUTE_NORMAL, NULL);
    WriteFile(copy, buffer, 100, 0, NULL);
  }

  system("pause");
  return 0;
}

Error:
df38adb2d4bb4fb3b2b35760f2953733.pngHelp!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2017-06-05
@nkorobkov

What about checking myfile for NULL?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question