I'm a little bit confused with that, but I cant understand why one code is working and another isn't.. Seems like it's the same, but largest one cathes and error in the string with calling function "GetFileAttributes". The first code is not working.
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
if (argc < 2){
char answer;
do
{
cout << "You didn't type in the path to the file. Do you want to do it now? [y/n]" << endl;
cin >> answer;
} while (!cin.fail() && answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');
cin.clear();
if (answer == 'N' || answer == 'n'){
return 0;
}
printf("%s", "Type in the path to the file\n");
scanf("%s", (argv + 1));
}
//LPCWSTR l = (LPCWSTR) (argv+1);
DWORD d = GetFileAttributes(argv[1]);
printf("%s", "Attrbiutes for this file are\n");
if (d == INVALID_FILE_ATTRIBUTES){
printf("%s", "Invalid attributes\n");
}
if (d & FILE_ATTRIBUTE_ARCHIVE){
printf("%s", "Archive\n");
}
if (d & FILE_ATTRIBUTE_COMPRESSED){
printf("%s", "Compressed\n");
}
if (d & FILE_ATTRIBUTE_DIRECTORY){
printf("%s", "Directory\n");
}
if (d & FILE_ATTRIBUTE_HIDDEN){
printf("%s", "Hidden\n");
}
if (d & FILE_ATTRIBUTE_READONLY){
printf("%s", "Read-only\n");
}
_getch();
return 0;
}
Here is the working one
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD x = GetFileAttributes(argv[1]);
if (x == INVALID_FILE_ATTRIBUTES)
{
std::cout << "error" << std::endl;
return 0;
}
if (x & FILE_ATTRIBUTE_ARCHIVE)
{
std::cout << "archive" << std::endl;
}
if (x & FILE_ATTRIBUTE_HIDDEN)
{
std::cout << "hidden" << std::endl;
}
if (x & FILE_ATTRIBUTE_READONLY)
{
std::cout << "read only" << std::endl;
}
if (x & FILE_ATTRIBUTE_SYSTEM)
{
std::cout << "system" << std::endl;
}
if (x & FILE_ATTRIBUTE_TEMPORARY)
{
std::cout << "temporary" << std::endl;
}
_getch();
return 0;
}