Script for running c++ inside Notepad++ without extra white line - c++

I have a C++ script as follows:
NPP_SAVE
cd $(CURRENT_DIRECTORY)
g++ -o "$(NAME_PART).exe" "$(FULL_CURRENT_PATH)"
"$(NAME_PART).exe"
I try to compile my first C++ programming, print Hello, World.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
However, I see a an extra white line
Question:
How to delete it?
It means as following:

Related

C++: Backspace characters not showing in stdout?

I am implementing a shell that has command history. When the user presses the up or down arrow, the text in the current line is replaced with the previous or next command.
The algorithm I'm planning on implementing is fairly simple. If the current command has k characters, I output k '\b' characters to delete the k characters int he current line and then output the selected command to the line.
However, I'm trying to start out with something basic, such as seeing if outputting '\b' characters even works in deleting characters from the current stdout line:
#include <iostream>
#include <queue>
#include <deque>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
cout << "asdf";
cout << "\b\b" << endl;
return 0;
}
The output of the above piece of code is:
asdf
When I expect:
as
Important: I want to delete characters after they are outputted to stdout.
You need ANSI escape codes. Try:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
cout << "asdf";
cout<<"\b\b\b\b\033[J";
return 0;
}
\033 stands for ESC and [J is a parameter to the code. ESC[J clears the screen from the cursor to the end of the screen. For more information: https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences
As #n. 1.8e9-where's-my-share m. mentioned, \b simply moves the cursor back one space, and does not rewrite anything, for most implementations.
I recommend to use ncurses and readline libraries.
Example:
sudo apt-get install libncurses5-dev libncursesw5-dev libreadline-dev
git clone https://github.com/ulfalizer/readline-and-ncurses
cd readline-and-ncurses
make
rlncurses
My environment:
lsb_release -d ; uname -r ; g++ --version | head -1
Description: Ubuntu 20.04.3 LTS
4.4.0-19041-Microsoft
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

How to use file strings as commands in c++

I am trying to make a program on Windows 10 using Visual Studio 2015 that would sim-link certain files to certain locations. I am trying to make a text file with the location of the files, and the sim-link destination to use.
This is an example of the file data that would be in the properties.txt file:
FileLocation: "Z:\Folder\file.txt"
FileMkdirLocation: "Z:\Folder2\file.txt"
I want to use something like system(mkdir "sim-link_file_location" "file_location") by changing the data that is in properties.txt. I want to be able to add more than 1 file, without recompiling the program and writing each command for each file, one by one.
The problem is that I don't know how to make the commands use the data in the file.
EDIT: I managed to find out a way, but I get errors when compiling the program. I use this code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
//initialization of Properties File used
ifstream PropertiesFile ("PropertiesFile.txt");
int main()
{
//initialization of variables used
int input_option;
char FileLocation[256], Command[]="mklink ";
// string FileLocation, Command;
PropertiesFile >> FileLocation;
/* switch (input_option)
{
case "add all mods":
}
*/
cout << "FileLocation: " << FileLocation;
cout << endl;
strcat(Command, FileLocation);
Command[strlen(FileLocation)] = '\0';
cout << Command;
cout << endl;
//system(command);
system("pause");
return 0;
}
I know that i haven't used all variables yet.
It tells me that "strcat" is deprecated and to use "strcat_s" instead, and when i replace it with that, I get
"Debug Assertion Failed - Expression: (L"Buffer is too small" && 0)"
I had to make the "Command" char bigger than "FileLocation" because then strcat_s would not be able to copy the content. After that the program worked fine, and there were no other Assert Errors.
The command to create a soft link in linux is: ln -s <source> <destination>
You can use this in a system(""); call, BUT before you continue in your code, you will have to make sure that the kernel finished executing this command.
After that you can read the link as if it was the original file.

writing a file in c++ with Xcode and running with terminal

I'm very new to programming and i bought myself a self help book but the book is designed for windows. I've mostly been able to translate so far but i'm stumped on writing/appending files and running them through terminal. I was wondering if someone could translate these lines for me. these lines are what I'm told to type in command prompt/terminal.
C:\MyPrograms> c++ write.cpp -o write.exe
C:\MyPrograms> write
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string poem = "\n\tI never saw a man who looked" ;
poem.append("\n\tWith such a wistful eye") ;
poem.append("\n\tUpon that little tent of blue") ;
poem.append("\n\tWhich prisoners call the sky") ;
ofstream writer("poem.txt") ;
if (! writer)
{
cout << "Error opening file for output" << endl ;
return -1 ; //signal an error then exit the program.
}
writer << poem << endl ; // write output
writer.close() ; // close filestream.
return 0 ;
}
This is the program i am trying to run named write.cpp please help thanks!
On OS X, the first line in Terminal would be:
g++ write.cpp -o write
The second line would be:
./write
The first line compiles your code and creates an executable called write. The second line runs the executable.

How can I enter continuously by another C++ program in C system / popen command?

I want to build a compile system in an online judge system.
Environment: Ubuntu 12.04 LTS, g++ version 4.9
My workflow is "Compile cpp" -> "Execute it" -> "Record message".
But I got some problems when "the cpp file exist 'scanf' or 'cin' commands".
Because this is a auto-compile & run program, there is an other input need to load. (Is a string from function call not enter in terminal by myself)
My problem
How can I run the executeCommand (below code in compiler.cpp), using the string input (below too) to enter for this program. If the executed program exist any scanf, cin or other commands.
compiler.cpp
This is system command version, can replace to popen command too.
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char ** argv) {
// Compiler one cpp file.
string compileCommand = "(g++ --std=c++11 ./main.cpp -o ./main.out) 2> main.err";
system(compileCommand.c_str());
// Execute this program.
string executeCommand = "(time timeout -k1s 0.01s ./main.out) > result.txt 2> time.txt";
system(executeCommand.c_str());
// I want the above main.out will scanf from this string.
string input = "Hello world, this is first line.\nThis is second line.";
return 0;
}
main.cpp
#include <stdlib.h>
#include <stdio.h>
int main () {
char str[256];
scanf("%s", str);
printf("%s\n", str);
return 0;
}
You probably need popen(3) (and you flagged your question as such).
FILE*pcmd = popen("time ./main.out", "w");
if (!pcmd) { perror("popen"); exit(EXIT_FAILURE); };
fprintf(pcmd, "Hello world, this is first line.\n");
fprintf(pcmd, "This is the second line.\n");
fflush(pcmd);
int bad = pclose(pcmd);
if (bad) {fprintf(stderr, "pclose failed %d\n", bad); };
Be aware of code injection issues, in particular when passing a computed command to popen or system
You might need some event loop around poll(2). Then use fork, execve, pipe and other syscalls(2) explicitly, so read Advanced Linux Programming
All you need is a pipe, system( "echo YOUR_STRING | ./main.out " )

Xcode reading chars from file in c++

I have the following code to read a character from a file:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[])
{
ifstream f("text.txt");
char c;
f.get(c);
cout << c << endl;
return 0;
}
and my text.txt file contains:
hello world!
However, when I run this on Xcode, I get an inverted question mark as the output.
It works fine on terminal, but not on Xcode. Does anyone know why this happens?
I'm using Xcode to debug some code, but I cant do that anymore because this problem is causing a lot of other errors in my program.
Your text.txt file will not be at the executable path.
Go to your Build Phases - > Copy Files -> Add Your text file
Make sure that:
Destination should be Products Directory
Copy only when installing should be unchecked