How to change permissions of a file in Linux with C++ [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to change the permissions of a file with C++ in Linux. The user has to enter the permissions using this syntax: "Please enter the permissions: rwx-w-r--" in the terminal.
Thanks for helping.

#include <sys/types.h>
#include <sys/stat.h>
int main() {
chmod("./myfile", S_IRWXU); // enables owner to rwx file
}
See man 2 chmod for more details.
If the question is how to parse a 9 character string of the form "rwx-w-r--", realize that the permissions are encoded in an int as bits. If a bit is on, that permission is on. The following code will take your string, turn it into bits, in the obvious way...no validation etc. is done. It's a proof-of-concept.
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
int parse(char* perms) {
int bits = 0;
for(int i=0; i<9; i++){
if (perms[i] != '-') {
bits |= 1<<(8-i);
}
}
return bits;
}
int main() {
char perms[]="rwx-w-r--";
int exmp = S_IRWXU | S_IWGRP | S_IROTH;
printf("%d %d\n", parse(perms), exmp);
// outputs 468 468
}

Related

Just input a number in cpp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm need to create id just number. For example: 12345678
If user inputs fail( contain char), delete char immediately.
For example: 123a =>input gain!
Please help me!
I think this is what you are looking for:
#include <iostream>
#include <string>
#include <cctype>
int main () {
std::string input;
bool valid;
do {
valid = true;
std::cin >> input;
for (char c : input)
if (! std::isdigit( static_cast<unsigned char>(c) ) )
valid = false;
} while (! valid);
// Here the string is guaranteed to be valid
}
Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.

Copy structure with char array to char array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a problem as the topic.
My code:
struct message{
char sender_name[20];
char dest_name[20];
char content_message[256];
};
int main() {
message tmp;
printf("Enter your name\n");
std::cin>>tmp.sender_name;
printf("Enter dest name\n");
std::cin>>tmp.dest_name;
printf("Enter message to %s \n",tmp.dest_name);
std::cin>>tmp.content_message;
memcpy(&buffer, &tmp,sizeof(tmp));
printf("MEASAGE: %s\n",buffer);
return EXIT_SUCCESS;
}
In buffer is only tmp.sendername and I don't know how to fix it.
I read a lot of topics but I can't fix it.
Please for help.
It copies everything, you're just accessing it wrong:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
struct message{
char sender_name[20];
char dest_name[20];
char content_message[256];
};
char buffer[sizeof(message)];
int main() {
message tmp;
printf("Enter your name\n");
std::cin>>tmp.sender_name;
printf("Enter dest name\n");
std::cin>>tmp.dest_name;
printf("Enter message to %s \n",tmp.dest_name);
std::cin>>tmp.content_message;
memcpy(&buffer, &tmp,sizeof(tmp));
message *cpy = reinterpret_cast<message*>(buffer);
printf(
"sender_name: '%s\n'"
"dest_name: '%s\n'"
"content_message: '%s\n'",
cpy->sender_name,
cpy->dest_name,
cpy->content_message
);
return EXIT_SUCCESS;
}
Test:
g++ buffer.cc && printf '%s\n' foo bar baz | ./a.out
Output:
Enter your name
Enter dest name
Enter message to bar
sender_name: 'foo
'dest_name: 'bar
'content_message: 'baz
'
Also, BTW, probably not a good way to read into char buffers. There can hardly be any range checking with this API (given that it also appears to work with pointers as opposed to just arrays) which would make it about as unsafe as gets. It also doesn't appear to be documented (http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) and out of gcc and clang, only gcc accepts it.

How detect spacebar? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I havenĀ“t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???
Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h

Extract HTML source code linux library [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this
Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.

OpenKey Windows 7 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what have of wrong here ? Work in my notebook, but not in my PC . . .
The two are 64-bits, Windows 7 ultimate.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
HKEY CH;
char File_Name[] = "C:\\Users\\RMS\\Desktop\\M.txt";
if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0){
printf("Erro - RegCreateKey\n");
system("PAUSE");
return -1;
}
if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0) // Abre a CH "Minha CH"
{
printf("Erro - RegOpenKey\n");
system("PAUSE");
return -1;
}
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",40) != 0)
printf("Erro - RegSetValue\n");
RegCloseKey(CH);
printf("\nsucesso !\n");
system("PAUSE");
return 0;
}
I found. . . Was only do this:
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",60) != 0)
VERY THANK GUYS !!
Your problem is that the HKLM registry key is only writable by elevated programs, and your program is not running elevated. The reason it works on one machine and not the other is that one has user access control turned down/off while the other doesn't.
If you ran the program from an elevated command prompt it will work.
Additionally, you're using L"" for the strings, but using a RegSetValueEx call with 40, which is 40 bytes, and will actually cut off the M.txt on the text you're setting (if it works at all). Where you initialize the .txt file you should use:
TCHAR File_Name[] = L"C:\\Users\\RMS\\Desktop\\M.txt";
Then for the RegSetValueEx you do:
RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) File_Name, sizeof File_Name + sizeof(TCHAR))
This makes it the number of bytes that corresponds to the filename, plus the final NULL TCHAR.
what error do you get?
try
RegOpenKeyEx
instead of RegOpenKey, since that's for 16 bit windows.