Hi,

I have a text file which looks in the following format

=====c:\text.txt ======

AAAA
BBBB
...
...
==================

I have to read each line of the text file into a buffer, and print it, which I did in the following way.

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
 
#define MAX_LENGTH 256
 
int _tmain(int argc, _TCHAR* argv[])
{
     FILE *pF; //pF to pointer to the text file
     char pBuff[MAX_LENGTH]; //Buffer to get each line
     int i = 0;
     int f=0,nf=0;
 
     pF = fopen("c:\\text.txt", "r"); //opening in read mode
     while(fgets(pBuff , MAX_LENGTH , pF)) 
     {
          if(strcmp(pBuff,"")!=0) //to discard empty lines
          {
              int len = strlen(pBuff); 
              pBuff[dbLen-1] = '\0';
              printf("%s::%d\n", pBuff,len);
          }
      }
fclose(rFile);
}
return 0;
}

but, I want to do the same stuff using Win32 APIs :( where I m naive.
But I proceeded in the following way..

created a File handle for the text file to read like this.

HANDLE hFile;

hFile = CreateFile(L"C:\\test.txt",GENERIC_READ,
FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

Now, could any one help me out to which API to be used, to proceed further..

I thought of doing like,

1. get the file size
2. create a char* and allocate the size of file size
3. copying the entire file into a string buffer using Readfile() API, and process the buffer character by character for '\n' and print it to console..

but I think this is not ideal, so is there any other way to do it better..

Thanks in advance
-vijay

win32 api does not have equivalent of fgets(). It only has like fread() and fwrite(). You are much better off by sticking with standard C functions in stdio.h or use c++ functions in <fstream>

HANDLE hFile;

hFile = CreateFile(L"C:\\test.txt",GENERIC_READ,
FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

I thought of doing like,

1. get the file size
2. create a char* and allocate the size of file size
3. copying the entire file into a string buffer using Readfile() API,

int FSize, Actual;
char *Buffer;

FSize = GetFileSize (hFile, NULL);
Buffer = new char (FSize+1);
ReadFile (hFile, &Buffer, FSize, &Actual, NULL);

I'm pretty rusty at C++, but that is how you would read the entire file into memory.

...I thought of doing like,
1. get the file size
2. create a char* and allocate the size of file size
3. copying the entire file into a string buffer using Readfile() API...
but I think this is not ideal, so is there any other way to do it better..

unless you have a very special reason (eg. the file size is larger than what is supported by the implementation of libstdc++ that you have), you should use the standard c++ library.
if you do have to use the win32 api, mapping a section object into the virtual address space of the process would be much more efficient (for large files).
for your simple requirement, the win32 CopyFile function would suffice.

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <cassert>
#include <windows.h>
using namespace std ;

int main()
{

  // portable C++ - copy text file to stdout
  {
    ifstream file( __FILE__ ) ; 
    assert(file) ;
    file >> noskipws ;
    istream_iterator<char> begin(file), end ;
    copy( begin, end, ostream_iterator<char>(cout) ) ;
  }

  // win32 api  - copy text file to stdout
  {
    HANDLE file = CreateFileA( __FILE__, FILE_READ_DATA, 0, 0,
                           OPEN_EXISTING, 0, 0 ) ;
    assert( file != HANDLE(-1) ) ;
    HANDLE map = CreateFileMapping( file, 0, PAGE_READONLY, 0, 0, 0 );
    const char* cstr = static_cast<const char*>( 
                         MapViewOfFile( map, FILE_MAP_READ, 0, 0, 0 ) ) ;
    DWORD nb = 0 ;
    WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), cstr, strlen(cstr), &nb, 0 );
    UnmapViewOfFile(cstr) ;
    CloseHandle(map) ;
    CloseHandle(file) ;
  }

  // win32 api  - simpler copy text file to console
  assert( CopyFileA( __FILE__, "CONOUT$", FALSE ) ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.