如何使用VC ++ Win32从文本文件中检索数据 [英] How to Retrieve the data from text file using VC++ Win32

查看:73
本文介绍了如何使用VC ++ Win32从文本文件中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Win32中,我动态创建了一个空文件&我已经向新创建的文件中写入了一些数据.代码是这样的:

 HANDLE hFile = CreateFile("  ,GENERIC_READ | GENERIC_WRITE, 0  0 ,OPEN_ALWAYS,
                          FILE_ATTRIBUTE_NORMAL, 0 );

如果(hFile!= INVALID_HANDLE_VALUE)
{
    字符 text [] = " ;
    DWORD dwBytesWritten =  0 ;
    SetFilePointer(hFile, 0  0 ,FILE_END);
    WriteFile(hFile,text,strlen(text),& dwBytesWritten, 0 );
    
    CloseHandle(hFile);
} 



现在,文件pszFileName包含的数据为已添加此行".

我的问题是:我想从文件中检索数据.有什么方法可以从文件中获取数据吗? br/> 您可能会发现花费在MSDN上的时间是值得投资的.
希望这会有所帮助,
Pablo.


使用ReadFile()取回数据.您的问题是什么?


下面提供的代码写入数据并从文本文件读取数据.看看吧.

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;
}


In Win32, Dynamically I have created the empty file & i had written some data to the newly created file. The code is like this:

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 ?

解决方案

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.


Use ReadFile() to get the data back. What is your problem?


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;
}


这篇关于如何使用VC ++ Win32从文本文件中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