【发布时间】:2014-11-26 17:02:58
【问题描述】:
我对此有点困惑,但我不明白为什么一个代码可以工作而另一个代码不能工作。看起来是一样的,但最大的一个在调用函数“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 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;
}
这是工作中的一个
#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;
}
【问题讨论】:
-
我不太明白你在问什么。进一步描述您的问题。
-
一个代码有效,另一个无效。为什么?
-
如果第一个代码不起作用,请使用第二个。 ... [讽刺关闭] 他们看起来不像我,无论是在代码还是行为上。那你想做什么?
-
a) 你使用 printf 很奇怪。 b) 不允许使用自己的内容填充 argv。 c) 在 INVALID_FILE_ATTRIBUTES 的情况下,第一个代码缺少返回。 d) 在使用 argv 之前始终检查 argc。 e) 不要使用 _tmain 和 TCHAR 等。 f) 不要使用 conio.h。 g) 带有 %s 的 scanf 存在空白问题。 h) 不要混合使用 C 和 C++。
-
"isn't working" 不是一个有用的问题描述,“cathes and error”(我认为这意味着“导致错误”)也不是一个有用的问题描述,因为您没有包含信息关于“错误”是什么。如果您需要帮助,请提供相关信息并提出具体问题。