Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

Simple Text File Open and Parse

$
0
0
I'm a rusty amateur coder re-writing some console apps in MS Visual Studio v14 rev 3. My code appears to fail on the file open.

I have these includes...

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

I search the directory for a particular file and find it with this code segment...

hFind = FindFirstFile(lfile, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf("FindFirstFile failed (%d)\n", GetLastError());
return;
}
else
{
_tprintf(TEXT("The first file found is %s\n"),
FindFileData.cFileName);
}


then try to open the file FindFileData.cFileName for reading with this code segment...

hFile = CreateFile(FindFileData.cFileName, // 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 | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
DisplayError(TEXT("CreateFile"));
_tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]);
return;
}

Am I wrong to use FindFileData.cFileName with CreateFile?

Viewing all articles
Browse latest Browse all 3042

Trending Articles