Is it possible to treat user input as lines of code in C++?
Or write it to a text file, then run that as c++ code?
I'm writing my own version of matlab, and right now it can do a lot of the numerical stuff, but only if I write the commands in like a script in main. Is there a way I could do it like matlab's command line?
Yes, there are many libraries that allow for scripting like Lua or Chaiscript which will allow you to take a file of code and execute it. There's also the advantage of being able to make changes to the code and not having to recompile each time.
Related
I can't seem to find the answer, how do you create a new file in Ocaml? Do you edit your file in the terminal? Where does the source code appear?
I think you're asking how to write code in OCaml, i.e., how to create an OCaml source file. (This isn't completely clear. You could be asking how to write OCaml code that creates a file.)
The details of creating OCaml source depend on your development environment, not on the language itself. So there is no one answer.
The general answer is that you can use any tool you like that knows how to create a text file. If you like working from the command line (as I do) you can work in a terminal environment and run some kind of vintage text editor from the last millennium (as I do). If you like a GUI environment, you can run some kind of "programmer's editor" from the current millennium, or really any kind of editor that creates basic utf-8 files (or even ASCII files).
Generally the editor will have to be told where to store the files that you edit. You would probably want to make some kind of folder for the project and make sure you store the text files in there.
I hope this helps! If you have any programmers nearby, they can probably get you started a lot faster than asking on StackOverflow.
I'm new with Matlab-C combination and I'm very lost. Any help will be very thankful.
Essentially, what I want is to use a C++ code from a simple matlab script. I've created some variables' values in matlab in order to give them as inputs to the C++ code and then capture theirs output of that C++ code and loading them into matlab.
I have a main.cpp and a makefile into the same folder. (they work well)
I've created a simple test.m file to create the variables' values for the C++ code and then, recover their outputs.
What I've done is to run the makefile using the mac terminal by typing
make
in order to create an executable called "benchmark". Each time I need to change a tiny variable's value from my matlab script, I must go to the terminal again, type
./benchmark
and then goes again to the matlab and run the test.m file to update the inputs of the C++ code. As you see, this is not the optimal way to work it.
So I'm wondering how to pass values created in matlab and in the same script updating the ./benchmark executable.
Thanks in advance guys.
I'm doing some programming problems from the previous year competition and in the problem text there is only one case, which is simple so I can just rewrite it when testing.
Now, I also have a folder with a bunch of .in and .out files, in format '01.in, 01.out, 02.in, 02.out, etc'.
Is there a way to somehow take one of those .in files and automatically use all the lines of it as input without making changes inside my program but rather doing it directly from the command line?
Thanks
Assuming linux:
cat *.in | yourprogram
On Windows you'd use type instead of cat.
I assume your program takes in and processes the agruments (argv[]) passed to it already. If this is the case, one way could be to write a simple wrapper program (in Python for example) which opens the required .in files, reads the lines in it and invokes your C++ program by passing the required lines as input to them.
Then you can execute this python program or make changes to it as required.
I am very new to c++.
I read some code samples of some experienced c++ coders, and noticed that they ran a file starting with scanf, that read through a large input.
However, they didn't use any specified ifstream to read in a file, they seemed to just run scanf as if this entire input .txt file was already in the stream.
Is this because they are using some special IDE where you can load in input file into the standard stream?
I am simply trying to run their code to duplicate something they are doing, but I need some way to read in a 5000+ line text file and then run scanf on it.
How do I do this. I'm not using any sort of IDE, just notepad++. If someone recommends an IDE, i'll take it, but otherwise would just like advice on how to do this. Thanks.
For a start, if they're still using scanf(), they are not experienced C++ programmers. If they were experienced, they would be using the C++ I/O facilities rather than the legacy C ones.
Having said that, you can quite easily use the functions accepting standard input (either scanf() or cin) if you can do redirection on the program, with lines like:
myProg <myInputFile.txt
someOtherProgWhichProducesOutput | myProg
This "attaches" the file myInputFile.txt to the standard input of the myProg program, or takes the output from another program and feeds it to the input of myProg, and is what makes UNIX and its brethren (including Windows in this context, since it too has redirection and pipelines) so powerful, allowing you to string together complicated pipelines from relatively simple commands.
How do you pass options to an executable? Is there an easier way than making the options boolean arguments?
EDIT: The last two answers have suggested using arguments. I know I can code a workable solution like that, but I'd rather have them be options.
EDIT2: Per requests for clarification, I'll use this simple example:
It's fairly easy to handle arguments because they automatically get parsed into an array.
./printfile file.txt 1000
If I want to know what the name of the file the user wants to print, I access it via argv[1].
Now about how this situation:
./printfile file.txt 1000 --nolinebreaks
The user wants to print the file with no line breaks. This is not required for the program to be able to run (as the filename and number of lines to print are), but the user has the option of using if if s/he would like. Now I could do this using:
./printfile file.txt 1000 true
The usage prompt would inform the user that the third argument is used to determine whether to print the file with line breaks or not. However, this seems rather clumsy.
Command-line arguments is the way to go. You may want to consider using Boost.ProgramOptions to simplify this task.
You seem to think that there is some fundamental difference between "options" that start with "--" and "arguments" that don't. The only difference is in how you parse them.
It might be worth your time to look at GNU's getopt()/getopt_long() option parser. It supports passing arguments with options such as --number-of-line-breaks 47.
I use two methods for passing information:
1/ The use of command line arguments, which are made easier to handle with specific libraries such as getargs.
2/ As environment variables, using getenv.
Pax has the right idea here.
If you need more thorough two-way communication, open the process with pipes and send stuff to stdin/listen on stdout.
You can also use Window's PostMessage() function. This is very handy if the executable you want to send the options to is already running. I can post some example code if you are interested in this technique.
The question isn't blazingly clear as to the context and just what you are trying to do - you mean running an executable from within a C++ program? There are several standard C library functions with names like execl(), execv(), execve(), ... that take the options as strings or pointer to an array of strings. There's also system() which takes a string containing whatever you'd be typing at a bash prompt, options and all.
I like the popt library. It is C, but works fine from C++ as well.
It doesn't appear to be cross-platform though. I found that out when I had to hack out my own API-compatible version of it for a Windows port of some Linux software.
You can put options in a .ini file and use the GetPrivateProfileXXX API's to create a class that can read the type of program options you're looking for from the .ini.
You can also create an interactive shell for your app to change certain settings real-time.
EDIT:
From your edits, can't you just parse each option looking for special keywords associated with that option that are "optional"?