run cmake with a txt file - c++

When I run my C++ program I need it to open a text file stored in my root directory. How can I make CMake to execute the program I have written with the text file?
When I build my program with Makefile alone, I use the command
./"executable" src/"txt file"

Honestly, by far the simplest would be to just modify your main function. As it stands you main function must be grabbing the filename from the command line arguments. Something like this:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
What you probably should do is to just modify that file to use some default filename when no argument is provided:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argc < 2 ? "/root/something.txt" : argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
Depending on how complicated you want to get, you could also let that filename be specified in your CMakeLists. Just add a definition like
set(DEFAULT_FILENAME "/root/something.txt")
target_compile_definitions(my_target PRIVATE "DEFAULT_FILENAME=\"${DEFAULT_FILENAME}\"")
and then take the filename like a macro in your main function:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argc < 2 ? DEFAULT_FILENAME : argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
To summarize... It sounds like you want to have one executable to be built with the filename into it. The aforementioned approaches will accomplish that.
However, there is a much simpler solution... just create a script that runs the file you want. For example, just throw your code
./"executable" src/"txt file"
into a script run.sh
Then make that script runnable (assuming Linux) with
chmod +x run.sh
and just run that script:
./run.sh

Related

Why doesn't this open the file in the directory of the program?

I have this short program:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
int main (int argc, char * argv[]) {
std::string homedir = std::getenv("HOME");
std::string filename = argc > 1 ? argv[1] : (homedir + "/" + "file");
std::cout << homedir << std::endl;
std::cout << filename << std::endl;
std::fstream file;
file.open(filename, std::ios::out);
file << "Yo yo waddup" << std::endl;
file.close();
return 0;
}
When I supply no arguments, it opens a file in the users home directory. That of course makes sense. But when I run it from a different directory like this:
$ ./folder/hometest examplefile
The program creates "examplefile" in my current directory instead of the directory where the program is.
Why exactly is this happening?
Why exactly is this happening?
The program is behaving just as expected.
The file is opened relative to the current work directory, not where the executable is located.
If it didn't work that way,
All your programs will have to work with absolute paths, or
The location of the program will be flooded with files. First, that might not be possible because of permissions issue. Second, in a multi-user system, users will end up trying to create the same file names/directories.
Neither of the above is desirable.

Reading multiple files in directory in C++

Hi I am new to c++ and was wondering if we could read from multiple files all saved in a single directory. Basically I have a folder consisting of a number of text files and I want my program to read the files. The thing is the folder can be updated (new files added or existing files deleted) so cannot specify in the program the number of and name of files to read (I want to read all the files). So is there a way to do this?
#include <iostream>
#include <fstream>
#include <string>
void run_with(const char* filename) {
std::ifstream fi(filename);
std::string s;
fi >> s;
std::cout << filename << " : " << s << std::endl;
}
int main(int argc, char** argv) {
for (int i = 1; i < argc; ++i) {
run_with(argv[i]);
}
Here you go. Its bassically opening one file but you open it multiple times.

C++ Execute a function through cmd or shell

First thing,sorry my bad english and any mistakes in asking.
I've searched it a lot,but i was not able to explain in simple words.
I work with Linux servers and command line, i'm used to calling programs through it like
./program foo -u adm -p 123
But i always wondered how make programs to act like that,i mean call a specific function and write parameters without needing to open program itself.
In other words.
If i code a C++ like that,and compile
#include <iostream>
using namespace std;
void SayHello(string Name)
{
cout << " Hello " << Name;
}
how can i call it through the command line like
./Program SayHello CARLOS
Sorry about my ignorance,but it's something that i want to learn.
Thanks for your attention
If you want to call a function of your program based on the arguments, you could do something like:
int main(int argc, char* argv[]) {
if(argc > 2){
if(strcmp(argv[1], "SayHello") == 0){
SayHello(argv[2]);
}
}
return 0;
}
Of course this is just a sketch and i can be improved if what you want to achieve is more complex.
You could also build a more dynamic solution if you want other functions than the "SayHello" one to be callable too.
int main( int argc, char** argv )
Here argc refers to the number of argument (arg count)
argv refers to the argument array ("char*" array) (arg value)
Calling your program from command line will result in a main entry with these parameters ; it remains to parse them and launch the command accordingly.
void main() {
char *name[] = {
"./program",
"-c",
"foo -u adm -p 123",
NULL
};
execvp(name[0], name);
}
There you go every executable needs a main function which is the entry point for execution.
#include <iostream>
#include <string>
using namespace std;
void SayHello(string Name)
{
cout << " Hello " << Name;
}
int main(int argc, char* argv[])
{
if (argc > 1)
SayHello(argv[1]);
}
To compile this do
$g++ hello.cpp
It should produce a.out on Linux.
To run it
./a.out "World!"

I want to know how to use Xcode properly for C++

I made an Xcode project. Like this:
Mac OS X -> Application -> Command Line Tool
I chose C++
And then I saved it in a folder.
Now I opened it in Xcode, there is a file named main.cpp under the project name title, so I opened that.
So main.cpp contains (location: Desktop/CPP/learn/learn/main.cpp)
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Now I click on File > New File and I selected C++ file without header, I named it main1.cpp
So that main1.cpp now just has
#include <stdio.h>
I replaced that and made main1.cpp to
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "MAIN1DOTCPP!\n";
return 0;
}
Then I ran main1.cpp, And the out put was "Hello World", I have been trying to get the output of the second file, That is "MAIN1DOTCPP" for like 2 hours now, Can someone please help?

Passing file location through main?

I am building a web application / Interface for my C++ program which will be hosted on the server and then using a scripting language ("PHP") I will then execute the program to run.
I am using G++ to compile the code and and I execute the command to run like so: ("./main") now is it possible that I can pass in the file location so then my program can run? So for example like this:
int main(int argc, char *argv[], string* fileLoc)
{
// code
}
Then execute like this ("./main(FILE_LOCATION)")?
Hope someone can help
You should keep the standard main signature int main(int argc, char *argv[]). The filename would be in argv[1], provided you execute it like this:
./main somefilename.txt
//main.cpp
#include <iostream>
int main(int argc, char* argv[])
{
if (argc > 1)
std::cout << argv[1] << "\n";
}
./main Hello_there
Hello_there