read_txt.c
//=============================================================
//read txt
//=============================================================
int read_txt(const char *filename, char *content)
{
const int BUFFERSIZE = 255;
HANDLE hFile;
DWORD dwBytesRead = 0;
char buffer[BUFFERSIZE] = {0};
hFile = CreateFile(filename, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Terminal failure: unable to open file update.txt for read.\n", "Sorry for read file error", MB_OK);
return 0;
}
// Read one character less than the buffer size to save room for
// the terminating NULL character.
if( FALSE == ReadFile(hFile, buffer, BUFFERSIZE-1, &dwBytesRead, NULL) )
{
MessageBox(NULL, "Terminal failure: Unable to read from update.txt.\n", "Sorry for read file error", MB_OK);
CloseHandle(hFile);
return 0;
}
// This is the section of code that assumes the file is ANSI text.
// Modify this block for other data types if needed.
if (dwBytesRead > 0 && dwBytesRead <= BUFFERSIZE-1)
{
buffer[dwBytesRead]='\0'; // NULL character
strcpy(content, buffer);
MessageBox(NULL, content, "haha", MB_OK);
}
else if (dwBytesRead == 0)
{
MessageBox(NULL,"No data read from file %s\n", "Sorry for read file error", MB_OK);
}
else
{
MessageBox(NULL,"** Unexpected value for dwBytesRead ** \n", "Sorry for read file error", MB_OK);
}
// It is always good practice to close the open file handles even though
// the app will exit here and clean up open handles anyway.
CloseHandle(hFile);
return 1;
}
浙公网安备 33010602011771号