problem with argv[]

Hey guys, I've been trying for way too long just to get this program to print the name of a directory from the command line and the first file name. I always just get a whole bunch of question marks and random characters.This is my first time working with tprintf and TCHAR and stuff, so I really don't know where it's going wrong.

#include <iostream>
#include <String>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <io.h>
#include <stdio.h>
using namespace std;

int main(int argc, TCHAR* argv[]){

LARGE_INTEGER fileSize;
TCHAR directory[MAX_PATH];
size_t length_of_arg;
WIN32_FIND_DATA data;
HANDLE finder = INVALID_HANDLE_VALUE;

StringCchLength(argv[1], MAX_PATH, &length_of_arg);
_tprintf(TEXT("\nTarget directory is %s\n\n"), argv[0]);
StringCchCopy(directory, MAX_PATH, argv[0]);
StringCchCat(directory, MAX_PATH, TEXT("\\*"));
finder = FindFirstFile(directory, &data);

_tprintf(TEXT("The first file found is %s\n"), data.cFileName);
FindClose(finder);
}
That's because argv[0] is the path and name of your application, it's not the first argument from the command line...

EDIT: Also, keep in mind that the very first file returned from this function if you pass it "*.*", which you should be doing if you want to see any and all of the files there, will be a single dot.
Last edited on
You also don't need to use tprintf, "WIN32_FIND_DATA.cFileName[]" is a TCHAR array which is a typecast of char. This will work fine with std::cout << ....
I switched argv[0] with argv[1] and used cout, now I get what appears to be a hex number instead of the question marks. With the first file as well, it prints a weird hex number and the file size is a huge negative number :S.
Are you sure you're getting anything at all? Test your "finder" variable to see if it is equal to "INVALID_HANDLE_DATA" which is an enumeration, not a literal string. if true then

std::cout << GetLastError() << '\n';
Here is a link to the error list: http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx

EDIT: I inserted a white space so that you could see the underscores in INVALID_HANDLE_DATA
Last edited on
added the if statement but still no change in output. I'm a bit of a n00b, sorry :(
That's fine, I just hope I can help in time as I have to leave soon.

Assuming you added the if check before your output we know that you are getting a real file. Also is "directory" a double backslashed UNC path?
INVALID_HANDLE_DATA should be INVALID_HANDLE_VALUE sorry about that.
no worries lol, I've started over and I've got it working better now, thank you for the help :)
Topic archived. No new replies allowed.