Now: Tutorial for Web and Software Design > Programming > win32 > Programming Content
> Win32 File Sample [Bookmark it]
Win32 File Sample
   Win32 File Samples article tries to explain how to read from and write to Disk files using 
Win32 functions. Though Win32 is not user-friendly and programmers are moving towards .Net, this could be useful in some places where a quick fix for old programs are needed. 

   The basic idea behind having the set of functions for manipulating the files in win32 was great. The same API was designed to be used for Named Pipes, Mail slots, Memory mapped files etc. The parameters are the only difference. The parameters will depend on the type of object to be opened.

   This win32 file article explains how to make simple use the functions createfile, writefile and readfile for writing and reading disk files. 

Writing to a Win32 File:


   This needs the file to be opened in Write mode (to allow the program a write access) and then call the win32 file function WriteFile with the data to be written. The sample code for the same is given as below.

HANDLE hFile; 
DWORD wmWritten; 
char strData[] = "Test data written to explain the win32 file sample";
hFile = CreateFile("C:\\test.txt",GENERIC_READ|GENERIC_WRITE, 
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
WriteFile(hFile,strData,(DWORD)(sizeof(strData)),&wmWritten,NULL); 
CloseHandle(hFile); 

Reading data from a Win32 File:

   This needs the file to be opened in read mode (to allow the program a read access) and 
then call the win32 file function ReadFile with the address of the character array/pointer 
where the data is to be read. The sample code for the same is given as below.

HANDLE hFile; 
DWORD wmWritten; 
char strVal[1024]; 
hFile = CreateFile("C:\\test.txt",GENERIC_READ|GENERIC_WRITE, 
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
ReadFile(hFile,strVal,1024,&wmWritten,NULL); 
CloseHandle(hFile); 

   It is better to forget using these functions and move towards using MFC classes CFile or 
CStdioFile for file handling. 

[Bookmark][Print] [Close][To Top]
  • Prev Article-Programming:

  • Next Article-Programming:
  • Related Materias
    Passing an Array to and fr
    Using the For匛ach Stateme
    Executing a VB Program wit
    Sorting Viewer Tutorial - 
    Creating a Toolbar - Visua
    Using Office applications 
    Visual Basic Explorer - Tu
    Visual Basic Explorer - OL
    Visual Basic Explorer - OO
    VB Tutorials - SQL for Beg
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial