CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010
    Posts
    10

    Unhappy Help with ReadFile()

    Hello, I wrote this code. All it does is read Readme.txt and displays its content. How could I modify my code so that it could read Readme.txt from a Nth byte position I specify?

    For example, the content of the TXT File is: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    I want my function to read the file from (for example) the F until M and store it in a variable.

    I need my function to read only 8 byte of a file from Nth position. Is it possible?? Thanks!

    Code:
    int main(int argc, char* argv[])
    {
    
    	HANDLE hFile;
    
    	hFile = CreateFile("H:\\readme.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,0,0);
    
    	if (hFile == INVALID_HANDLE_VALUE) {
    		printf("File was not found\n");
    	} else {
    	
    	DWORD dwFileSize;
    	dwFileSize = GetFileSize(hFile, NULL);
        
    	if(dwFileSize != 0xFFFFFF) { 
    	 
    		LPSTR FileContent;
    		FileContent = (LPSTR) GlobalAlloc(GPTR, dwFileSize +1);
    		if (FileContent != NULL)
    		{
    		  DWORD dwRead;
    
    
    
              if(ReadFile(hFile,FileContent, dwFileSize, &dwRead,NULL)) {
    		     
    			  FileContent[dwFileSize] = 0; // NULL TERMINATOR
    			  printf(FileContent);
    		  }
    		  GlobalFree(FileContent);
    
    		}
    	}
    CloseHandle(hFile);
    	}
    return 0;
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Help with ReadFile()

    Use fseek()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured