C++相同的代码,但不起作用的WinAPI

C++ Same code but not working WinAPI

本文关键字:不起作用 WinAPI 代码 C++      更新时间:2023-10-16
我对此有点

困惑,但我不明白为什么一个代码有效而另一个代码无效。看起来是一样的,但最大的一个是调用函数"GetFileAttributes"的字符串中的错误。第一个代码不起作用。

#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 filen");
        scanf("%s", (argv + 1));
    }
    //LPCWSTR l = (LPCWSTR) (argv+1);
    DWORD d = GetFileAttributes(argv[1]);
    printf("%s", "Attrbiutes for this file aren");
    if (d == INVALID_FILE_ATTRIBUTES){
        printf("%s", "Invalid attributesn");
    }
    if (d & FILE_ATTRIBUTE_ARCHIVE){
        printf("%s", "Archiven");
    }
    if (d & FILE_ATTRIBUTE_COMPRESSED){
        printf("%s", "Compressedn");
    }
    if (d & FILE_ATTRIBUTE_DIRECTORY){
        printf("%s", "Directoryn");
    }
    if (d & FILE_ATTRIBUTE_HIDDEN){
        printf("%s", "Hiddenn");
    }
    if (d & FILE_ATTRIBUTE_READONLY){
        printf("%s", "Read-onlyn");
    }
    _getch();
    return 0;
}

这是工作的一个

#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;
}
这不是

使用argv的正确方法。 不要修改它。 将其视为只读。 使用单独的变量来保存文件名,提示用户根据需要填充该变量,然后在使用文件名时使用该变量而不是argv

此外,新的

操作系统版本引入了新的文件属性,您没有考虑到这些属性。 您应该输出所有属性,甚至是您不认识的属性。

此外,您应该停止混合使用 C 和 C++ I/O。 选择一个或另一个并与之保持一致。

尝试更多类似的东西:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
    std::string filename;
    char ch;
    if (argc >= 2)
    {
        filename = argv[1];
    }
    else
    {
        do
        {
            std::cout << "You didn't provide a path to the file. Do you want to type it now? [y/n]" << std::endl;
            if (!(std::cin >> ch))
                return 0;
        }
        while ((ch != 'y') && (ch != 'Y') && (ch != 'n') && (ch != 'N'));
        if ((ch == 'N') || (ch == 'n'))
            return 0;
        cin.clear();
        do
        {
            std::cout << "Type in the path to the file" << std::endl;
            if (!std::getline(std::cin, filename))
                return 0;
        }
        while (filename.empty());
    }
    DWORD d = GetFileAttributesA(filename.c_str());
    std::cout << "Attributes for file: " << filename << std::endl;
    if (d == INVALID_FILE_ATTRIBUTES)
    {
        std::cout << "Invalid attributes" << std::endl;
    }
    else
    {
        if (d & FILE_ATTRIBUTE_ARCHIVE)
        {
            std::cout << "Archive" << std::endl;
            d &= ~FILE_ATTRIBUTE_ARCHIVE;
        }
        if (d & FILE_ATTRIBUTE_COMPRESSED)
        {
            std::cout << "Compressed" << std::endl;
            d &= ~FILE_ATTRIBUTE_COMPRESSED;
        }
        if (d & FILE_ATTRIBUTE_DIRECTORY)
        {
            std::cout << "Directory" << std::endl;
            d &= ~FILE_ATTRIBUTE_DIRECTORY;
        }
        if (d & FILE_ATTRIBUTE_HIDDEN)
        {
            std::cout << "Hidden" << std::endl;
            d &= ~FILE_ATTRIBUTE_HIDDEN;
        }
        if (d & FILE_ATTRIBUTE_READONLY)
        {
            std::cout << "Read-only" << std::endl;
            d &= ~FILE_ATTRIBUTE_READONLY;
        }
        if (d != 0)
        {
            std::cout << "Other: " << std::hex << std::showbase << std::setw(8) << std::setfill('0') << d << std::endl;
        }
    }
    std::cout << "Press a key to exit" << std::endl;
    std::cin >> ch;
    return 0;
}