Visual Studio Code Compilation problem, c++ - 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

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

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

OpenCV 3.2.0, Visual Studio 2015, Windows 7

I installed the pre-built libraries OpenCV 3.2.0 on Windows 7 following the instructions here and I am encountering errors when trying to use them in Visual Studio 2015.
The variable OPENCV_DIR is set correctly:
C:\>echo %OPENCV_DIR%
C:\OpenCV\Build\x64\vc14
C:\>dir %OPENCV_DIR%
Le volume dans le lecteur C s'appelle OS
Le numéro de série du volume est 1234-ABCD
Répertoire de C:\OpenCV\Build\x64\vc14
27/01/2017 17:10 <REP> .
27/01/2017 17:10 <REP> ..
27/01/2017 17:11 <REP> bin
27/01/2017 17:10 <REP> lib
0 fichier(s) 0 octets
4 Rép(s) 19 236 450 304 octets libres
C:\>
And the rules for the project are like there
With the libraries specified as
opencv_calib3d320d.lib
opencv_core320d.lib
opencv_features2d320d.lib
opencv_flann320d.lib
opencv_highgui320d.lib
opencv_imgcodecs320d.lib
opencv_imgproc320d.lib
opencv_ml320d.lib
opencv_objdetect320d.lib
opencv_photo320d.lib
opencv_shape320d.lib
opencv_stitching320d.lib
opencv_superres320d.lib
opencv_ts320d.lib
opencv_video320d.lib
opencv_videoio320d.lib
opencv_videostab320d.lib
But when I try to compile the basic test project written there
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I get the error
1>------ Build started: Project: ImageCorrection, Configuration: Debug x64 ------
1> test.cpp
1>LINK : fatal error LNK1104: cannot open file 'opencv_calib3d320d.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I am really new to compilation and linking on Windows and Visual Studio (I am used to g++ on Linux) so I really have no idea of what I am doing wrong here.
I think that it might have something to do with the dynamic linking, but I do not know neither how to investigate nor how to solve it.
Ant help is most appreciated! :D
OpenCV 3.2 prebuild binaries have just the world lib:
opencv_world320.lib for release
opencv_world320d.lib for debug
That's all you need to link.

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.

Parse directory locations from file in 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;
}