Searching for and Reading INIs
Hey, I'm looking for some direction in where to start
This app. I'm attempting to make is a console app. that will search the C: for .INI's and check for 13character strings, and then write them to a seperate .txt
Help please!
[254 byte] By [
80Degrees] at [2007-11-19 18:30:35]

# 1 Re: Searching for and Reading INIs
You can use FindFirstFile (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstfile.asp)/FindNextFile (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findnextfile.asp) to enumerate files/directories on a disk. Then when wanted files are found, you can use GetPrivateProfileSectionNames (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getprivateprofilesectionnames.asp)/GetPrivateProfileSection (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getprivateprofilesection.asp) to enumerate values in the .ini files.
- petter
# 2 Re: Searching for and Reading INIs
Much appreciated, Wildfrog!
# 3 Re: Searching for and Reading INIs
Hrm, I'm having trouble implementing these functions, anywhere I can find sample code thats well commented?
# 4 Re: Searching for and Reading INIs
My code looks like this at the moment... it's a little modified from the sample they give at MSDN
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
int main()
{
char findFile [80];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Enter file to find: ");
scanf ("%s",findFile);
hFind = FindFirstFile(findFile, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
return (0);
}
else
{
printf ("The first file found is %x\n",
FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}
Problem is... for anything I enter in to search for it gives me: "The first file found is 12fe1c"
# 5 Re: Searching for and Reading INIs
Use "The first file found is %s\n" instead of "The first file found is %x\n", otherwise, it displays the address of the FindFileData.cFileName string buffer.
# 6 Re: Searching for and Reading INIs
Thanks! That fixed it, could you provide a quick explenation on the value's behind %d %s and %x?
Actually, correct me if I'm wrong but %d returns decimal value, %x returns hex value and %s returns string value?
# 7 Re: Searching for and Reading INIs
Actually, correct me if I'm wrong but %d returns decimal value, %x returns hex value and %s returns string value?
The word "return" is not exact.
It is better to say : %d outputs a decimal value, %x outputs an hex value and %s outputs a string value.
printf does not know the type of the parameters that are passed to it.
printf interprets its arguments, depending on the format string.
If it sees %x, he interprets the argument as an integer, and outputs it in hexadecimal format.
If it sees %d, he interprets the argument as an integer, and outputs it in decimal format.
If it sees %s, he interprets the argument as a pointer to an array of characters, and look at the data pointed, outputing each character pointed to, until it reaches a '\0' character.
Knowing that a string is just a pointer to a character array, arbitrary terminated by a '\0' character, and knowing that a pointer is just an address (whose size is often 4 bytes), we can deduce that printf("%x",FindFileData.cFileName); outputs (in hex format) the address of the first byte of the character array which represents the string.
C++ has iostreams, which don't need an explicit format specifier. :)
But, your program appears to be a C program. You can still learn C++ :D :lol:
# 8 Re: Searching for and Reading INIs
Yes, I am actually currently taking an introduction to programming course (C++). This program I did not code myself, just edited it a little from the sample given at MSDN which appears to be in C, making things just a little more difficult for me! ;o
# 9 Re: Searching for and Reading INIs
My code now looks as
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <string.h>
#include <stdio.h>
int main()
{
char findFile [80];
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH + 1];
DWORD dwError;
printf ("Enter file to find: ");
scanf ("%s",findFile);
strncpy (DirSpec, findFile, strlen(findFile)+1);
strncat (DirSpec, "\\*", 3);
hFind = FindFirstFile(findFile, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. Error is %u\n",
GetLastError ());
return (-1);
}
else
{
printf ("The first file found is %s\n",
FindFileData.cFileName);
while (FindNextFile(hFind, &FindFileData) != 0)
{
printf ("Next filename is %s\n",
FindFileData.cFileName);
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u\n", dwError);
return (-1);
}
}
return (0);
}
Which works well in finding multiple files in the directory that I specify but it doesn't go into any subdirectories, is there a way to change that?
# 10 Re: Searching for and Reading INIs
...but it doesn't go into any subdirectories, is there a way to change that?
This FAQ can help you: How to search for files in a directory and subdirectories? ( http://www.dev-archive.com/forum/showthread.php?t=312461)
Cheers
# 11 Re: Searching for and Reading INIs
Got many errors when trying to compile that code, Golanshahar
# 12 Re: Searching for and Reading INIs
Got many errors when trying to compile that code, Golanshahar
What kind of error? That code should work.
Cheers