#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD d = GetFileAttributes(argv[0]);
_TCHAR* temp;
printf("%d\n", d);
switch(d)
{
case 2048: temp = L"Compressed"; break;
case 32: temp = L"Archive"; break;
case 16: temp = L"Directory"; break;
case 16384: temp = L"Encrypted"; break;
case 2: temp = L"Hidden"; break;
case 128: temp = L"Normal"; break;
case 1: temp = L"Readonly"; break;
case 4: temp = L"System"; break;
case 256: temp = L"Temporary"; break;
default: temp = L"Error or unsupported attribute"; break;
}
_tprintf(temp);
getch();
return 0;
}
what's wrong with this code? I always get 32 in d, even when I launch it with no attributes?
I'm using visual studio 2010.
Thank you!
argv[0] is the name of your executable program. Simply set the index to 1 (ensure it exists). You may also want to use a bitwise AND operation to determine if a flag is set.
Related
i have a question:
(i use winows, visual studio)
i making a program and i need to read keyboard and mouse input istantly so like getch() (because if i use cin i need to press fullstop new line evry time).
i have this:
char p;
while (running) {
if (_kbhit()) {
p = _getch();
switch (p) {
case 'w':
mx = -1;
map(player, mx, 0);
break;
case 'a':
my = -1;
map(player, 0, my);
break;
case 's':
mx = 1;
map(player, mx, 0);
break;
case 'd':
my = 1;
map(player, 0, my);
break;
case 27:
running = false;
break;
}
}
}
the problem is with _getch() i can't read mouse imput.
so how i can get this?
I used getopt() function to get value of optarg parameters in Visual Studio. However, following to document of GNU, getopt() is a function that used to parse command-line options of the Unix/POSIX style and I can not use commandline in Visual Studio. So, how can I get the optarg in VS without using command line or getopt().
Here is my code:
int ch;
extern char* optarg;/*
extern int optind, opterr;*/
char* testfn;
bool testflag = false;
double prd = 60 * 24; // minutes of a day
int maxstep = 100; // # em steps
int Z = 2; // # mixtures
double initscale = 0.25 * prd; // 6 hours
while (optind < argc) {
if ((ch = getopt(argc, argv, "t:m:z:s:")) != -1) {
switch (ch) {
case 't':
testflag = true;
testfn = optarg;
break;
case 'm':
maxstep = atoi(optarg);
break;
case 'z':
Z = atoi(optarg);
break;
case 's':
initscale = atof(optarg);
break;
default:
usage();
}
}
else {
// Regular argument
//code to handle the argument
optind++; // Skip to the next argument
}
}
I'm trying to implement the program, which recognizes Windows file attributes. I have a code, but sometimes, I receive heap error after passing return in the main block. Thank you for your attention and help!
#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
_TCHAR* getStringAttributes(int value, _TCHAR* str[])
{
DWORD d = GetFileAttributes(str[value]);
_TCHAR* temp;
switch(d)
{
case 2048: temp = L"Compressed\n"; break;
case 32: temp = L"Archive\n"; break;
case 16: temp = L"Directory\n"; break;
case 16384: temp = L"Encrypted\n"; break;
case 2: temp = L"Hidden\n"; break;
case 128: temp = L"Normal\n"; break;
case 1: temp = L"Readonly\n"; break;
case 4: temp = L"System\n"; break;
case 256: temp = L"Temporary\n"; break;
default: temp = L"Error or unsupported attribute\n"; break;
}
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR* attString = new _TCHAR();
char* ynAnswer = new char();
if(argv[1] == NULL)
{
printf("%s\n","You didn't type file path. Try again?[Y/N]");
gets_s(ynAnswer,10);
if(*ynAnswer == 'y' || *ynAnswer == 'Y')
{
printf("%s\n","Type in a path to the file");
argv[1] = new _TCHAR();
_getws(argv[1]);
if(argv[1] != L"")
{
printf("%s","Attribute: ");
attString = getStringAttributes(1,argv);
_tprintf(attString);
printf("%s","for\n");
_tprintf(argv[1]);
}
}
}
else
{
printf("%s","Attribute: ");
attString = getStringAttributes(1,argv);
_tprintf(attString);
}
printf("%s","Goodbye");
getch();
delete[] ynAnswer;
delete[] attString;
return 0;
}
you input whole string into 1 or 2 bytes strings.
These allocations:
_TCHAR* attString = new _TCHAR();
char* ynAnswer = new char();
Allocate only 1 item array.
it should be:
_TCHAR* attString = new _TCHAR[MAX_SIZE];
char* ynAnswer = new char[MAX_SIZE];
When MAX_SIZE must beeb defuned as macro.
even better is to use:
wstring attString ;
string ynAnswer;
wcin>> attString;
cin>> ynAnswer;
More problem is the allocation in the line:
argv[1] = new _TCHAR();
in addition to the previous answered problem, in this line argv[1] may not even exists.
You should input to other buffer.
it can be like this:
wstring argv1;
if(argc <2){
wcin>>argv1;
}else{
argv1=argv[1];
}
now use argv1 instead of argv[1]
One more thing:
You are using _TCHAR as WCHAR.
when you use _TCHAR strings should be decalared as:
_T("some string")
not as:
L"some string".
I want to move the position of symbol "A" in the terminal via the following code in c++, but the terminal closes and seems it does not enter the for loop. I don't know where I am wrong. I will be grateful if you help me:
'w' should move it up
's' should move it down
'a' and 'd' to right and left
#include <iostream>
#include <conio.h>
#include <string>
#include <Windows.h>
using namespace std;
void goToXY(int x=0,int y=0)
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(h,c);
}
int main()
{
char symbol='A';
int X=0, Y=0;
goToXY(X,Y);
cout<<symbol;
for(;;)
{
if(kbhit())
{
char ch = getch();
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
case 's':
goToXY(X,Y+1);
cout<<symbol;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
}
}
getch();
return 0;
}
}
1) You forgot to add break; after each case-body.
2) And you've put return 0; in the body of for-loop, so your program stops after first iteration.
Try this:
for(;;)
{
if(kbhit())
{
char ch = getch();
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
break;
case 's':
goToXY(X,Y+1);
cout<<symbol;
break;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
break;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
break;
}
}
}
getch();
return 0;
You have not used the break; statement after each case in your switch statement. Hope this helps.
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
break;
case 's':
goToXY(X,Y+1);
cout<<symbol;
break;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
break;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
break;
}
I know this queston has been asked multiple times, but still I am unable to figure this out
#include<stdio.h>
#include<getopt.h>
int ch;
int queue_time=60;
int thread_num=4;
char *scheduling_algo="FCFS";
extern char *optarg;
int port=8080;
int debug_flag,h_flag,l_flag;
int main(int argc,char *argv[])
{
while ((ch = getopt(argc, argv, "dhlprtns")) != -1)
switch(ch)
{
case 'd':
debug_flag=atoi(optarg); /* print address in output */
break;
case 'h':
h_flag=atoi(optarg);
break;
case 'l':
l_flag=atoi(optarg);;
break;
case 'p':
port = atoi(optarg);
break;
case 'r':
printf("%s",optarg);
break;
case 't':
queue_time = atoi(optarg);
break;
case 'n':
thread_num = atoi(optarg);
break;
case 's':
scheduling_algo = optarg;
break;
default:
printf("nothing was passed");
}
printf("%d",queue_time);
printf("%d",debug_flag);
printf("%d",h_flag);
printf("%d",l_flag);
}
I am executing my program using the following command
./a.out -d -h -l -t 55
I am getting the core dumped error . I read a few examples on google, but still I am facing this problem. Can anyone please help?
You need to read the man page for getopt()
while ((ch = getopt(argc, argv, "dhlprtns")) != -1)
^^^^^^^^
This does not correspond to the way you are using the arguments. You
want colons ":" after the flags which expect arguments. In your code
"d" is not followed by a colon and yet you seem to want an value for it:
case 'd':
debug_flag=atoi(optarg); /* print address in output */
break;
So what is happening is you are calling atoi(0) and this is seg faulting.
Here's the example from the man page, note how "b" is not followed by a
colon while "f" is.
#include <unistd.h>
int bflag, ch, fd;
bflag = 0;
while ((ch = getopt(argc, argv, "bf:")) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 'f':
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
(void)fprintf(stderr,
"myname: %s: %s\n", optarg, strerror(errno));
exit(1);
}
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
This may be of use to others: You will also get a segfault if you specify an option letter as both without colon, and with colon eg "dabcd:e" - in this case "d" occurs with and without colon.... and then use that option letter.
It appears getopt and its variants do not check for this conflict and return an error!