473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with FindFirstFile

Hi group

I have just got the FindFirstFile function working in my small tutorial app,
the problem is that I can only seem to get it working when I hard code the
search string. I have defined a string str1 but I get a conversion error
when passing it to the function. Also when I attempt to print a string I
get a memory error: "Memory could not be read".

I have included the code below for you all to see, it is just a subset of
the code I am using.

I would be *very* grateful of any pointers you could provide me with to
fixing this problem.

Kind regards

Rich Strang

#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

string str1 = "main.cpp";

//printf ("Target file is %s.\n", str1);
hFind = FindFirstFile(str1, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError
());
return (0);
}
else
{
printf ("The first file found is %s\n", FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}
Jul 22 '05 #1
2 16103

"Rich Strang" <ri*******@hotmail.com> wrote in message news:uy*********************@news-text.cableinet.net...

string str1 = "main.cpp";
hFind = FindFirstFile(str1, &FindFileData);


FindFirstFile takes a const char* (or perhaps a const wchar_t*) not a string.
hFind = FindFirstFile(str1.c_str(), &FindFileData);
Jul 22 '05 #2

"Rich Strang" <ri*******@hotmail.com> wrote in message
news:uy*********************@news-text.cableinet.net...
Hi group

I have just got the FindFirstFile function working in my small tutorial app, the problem is that I can only seem to get it working when I hard code the
search string. I have defined a string str1 but I get a conversion error
when passing it to the function. Also when I attempt to print a string I
get a memory error: "Memory could not be read".

I have included the code below for you all to see, it is just a subset of
the code I am using.

I would be *very* grateful of any pointers you could provide me with to
fixing this problem.

Kind regards

Rich Strang

#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

string str1 = "main.cpp";

//printf ("Target file is %s.\n", str1);
hFind = FindFirstFile(str1, &FindFileData);
Try this:
printf ("Target file is %s.\n", str1.c_str());
hFind = FindFirstFile(str1.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError
());
return (0);
}
else
{
printf ("The first file found is %s\n", FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}


Max.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tom | last post by:
Hello, I want to know if FindFirstFile() can do more as I expect. FindFirstFile() can take the arguement of "*" or "?", for example, FindFirstFile("result.*.txt", &FileData); I want to...
1
by: Paulo Eduardo | last post by:
Hi, all! We are devleping one app that will use disk access with FindFirstFile and FindNextFile for it. Can someone expose me how works FindFirstFile and FindNextFile in disk access? Does the...
3
by: vighnesh | last post by:
Hello Everyone I am developing a project in VB.NET in which I need to use Win32 API function FindFirstFile()/FindNextFile()/FindClose() The same code is wroking fine in VB 6.0 , but when I used...
2
by: LewS | last post by:
My application uses FindFirstFile for searching for specific file types entered by a user. Entries like c:\myfiles\*.txt and \\myserver\shareddrive\myfiles\*.txt work okay, but when I use a UNC...
13
by: Lance | last post by:
Hi All, I'm working on a program that requires searching multiple drives for multiple file types and cataloging them based on certain geospatial attributes. All together, there are hundreds of...
1
by: Rups | last post by:
API FindFirstFile searches for files based on long and short names(8+3). If i need to search files based on long names only . How can i do ?Is there any other API which does search based on long...
12
by: DFS | last post by:
I need to scan various network folders and capture all the filenames and create dates. ======================================================= 1st effort used the typical drive scan cFile =...
9
by: Comcast Newsgroups | last post by:
Hello everyone, I am programming in Visual Studio 2002, VB.net. I typically stick to version 1.0 of the framework for compatibility reasons. Recently, I ran into an issue that I couldn't...
3
by: gudiya | last post by:
hi, My code works but i am missing the first 2 characters of file name. Any idea why???? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.