How to make C++ program a Terminal program (UNIX) - c++

I wrote this simple C++ program to compare two strings for a match. Although basic, it's very useful to me as I often need to verify details multiple times a day.
I want to initiate the program with a command name e.g. check4match (the program name) so I can run the program in the terminal.
#include <iostream>
#include <string>
using namespace std;
void match(string, string);
int main() {
string addrOne, addrTwo;
cout<<"Insert str one: "; cin>>addrOne;
cout<<"Insert str two: "; cin>>addrTwo;
match(addrOne, addrTwo);
return 0;
}
void match(string addrOne, string addrTwo){
if(addrOne == addrTwo)
cout<<"SAFE: strings match";
else
cout<<"WARNING: N0 match found";
}

Ok, as always relatively simple in the end. So chmod a+x didn't work to make the cpp program executable. Simple make filename (without the .cpp extension).
Then I moved the newly made .exe file to /usr/local/bin. I had to drag & drop the file, as moving via terminal command wasn't allowed, even with sudo.

Related

I can't read from a file

I wrote a code to make "text1.txt" file. It worked correctly, then I've been trying to read from the file, but every time is_open() function doesn't return true. Even so I copied other codes in the way exactly they are in different compilers, but it never works. How will I solve this:(
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file1("text1.txt");
string str;
if(file1.is_open()){
while(getline( file1, str)){
cout<<str;
}
}
else
cout<<"the file is not open"<<endl;
return 0;
}
How are you running your program?
The most common cause of this I've seen is that you're running your program inside an IDE (like Visual Studio), and your current directory isn't where you think it is.
Try putting in the full path to the file and see if your problem disappears.

how to navigate to a user input site on OSX?

I'm pretty new to C++ and I'm trying to write a simple function to take user input and navigates to the site, adding the https://www. at the beginning of the string, but I cant quite figure out how to call the link within the quotes (or otherwise) within the system function. I come from python so I'm used to being able to use an f string for something like this, and I'm not sure if there's a C++ equivalent, here's my code:
#include <fstream>
#include <iostream>
using namespace std;
void openChrome(string site){
string link = "https://www." + site;
system("open -a 'Google Chrome' //link//");
cout << link;
}
int main()
{
openChrome("apple.com");
}
the cout correctly outputs the full site link, I tried moving the link var outside of the quotes, and that throws an error, so is there something obvious that I'm missing?
EDIT: The issue I'm having isn't with the string concatenation, rather if I try to call the link variable there, it just outputs as the literal since it's in quotes and interpreted directly by the terminal, and throws an error that says 'link' isn't a valid link
I made a few changes to your program:
#include <fstream>
#include <iostream>
using namespace std;
void openChrome(string site) {
string link = "https://www." + site;
string script = "open -a \"Google Chrome\" " + link;
const char *command = script.c_str();
system(command);
cout << link;
}
int main()
{
openChrome("apple.com");
}

Reading from ifstream until file ends

I'm writing simple console application in cpp but none of my approaches to write it were succesful. I'm trying to read row after row from ifstreamed file until the file ends.
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void lowtempbin(string inpfile){
ifstream wyciag(inpfile.c_str());
string row_temp_bin;
int i=0;
while(getline(wyciag, row_temp_bin)){
i++;
cout<<i;
}
}
int main(){
lowtempbin("danesystemy.txt");
return 0;
}
Why the program doesn't enter while loop, and if getline does load nothing, then whole function should return 0? And then code after while is executed (not inside). I'll add that I pass as the argument to lowtempbin()inside main, name of the file that is in the same directory as executable (in code:blocks /project/bin/Debug). Also when I debug the application, while loop is never executed, as if getline returns negative value.
Code shoud cout all numbers, one for every row, but it just returns 0;
The program compiles and produces the expected result when ran.
Your problem seems to be one of the following:
The filename is incorrect.
The file is not in the same directory as the executable.
The file doesn't exist at all.

NetBeans C++ will BUILD but not RUN (exit value 127) when I use a "string" command in my code

Hey i'm pretty new to coding and I can't seem to get a very basic string program to work. Here is my code:
# #include <string>
# #include <iostream>
using namespace std;
int main() {
string name;
cin >> name;
string message("hi");
cout << name << message;
return 0;
}
This is a very generic example but whenever I run it in NetBeans 8.1 it will build but not run and give me this:
Process is started in an external terminal ...
RUN FAILED (exit value 127, total time: 352ms)
Any other file I run will work as long as it does not contain a string command. I figure it must be something with the settings in NetBeans. I've tried using std:: etc etc but it doesn't fix the problem. Any tips/advice would be much appreciated!
You should change the include statements to have only one # symbol preceding them.
#include <string>
#include <iostream>
This would hopefully fix your error; the rest of the code seems fine.

A running executable that modifies itself

I have a Visual Studio 2008 C++ project for Windows CE 5 where I would like the currently running executable to modify itself.
Specifically, I would like to be able to read/write some data stored within the exe file itself. I do not need (or wish to) modify executable code.
In regular windows, I could use a string resource and the UpdateResource function, but that doesn't exist in WinCE.
CreateFile, unfortunately, fails because the file is already in use.
Does anybody have any other suggestions?
First, why do you need to do this? You should be able to do this with other methods.
I'm not particularly familiar with Windows-CE, but if you need to, you can probably copy the file, edit the copy, delete the first, and then run the other. That's an inefficient way, but if you only need to do it once or twice in the span of the program and speed isn't a concern, I guess you could do it:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char * argv[]) {
// Check if this IS the copy:
if (argv[0].find(argv[1]) != string::npos) {
system("taskkill -IM myOLDfile.exe"); // Stop the old one running,
system("del myOLDfile.exe"); // Then delete it.
}
ifstream myself(argv[0]); // argv[0] is the program itself
string fullcode;
string line;
if (file.is_open()) {
while (file.good()) {
getline(myself, line);
line.append("\n");
fullcode.append(line);
}
}
myself.close();
// Do whatever you need to do to the code here.
ofstream newcode("myNEWfile.exe");
newcode.write(fullcode);
newcode.close();
system("myNEWfile.exe myNEWfile.exe"); // Starts new file. Also, not a typo.
}
Good luck on your project!