Parse directory locations from file in c++ - c++

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;
}

Related

recursive_directory_iterator c++ no longer finds directories

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

Visual Studio Code Compilation problem, c++

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

Check all files sizes in a path (C++)

I'm trying to loop so that my program can get the weight of all files in a folder, and if the weight of any of these is equal to X, it will do an action, I need to know how I can loop like this, and i have a func to know whats the file size
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
Here's a short example program that demonstrates how to use C++17's <filesystem> library to iterate over a directory. If your compiler is fairly up-to-date, it should support C++17 without issue.
#include <filesystem>
#include <iostream>
int main() {
namespace fs = std::filesystem;
fs::path pwd(""); // Current directory program was executed from
pwd = fs::absolute(pwd);
for (auto& i : fs::directory_iterator(pwd)) {
try {
if (fs::file_size(i.path()) / 1024 > 2048) {
std::cout << i.path() << " is larger than 2MB\n";
}
} catch (fs::filesystem_error& e) {
std::cerr << e.what() << '\n';
}
}
}
This was the contents of the directory:
.
├── a.out
├── fiveKB
├── fourMB
├── main.cpp
└── oneMB
0 directories, 5 files
And information about the files:
drwxr-xr-x 7 user staff 224B Jul 29 22:11 ./
drwxr-xr-x 13 user staff 416B Jul 29 21:59 ../
-rwxr-xr-x 1 user staff 47K Jul 29 22:10 a.out*
-rw-r--r-- 1 user staff 5.0K Jul 29 21:58 fiveKB
-rw-r--r-- 1 user staff 4.0M Jul 29 21:59 fourMB
-rw-r--r-- 1 user staff 450B Jul 29 22:11 main.cpp
-rw-r--r-- 1 user staff 1.0M Jul 29 21:59 oneMB
And finally, the output:
"/Users/user/Documents/tmp/test/fourMB" is larger than 2MB

trying to connect to postgresql via c++ program

Background
I'm running linux... and I'm trying to write a basic little c++ program that connects to a postgresql database.
I'm trying to follow this article
http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
Problem
I've been able to compile the library... and I can see now that I have the following folder on my computer
/usr/local/include/pqxx
But when i try to write some basic code and compile it, I get the following error:
devbox2:/var/abus# g++ testdb.cpp -lpqxx -lpq
testdb.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory
#include <pqxx/pqxx>
^
compilation terminated.
Source Code
Here's what the code looks like:
1 #include <iostream>
2 #include <pqxx/pqxx>
3
4 using namespace std;
5 using namespace pqxx;
6
7 int main(int argc, char* argv[])
8 {
9 try{
10 connection C("dbname=testdestination user=testuser password=testpassword \
11 hostaddr=127.0.0.1 port=5432");
12 if (C.is_open()) {
13 cout << "Opened database successfully: " << C.dbname() << endl;
14 } else {
15 cout << "Can't open database" << endl;
16 return 1;
17 }
18 C.disconnect ();
19 }catch (const std::exception &e){
20 cerr << e.what() << std::endl;
21 return 1;
22 }
23 }
What I've tried so far:
I've been poking around the /usr/local/include/pqxx folder and I can see that there is a file called pqxx... but it doesn't have any extension on it.
Here's a snippet from the ls -lah command for that folder:
-rw-r--r-- 1 root root 637 Dec 8 21:42 pipeline
-rw-r--r-- 1 root root 7.5K Dec 8 21:42 pipeline.hxx
-rw-r--r-- 1 root root 1.1K Dec 8 21:42 pqxx
-rw-r--r-- 1 root root 728 Dec 8 21:42 prepared_statement
-rw-r--r-- 1 root root 8.2K Dec 8 21:42 prepared_statement.hxx
I've also made sure that my PATH includes the /usr/local/include/pqxx folder. This is what my PATH looks like:
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/gcc:/usr/local/include/pqxx:/usr/local/include'
I'm not sure what else I should check. Any suggestions would be appreciated.
Thanks.
To find the include files, you must add an -I option, e.g.
g++ -I/usr/local/include testdb.cpp -lpqxx -lpq
Adding directories to PATH doesn't help here, PATH is for locating executables from the shell.

What do /proc/fd file descriptors show?

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.