Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Win32, Dynamically I have created the empty file & i had written some data to the newly created file. The code is like this:

C++
HANDLE hFile = CreateFile("E:\\pszFileName.txt", GENERIC_READ|GENERIC_WRITE,0,0,OPEN_ALWAYS, 
                          FILE_ATTRIBUTE_NORMAL, 0);

if( hFile != INVALID_HANDLE_VALUE)
{
    	char text[] = "This line was added\r\n";
    	DWORD dwBytesWritten = 0;
    	SetFilePointer(hFile, 0, 0, FILE_END);
    	WriteFile(hFile,text, strlen(text), &dwBytesWritten, 0);
    
    	CloseHandle(hFile);
}


Now the file pszFileName contains the data as "This line was added".

My question is : I want to Retrive the data from the file. Is there any way to get the data from the file ?
Posted
Updated 6-Mar-12 8:16am
v4

If you already feel comfortable with WriteFile, ReadFile should be a cinch...
You may find that time spent in MSDN is time well invested.
Hope this helps,
Pablo.
 
Share this answer
 
Comments
Guru_C++ 6-Mar-12 8:28am    
I have used the ReadFile function, but actually it doesn't help me to retrieve the data .. Actually i need data from the File ...
Pablo Aliskevicius 6-Mar-12 8:41am    
ReadFile will read the data in the file; you have to create CreateFile again, with different flags (GENERIC_READ only).
Try to play with the MSDN examples for ReadFile.
If what you want is data about the file (size on disk, date, ...) there are other API functions that may be of assistance.

See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx
and GetFileInformationByHandleEx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364953(v=vs.85).aspx)
Best wishes,
Pablo.
Use ReadFile() to get the data back. What is your problem?
 
Share this answer
 
Below provided code writes data and reads data from a text file. Have a look on it.
int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hFile = CreateFile("E:\\pszFileName.txt", GENERIC_READ|GENERIC_WRITE,0,0,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
	if( hFile != INVALID_HANDLE_VALUE)
	{
	   char text[] = "This line was added\r\n";
	   DWORD dwBytesWritten = 0;
	   SetFilePointer(hFile, 0, 0, FILE_END);
	   WriteFile(hFile,text, strlen(text), &dwBytesWritten, 0);

	   CloseHandle(hFile);
	}

	// Now Read the Data
	hFile = NULL;
	hFile = CreateFile("E:\\pszFileName.txt", GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if( hFile != INVALID_HANDLE_VALUE)
	{
		char textOut[1024] = {0};
	   DWORD dwBytesRead = 0;
	   SetFilePointer(hFile, 0, 0, FILE_BEGIN);
	   ReadFile(hFile,textOut, 1024, &dwBytesRead, 0);
		MessageBox(NULL,textOut,"",MB_OK);
	   CloseHandle(hFile);
	}
	return 0;
}
 
Share this answer
 
Comments
Guru_C++ 6-Mar-12 23:46pm    
Thank you so much... It had worked....:)
you can use fstream class available in standard template library (std) to read and write to files instead of the more complex win32 functions.I have used it before to create my on file system for my programs.

You can write to file as shown below

C++
#include <fstream>

using namespace std;

void main()
{

 string text = "This line was added\r\n"; //would rather use string than array

 ofstream out("E:\\pszFileName.txt",ios::out); //create and open file

 out>>text; //write to file

 out.close(); //close file

 return;
}

and you can read from file


C++
#include <fstream>

#include <iostream>

using namespace std;

void main()
{

 string text,str;

 ifstream in("E:\\pszFileName.txt"); //open existing file

 if(!in) return; //check if file actual exists return otherwise

  while(in)
  {
     in>>str; // read from file. NOTE: Here reading ignores the whitespaces
              
     if(in.eof()) break; //check if end of file is reached

     text += str + " "; // concatenate strings
  } 

 cout<<text; //write to console to check results

 in.close(); //close file

 return;
}

This is simpler and saves you from the complicated win32 functions.
 
Share this answer
 
v2
If you want to use the average c++ style file I/O, try this link[^]and read away! :-)
 
Share this answer
 
I guess that you added that for test issue. Anyway if you wanna create an empty file just use the TRUNCATE_EXISTING mode: look here

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900