How to execute gvim command using c++ - c++

I need to write a code to jump to a particular given line number in gVim. But I need to do this using a C++. Is there any way to do it?
There are ways to open the gvim by executing the Linux commands using C++ code. But is there a way to execute the gVim command using the same C++ code?

Do you mean the function system that runs linux commands? You can use it for any command and command parameters. Bellow is the example, modify it according to your requirements.
int line_no = 576;
std::string file_name;
std::stringstream command;
command << "gvim +" << line_no << " " << filename;
system(command.str().c_str());
It will execute gvim +576 file_name, that opens a file at a particular line in gvim.

You can ask vim to run commands from the command line. See the list of vim command line options.
To position cursor on the line num run vim +num filename, for example vim +578 filename jumps to line 578 in the named file. vim + filename jumps to the last line.
Search: vim +/pattern filename will position cursor on the first line containing "pattern".
Other commands: vim -c command filename executes the ex (:) command. You can run up to 10 commands (up to 10 -c options).

Related

How do I use a text file for stdin input in DEV C++ IDE?

I am trying to use a .txt file for my stdin in a c++ program. Essentially, I am just trying to find where in this IDE (Dev C++) I would be able to specify the file I want to use for program input.
I thought that I would be able to read file input here:
But this doesn't seem to be working. I have tried with a space between the 1 and the file name and with quotes around the file name.
I know my program allows for the file to be read properly because when running it in the command line it outputs the first line of the stdin file, test01.txt.
The command I am using to run the program in cmd line is: ./prog 1 < test01.txt where 1 is a case number for a switch statement and test01.txt is input used by that switch statement.
How would I mimic this command line call in Dev C++ IDE? The test01.txt file is part of my project files...

Ubuntu shell script: Using >> the output txt file is not opening

I using the following python script to create a shell script:
with open("decode_JOURNAL2017.sh", "a") as myfile:
levels_no = 16
#myfile.write("#!/bin/bash\n")
for x in range(0, levels_no):
myfile.write("/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx "+str(x)+" -o"+str(x)+" /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt\n")
Then I transfer the following created script to an ubuntu server:
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 0 -o0 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_0_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_0_dec.txt
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 1 -o1 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_1_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_1_dec.txt
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 2 -o2 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_2_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_2_dec.txt
When I run it, the txt files are created but when I double click to one to open it I get this message:
Can't create file 'C:\Users\admin\AppData\Local\Temp\scp43940\home\zinonas\decode\pirkagia_10b_lowdelay_P_level_0_dec.txt
'.
System Error. Code: 123.
The filename, directory name, or volume label syntax is incorrect
I can't even transfer this txt file to my windows desktop via winscp.
The yuv files are working properly!
EDIT: When I copy/paste the content of the shell script to the terminal, the txt files are working properly.
EDIT 2: When I rename the file and remove txt and add it again, the file opens properly...
Do you know how to fix this?
Thank you in advance!
You probably have Windows line endings in the script file – at least that's why I get from the error message if you pasted it correctly.
Run dos2unix scriptFilename.sh or sed -i 's/\r//' scriptFilename.sh to remove the Windows line endings from the script.
For Python scripting refer to this Question&Answer: How to write Unix end of line characters in Windows using Python
I found the solution.
While creating the shell script, next to the name of the txt file I had:
...
>> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt\n")
so the new line character \n want next to txt. That was the problem. I added a new space between them and the problem solved!
My new code now reads:
...
>> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt \n")
Is there a "rogue" CR at the end of the file name?

Want to run a cpp executable in a new terminal and then send a file into the input stream

