I'm trying to do a recursive search for files in the c:\ directory using c++, but I saw that the recursive_directory_iterator enters the first directory, does the recursive search, but when it enters the next directory it exits the loop and I'm not able to solve this problem.
my code:
#include <filesystem>
#include <iostream>
using namespace std;
int main(){
string directory = "c:\\Users\\";
auto d_init = filesystem::recursive_directory_iterator(directory.c_str(),filesystem::directory_options::skip_permission_denied);
auto d_end = filesystem::end(d_init);
auto error_code = std::error_code();
for(; d_init != d_end; d_init.increment(error_code)){
if(!error_code){
if(d_init->is_regular_file()){
cout << "File: " + d_init->path().string() << endl;
}
}
}
cout << endl << "End Here" << endl;
}
File: c:\Users\All Users\chocolatey\tools\7z.exe
File: c:\Users\All Users\chocolatey\tools\7z.exe.ignore
File: c:\Users\All Users\chocolatey\tools\7z.exe.manifest
File: c:\Users\All Users\chocolatey\tools\7zip.license.txt
File: c:\Users\All Users\chocolatey\tools\checksum.exe
File: c:\Users\All Users\chocolatey\tools\checksum.exe.config
File: c:\Users\All Users\chocolatey\tools\checksum.exe.ignore
File: c:\Users\All Users\chocolatey\tools\checksum.license.txt
File: c:\Users\All Users\chocolatey\tools\shimgen.exe
File: c:\Users\All Users\chocolatey\tools\shimgen.exe.ignore
File: c:\Users\All Users\chocolatey\tools\shimgen.license.txt
End Here
PS C:\Users> cmd.exe /c dir /A
O volume na unidade C é Windows
O Número de Série do Volume é 10FB-D95E
Pasta de C:\Users
06/04/2022 08:44 <DIR> .
06/04/2022 08:44 <DIR> ..
07/12/2019 06:30 <SYMLINKD> All Users [C:\ProgramData]
15/02/2022 08:20 <DIR> XXXXXX4
15/02/2022 08:20 <DIR> XXXXXX3
15/02/2022 08:20 <DIR> XXXXXX2
15/02/2022 08:20 <DIR> XXXXXX1
Related
There is an error when compiling the code in visual studio, the code and the error are copied here, the code seems to be fine still the problem is occurring, kindly help.The error keeps on coming, I've also attested a screenshot of the same,it might help in finding the mistake, do look into it as I am not able to interpret its meaning.
#include <iostream>
#include<windows.h>
using namespace std;
int main(){
int h,m,s,a,err; cout<<"Enter hour"<<endl;
cin>>h;
err=a=0;
while (err==0) {
cout<<"Enter hour"<<endl;
cin>>h;
cout<<"Enter minutes"<<endl;
cin>>m;
cout<<"Enter seconds"<<endl;
cin>>s;
if(h<24&& m<60&& s<60)
err++;
else
system("cls"); }
while (a==0)
{
system("cls");
cout<<h<<":"<<m<<":"<<s<<endl;
Sleep(1000);
s++;
{ s=0;
m++; }
if (m>59)
{m=0;
h++;}
if(h>24)
{
h=0; }
}
return 0;
}
THE ERROR
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\acer\Desktop\C PROGRAMS\.vscode> dir
Directory: C:\Users\acer\Desktop\C PROGRAMS\.vscode
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 12-05-2021 01:13 .vscode
-a---- 11-05-2021 23:06 83 a.c
-a---- 11-05-2021 23:45 54022 a.exe
-a---- 12-05-2021 01:12 782 calc.clock.cpp
-a---- 12-05-2021 02:18 723 calc.cpp
-a---- 12-05-2021 01:32 3847555 calc.exe
-a---- 12-05-2021 01:10 782 hello.cpp
-a---- 11-05-2021 23:44 75943 hello.exe
-a---- 11-05-2021 23:38 83 helloworld.c
-a---- 06-04-2021 15:42 180 settings.json
PS C:\Users\acer\Desktop\C PROGRAMS\.vscode> .\calc.exe
Program 'calc.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:1
+ .\calc.exe
+ ~~~~~~~~~~.
At line:1 char:1`enter code here`
+ .\calc.exe
+ ~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
I tried to use system function to run a cmd command, but I can't get the command output, Because My Windows is Italian When I type / in my system() function to call cmd its getting actually - and not getting / I tried this in my cmd and is receiving this - instead / ,I tried to use chcp 437 for English cmd but , it didn't work
example :
system("net user xxx xxxx /add");
the command is getting :
net user xxx xxxx -add
I just do not want to do this to work in Italian Windows and work for other languages actually, how to solve this problem?
You should never use system(). You are programming in C++. There is no need to use system() since you have access to everything in the, well, system :D. system() was written in C after all.
And there is the security risk: someone could replace system() or the command you are trying to run using system() in your machine and make not-nice-things in your system.
you can change the code page in your code before calling system() using
SetConsoleOutputCP(); that lives in windows.h
1252 is the Latin codepage and should do ok in Italian. Also 65001 is the utf-8 codepage and should also work well
to run your program on the "new" Windows Terminal is also an option since it is Unicode
pass a string to system() and not a literal. this way you can be sure it has what you want, before the call.
it is a good practice to save the codepage in use before change and restore it on exit
A C++ Example
This program
takes an array of commands
const char* command[] =
{
"DIR .\\*.* /O:D",
"NET USER /Add /?"
};
and runs on the console. The commands uses slashes and backslashes and outputs text so you can test a bit more. And you can just edit the array and add new commands to test
You can try alternative codepages. Here I used 65001, the one for Unicode
int originalOCP = GetConsoleOutputCP();
std::cout << "Original CodePage: " << originalOCP << "\n";
SetConsoleOutputCP(65001);
std::cout << "CodePage now is " << GetConsoleOutputCP() << "\n";
The command is written on the console before being passed to system()
std::cout <<
"\n\n\t==> command " <<
i << " is '" <<
command[i] << "'\n\n";
system(command[i]);
The output in Portuguese Windows
Original CodePage: 850
CodePage now is 65001
==> command 0 is 'DIR .\*.* /O:D'
O volume na unidade C não tem nome.
O Número de Série do Volume é 7E52-1BF2
Pasta de C:\Users\toninho\source\repos\ConsoleApplication8\ConsoleApplication8
29/10/2020 10:21 168 ConsoleApplication8.vcxproj.user
29/10/2020 10:38 974 ConsoleApplication8.vcxproj.filters
29/10/2020 10:38 7.199 ConsoleApplication8.vcxproj
29/10/2020 10:59 676 a.cpp
29/10/2020 10:59 <DIR> ..
29/10/2020 10:59 <DIR> .
29/10/2020 10:59 <DIR> Debug
4 arquivo(s) 9.017 bytes
3 pasta(s) 128.838.795.264 bytes disponíveis
==> command 1 is 'NET USER /Add /?'
A sintaxe deste comando é:
NET USER
[nome de usuário [senha | *] [opções]] [/DOMAIN]
nome de usuário {senha | *} /ADD [opções] [/DOMAIN]
nome de usuário [/DELETE] [/DOMAIN]
nome de usuário [/TIMES:{horários | ALL}]
nome de usuário [/ACTIVE: {YES | NO}]
CodePage now is 850
The code
#include <iostream>
#include <windows.h>
int main(int argc, char** argv)
{
const char* command[] =
{
"DIR .\\*.* /O:D",
"NET USER /Add /?"
};
int originalOCP = GetConsoleOutputCP();
std::cout << "Original CodePage: " << originalOCP << "\n";
SetConsoleOutputCP(65001);
std::cout << "CodePage now is " << GetConsoleOutputCP() << "\n";
for (int i = 0; i < sizeof(command) / sizeof(char*); i += 1)
{
std::cout <<
"\n\n\t==> command " <<
i << " is '" <<
command[i] << "'\n\n";
system(command[i]);
};
SetConsoleOutputCP(originalOCP);
std::cout << "CodePage now is " << GetConsoleOutputCP() << "\n";
return 0;
}
Learning about the /proc/ directory today, in particular I'm interested in the security implications of having all the information about a process semi-publicly available, so I wrote a simple program that does some simple whatnot that allows me to explore some properties of the /proc/ directory:
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
extern char** environ;
void is_linux() {
#ifdef __linux
cout << "this is running on linux" << endl;
#endif
}
int main(int argc, char* argv[]) {
is_linux();
cout << "hello world" << endl;
int fd = open("afile.txt", O_RDONLY | O_CREAT, 0600);
cout << "afile.txt open on: " << fd << endl;
cout << "current pid: " << getpid() << endl;;
cout << "launch arguments: " << endl;
for (int index = 0; index != argc; ++index) {
cout << argv[index] << endl;
}
cout << "program environment: " << endl;
for (char** entry = environ; *entry; ++entry) {
cout << *entry << endl;
}
pause();
}
Interestingly though (to me anyway), when I check the file-descriptors folder (/pid/<PID#>/fd), I see this:
root#excalibur-VirtualBox:/proc/1546/fd# ls -l
total 0
lrwx------ 1 root root 64 Nov 7 09:12 0 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 1 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 2 -> /dev/null
lrwx------ 1 root root 64 Nov 7 09:12 3 -> socket:[11050]
why do the file descriptors point to /dev/null? Is that to prevent user's from being able to inject content into a file without actually being the process itself, or am I off base on that? And even more curious, why does the file descriptor to an open file point to a socket? That seems really odd. If anyone can shed some light on this for me, I would really appreciate it. Thanks!
You are definitely looking at the wrong /proc directory (for other PID or on another computer). The contents of /proc/<pid>/fd for your program should look like here:
lrwx------ 1 user group 64 Nov 7 22:15 0 -> /dev/pts/4
lrwx------ 1 user group 64 Nov 7 22:15 1 -> /dev/pts/4
lrwx------ 1 user group 64 Nov 7 22:15 2 -> /dev/pts/4
lr-x------ 1 user group 64 Nov 7 22:15 3 -> /tmp/afile.txt
Here we can see that file descriptors 0, 1, and 2 are shown as symbolic links to the pseudo terminal in which the program is running. It could be /dev/null if you started your program with input, output, and error redirection. The file descriptor #3 points to the file afile.txt which is currently opened.
Hi I have txt files like this :
Directory of C:\Users\RDep
01/09/2014 05:10 PM <DIR> .
01/09/2014 05:10 PM <DIR> ..
01/07/2014 09:32 PM 569 GNU CLISP 2.49.lnk
01/07/2014 09:38 PM 493 lsp 23.fas
01/07/2014 09:38 PM 28 lsp 23.lib
01/07/2014 09:35 PM 35 lsp 23.lsp
01/09/2014 05:02 PM <DIR> Sigma
01/09/2014 05:10 PM <DIR> Sxz
4 File(s) 1,125 bytes
Directory of C:\Users\RDep\Sigma
01/09/2014 05:02 PM <DIR> .
01/09/2014 05:02 PM <DIR> ..
0 File(s) 0 bytes
Directory of C:\Users\RDep\Sxz
01/09/2014 05:10 PM <DIR> .
01/09/2014 05:10 PM <DIR> ..
01/09/2014 05:10 PM 0 Zop.txt
1 File(s) 0 bytes
Total Files Listed:
5 File(s) 1,125 bytes
8 Dir(s) 46,734,090,240 bytes free
I just know the parent directory Is : C:\Users\RDep\ (It depend on user choice and may change during run time) . And the structure Is sth like above code .
Those other names and formats are unknown ( zop.txt -lsp 23.lsp ,... )
Now I want to locate :
C:\Users\RDep
lsp 23.fas
lsp 23.lib
lsp 23.lsp
C:\Users\RDep\Sigma
Zop.txt
C:\Users\RDep\Sxz
I cant find any useful pattern for finding these strings !
Any idea ?
I think 'Regular Expression' is good method, however, in your file, matching pattern is quite simple. So, I just use find function of std::string.
This code is just a little code snippet. So, there are several improvements points, also.
For example, through using vector<string> for finding pattern, it will be easier to add finding patterns and handle the patterns.
//Originally, each data is from your file.
//but, this is just sample.
std::istringstream input;
input.str("Directory of C:\\Users\\RDep\n"
"01/07/2014 09:32 PM 569 GNU CLISP 2.49.lnk\n"
"01/07/2014 09:38 PM 493 lsp 23.fas\n"
"01/07/2014 09:38 PM 28 lsp 23.lib\n"
"01/07/2014 09:35 PM 35 lsp 23.lsp\n");
//to find pattern
std::string directory_pattern = "Directory of ";
std::string lsp_pattern = "lsp ";
std::size_t found = std::string::npos;
for (std::string line; std::getline(input, line); ) {
found = line.find(directory_pattern);
if (found!=std::string::npos)
{
//write on file for 'directory'
std::string directory_name(line, found + directory_pattern.length() );
std::cout << "\nDirectory : " << directory_name;
continue;
}
found = line.find(lsp_pattern);
if (found!=std::string::npos)
{
//write on file for 'lsp'
std::string lsp_name(line, found );
std::cout << "\nlsp : " << lsp_name;
continue;
}
}
Above code will print like below,
Directory : C:\\Users\\RDep
lsp : lsp 23.fas
lsp : lsp 23.lib
lsp : lsp 23.lsp
Try something like:
bool foo(char* filename)
{
FILE* f = fopen(filename, "r");
if(f == NULL)
return false;
char buffer[1024];
char pattern[] = "C:\\Users\\RDep";
while(fgets(buffer, 1024, f) != NULL)
{
if(strstr(buffer, pattern) != null)
{
// Found pattern!
}
}
return true;
}
I am looking for a complete i18n gettext() hello world example. I have started a script based upon A tutorial on Native Language Support using GNU gettext by G. Mohanty. I am using Linux and G++.
Code:
cat >hellogt.cxx <<EOF
// hellogt.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
#include <cstdlib>
int main (){
char* cwd = getenv("PWD");
std::cout << "getenv(PWD): " << (cwd?cwd:"NULL") << std::endl;
char* l = getenv("LANG");
std::cout << "getenv(LANG): " << (l?l:"NULL") << std::endl;
char* s = setlocale(LC_ALL, "");
std::cout << "setlocale(): " << (s?s:"NULL") << std::endl;
std::cout << "bindtextdomain(): " << bindtextdomain("hellogt", cwd) << std::endl;
std::cout << "textdomain(): " << textdomain( "hellogt") << std::endl;
std::cout << gettext("hello, world!") << std::endl;
}
EOF
g++ -ohellogt hellogt.cxx
xgettext -d hellogt -o hellogt.pot hellogt.cxx
msginit --no-translator -l es_MX -o hellogt_spanish.po -i hellogt.pot
sed --in-place hellogt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/'
sed --in-place hellogt_spanish.po --expression='s/PACKAGE VERSION/hellogt 1.0/'
mkdir -p ./es_MX/LC_MESSAGES
msgfmt -c -v -o ./es_MX/LC_MESSAGES/hellogt.mo hellogt_spanish.po
export LANG=es_MX
ls -l $PWD/es_MX/LC_MESSAGES/hellogt.mo
./hellogt
strace -e trace=open ./hellogt
The program compiles, the text is extracted, Spanish file is created, modified and binary created but hellogt still displays English. The trace shows no evidence of looking in the current working directory for es_MX nor any references to LC_MESSAGES directory.
Your problem is that hellogt.mo is in the wrong location - your program isn't actually opening it. You can tell this by using strace to trace open syscalls:
strace -e trace=open ./hellogt
...
open("/tmp/.//es_MX/LC_MESSAGES/hellogt.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/tmp/.//es/LC_MESSAGES/hellogt.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
You can affect where gettext looks for message catalogs with the LOCPATH environment variable, but if you move it to where gettext is attempting to load it from your example works:
mkdir -p es/LC_MESSAGES
cp hellogt.mo es/LC_MESSAGES
./hellogt
hola mundo
cat >hellogt.cxx <<EOF
// hellogt.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
int main (){
setlocale(LC_ALL, "");
bindtextdomain("hellogt", ".");
textdomain( "hellogt");
std::cout << gettext("hello, world!") << std::endl;
}
EOF
g++ -o hellogt hellogt.cxx
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
msginit --no-translator --locale es_MX --output-file hellogt_spanish.po --input hellogt.pot
sed --in-place hellogt_spanish.po --expression='/"hello, world!"/,/#: / s/""/"hola mundo"/'
mkdir --parents ./es_MX.utf8/LC_MESSAGES
msgfmt --check --verbose --output-file ./es_MX.utf8/LC_MESSAGES/hellogt.mo hellogt_spanish.po
LANGUAGE=es_MX.utf8 ./hellogt
Here is a description of the files created by the above:
hellogt.cxx C++ source file
hellogt Executable image
hellogt.pot Extracted text from C++ source file (portable object template)
hellogt_spanish.po Modified text for Spanish with translations added (using sed)
es_MX.utf8/
LC_MESSAGES/
hellogt.mo Binary translated text for Spanish used at run-time
Here is a description of gettext from Fedora Project. It is simple to follow. But it is in C.
http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext