Testing EOF with WriteFile/ReadFile ...

This is a discussion on Testing EOF with WriteFile/ReadFile ... within the Windows Programming forums, part of the Platform Specific Boards category; My aim is to read/write multiple records to a file. I've only worked with fprintf/fwrite & fscanf/fread... My first attempt ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Testing EOF with WriteFile/ReadFile ...

    My aim is to read/write multiple records to a file. I've only worked with fprintf/fwrite & fscanf/fread... My first attempt in using Win32 API WriteFile is...

    Code:
    // Retrieve saved records of radar from file an opened file
    void ReadFromFile(CALC_DATA data, DWORD fileSize)
    {
    	// Open for reading
    	HANDLE inFile = fileOpen("RFSfile.dat", OPEN_EXISTING, GENERIC_READ);	
    	
    	// Loop through records of file & read
    	while( ReadFile(inFile, data, sizeof(data), &fileSize, NULL) && fileSize > 0 );
    
    	CloseHandle(inFile);
    }
    
    // Create file for in/out
    void fileOpen(LPCTSTR lpFileName, DWORD fileDiscript, DWORD acces)
    {
    	// Create file for either writing or reading
    	if ( CreateFile(lpFileName, acces, 0, 0, 
    		fileDiscript, 0, 0) == INVALID_HANDLE_VALUE )
    	{
    		WriteFile(STD_ERROR_HANDLE, "Error opening source: %d", GetLastError());
    	}
    }
    Q. Does ReadFile() return NULL when it has reached the end of file, or when the file is empty, or does not exist? I wanted to add an if statement to test for a FALSE return then print an error message, but what if FALSE means the file has finished reading...

    is there a Win32 feof()( like function?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Quote Originally Posted by csonx_p View Post
    Q. Does ReadFile() return NULL when it has reached the end of file, or when the file is empty, or does not exist? I wanted to add an if statement to test for a FALSE return then print an error message, but what if FALSE means the file has finished reading...
    RadFile returns FALSE on failure and GetLastError returns ERROR_HANDLE_EOF when EOF is reached.

    is there a Win32 feof()( like function?
    No. You have to use ReadFile/WriteFile for that (AFAIK).

    Is there a reason you use Win32 API aside from the C functions?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    RadFile returns FALSE on failure and GetLastError returns ERROR_HANDLE_EOF when EOF is reached.


    No. You have to use ReadFile/WriteFile for that (AFAIK).

    Is there a reason you use Win32 API aside from the C functions?
    Learning new stuff i suppose... So my code will run with no probs except i'm not checking for read errors?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    I don't know. Win32 API is too complicated - I never used it nor intend to. The C/C++ file functions are more than enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    strange warning

    I'm getting a warning i can't understand...

    Code:
    HANDLE file_io;
    
    file_io = CreateFile( lpFileName, access, 0, NULL, fileDiscript, 
    		FILE_ATTRIBUTE_NORMAL , NULL);
    CreateFile returns a handle and file_io is declared as a handle

    warning...
    Code:
    warning C4047: '=' : 'HANDLE' differs in levels of indirection from 'int'

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    probably - missing include
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 03:04 PM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  5. testing for EOF with strings
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-26-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21