C++相同的代码,但不起作用的WinAPI
C++ Same code but not working WinAPI
我对此有点
困惑,但我不明白为什么一个代码有效而另一个代码无效。看起来是一样的,但最大的一个是调用函数"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;
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- WinAPI 的睡眠在子线程中不起作用
- WinAPI - 菜单加速器不起作用
- C++相同的代码,但不起作用的WinAPI