I would like to run a c++ executable in a new linux terminal, which I am doing using:
xterm -e executable options &disown
and this works. However, I also need to parse a text file through the command line. Normally, the file would be parsed by:
./executable options < inputFile.txt
and then the file is handled by the c++ code using this function:
void parse_lines(istream &in){
verify_version_number(in);
read_variables(in);
...
}
However the following line does not work:
xterm -e executable options < inputFile.txt &disown
How can I run the executable in a new terminal and then send the contents of inputFile.txt into the istream?
Thanks!
If you put quotes around the command, it will be sent to the shell as a single command, and the special characters < and & will be interpreted in the shell running within xterm rather than in the shell where you start xterm:
xterm -e "executable options < inputFile.txt &disown"
Try x-terminal-emulator instead of xterm. Perhaps it works.

C++ parsing a command line

I am trying to run two of the command lines in a C++ program, but landing in a strange error. The command lines that i want to run are
vlc -vvv dshow:// :dshow-vdev='USB Video Device' :dshow-adev="" :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display} :sout-keep
vlc -vvv udp://#localhost:1234:network-caching=50
They both run fine on command prompt. But when in use a C++ system shell call to run these, the first one fails, where as second one works. The way i run these in C++ is as follows:
system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\" :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");
system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://#localhost:1234:network-caching=50");
The first command throws an error "G:/Program is not recognized as an internal or external command, operable program or batch file.", which is strange since the way both commands handle path to the file is the same. Please let me know the reason for this.
The computer is running on Windows XP and i am using Microsoft Visual Studio 2010.
Replace
system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\" :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");
system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://#localhost:1234:network-caching=50");
with
system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\" :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");
system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv udp://#localhost:1234:network-caching=50");
using system() to run commands has the following caveats and switch requirements for what you are attempting (from cmd documentation) as it effectively runs cmd /C *mycommandhere*:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
1. If all of the following conditions are met, then quote characters
on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()#^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.
2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
If you reformat your commands as:
system("cmd /S /C \"\"c:\\testDir\"\"\\dir with spaces\"\"\\myexe.exe");
that should work:) ( in the previous version I had forgotten some escape characters).
There is also a related question here.
Or if it is for windows only You could utilise ShellExecute or ShellExecuteEx with some documentation on the aforementioned at msdn, here
Hope this helps, as I just lost connection to my home environment and cannot get a fixed version for my previous fix at present, will do so tonight however.

C++ program cannot find latex command with system() function call

I am writing a C++ program which has to automatically generate some data to be used by students in an integrated exercise. I have already exported this data to .tex files and would also like the C++ program to be able to compile these tex files automatically.
Usually I would compile tex files from the command line by doing the following:
$ latex file.tex
$ latex file.tex
$ dvipdf file.dvi
So I tried doing the following in my C++ code (directory and filename are both strings):
//Move to the location where I created the files
string mycommand = "cd ";
mycommand += directory;
system(mycommand.c_str());
//Compile latex
mycommand = "latex " + filename + "_main.tex";
system(mycommand.c_str());
system(mycommand.c_str());
//Create .pdf
mycommand = "dvipdf " + filename + "_main.dvi";
system(mycommand.c_str());
Which then produces the following error message on the terminal output:
sh: latex: command not found
sh: latex: command not found
sh: dvipdf: command not found
I have searched this online but I have failed to find a solution for this problem, though I believe it is likely to be something very simple.
I am working on OSX and have the following version of latex installed:
pdfTeX 3.1415926-2.4-1.40.13 (TeX Live 2012)
kpathsea version 6.1.0
All help is greatly appreciated!
First, the path to the programs latex and dvipdf needs to be in your PATH environment variable.
Second, the calls of the shell via system are totally independent (in fact a new instance of the shell is started each time). So if you switch the directory in one, this does not affect the others. Switch instead the current directory of your program via:
chdir(directory.c_str())
You need for this
#include <cunistd>
using namespace std;
at the beginning of the file.
Please note that system calls with command lines depending on input parameters can be easily exploited to run arbitrary commands if the parameters (in your case the filename) are not carefully checked. As you do not have quotes, the program fails if there are e.g. whitespaces in the filename.