Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I try to understood how shell script interpreter working.
for example i wrote custom shell with c++ :
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string input;
while (1)
{
cout << "prompt:> ";
cin >> input;
if(input=="exit")
return 0;
else if(input=="test")
cout << "You executed test command\n";
else
cout << "Unknown command.\n";
}
}
now i wrote a script like this :
#!/PATH/TO/COMPILED/SHELL
test
wrong_command1
wrong_command2
exit
Actually this script not working and i want to understand what part of my thinking is wrong .
Note: I executed this script on /bin/bash shell .
can i say ,my c++ code is: interactive shell
How interpreters work on shell scripts ? #!/PATH/TO/COMPILED/SHELL
How can fix code or script to activate interpreting feature ?
No idea what that means
If you compile your program to /tmp/a.out and have an executable file script with:
#!/tmp/a.out
test
wrong_command1
wrong_command2
exit
which you invoke on command line as ./script then the shell running the command line will invoke /tmp/a.out ./script. I.e. looks at the shebang, invokes that command and passes the script as its first argument. The rest is up to that command.
There is no interpreting feature in C++, you have to write it yourself, what you have is a good start except you need to read from the passed file argument, not stdin. Also std::getline might come handy.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am currently learning how to use text files, but when i put a condition like the one in the code below, even if the condition is true it won't try the while part. Can anyone help out?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s, d;
ofstream k("t1.txt");
int a;
cin >> a;
if (a > 3)
{
while (k.is_open())
{
getline(cin, s);
k << s;
k.close();
}
}
ifstream r("t1.txt");
while (r.is_open())
{
getline(r, d);
cout << d;
r.close();
}
}
I ran the code in gdb, it runs perfectly. It opens the file, gets the < cr > character from stdin, writes that to the first line which wipes out the first line, then tries to read the first line which is empty so there is no output. Good job 8).
You're just having trouble understanding your own expectations, your code works fine, so just think a little bit more about what you think it's supposed to do.
You'll also need to examine the contents of your text file before and after running the code, and try entering some text in the text file before you start the program, to see what happens to the text.
The getline where you try to read from the console is the issue, that returns without asking for input so it ends up giving you a blank line to write to the file.
From this link:
http://www.cplusplus.com/forum/beginner/39549/
There's a similar question there, and the first of the comments mentions this about the behavior of getline:
std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. .......
That's why it's not "doing anything" - it's not asking you for input, so the program has nothing to output and it looks like it's not working.
(this, by the way, is why programmers are snarky - we have to deal with stupid little behavioral issues of machines and we forget that PEOPLE are not normally like this and that PEOPLE hate being held to these kinds of standards)
I put a second getline in your code right after hte first one and now it asks for and outputs a string that I type in and sticks it in the file.
To run the program in gdb, do the following:
g++ -g < your cpp file > -o < whatever you want the binary to be called >
like this:
g++ -g myfile.cpp -o myrunnableprogram
this creates a binary with symbols that you can open in gdb
then
gdb myrunnableprogram
then in gdb
start
next
next
next
....
these commands will start the program and pause it at the first breakable spot, then each time you type next you will step to the next command and you can poke around at variables to see what's going on.
read up on gdb, it's really useful.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am using C++ to write a music memorize game for my school project, here the Player objects are not being saved in the PLAYER_DATA.DAT file even i tried clear() function
here is the peace of code (as the whole code is 600+ lines)
Player p_dat,plyr;
plyr.getData();
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
while(P_file.read((char*)&p_dat, sizeof(p_dat)))
{
if(nameEqual(plyr,p_dat))
{
P_file.clear();
gotoxy(1,10);
delline();
textcolor(RED);
cout<<"\t\t EXIXTING PLAYER PROFILE FOUND!\n";
int ch = playPanel("It's me", "Change Name");
if(ch == 0)
{
P_file.seekp(P_file.tellg() - sizeof(Player));
GameStarted = 1;
if(c == 1)
Campaign(p_dat,P_file);
else
Endless(p_dat,P_file);
return;
}
else
{
startGame(c);
return;
}
}
}
P_file.clear();
P_file.seekp(0,ios::end);
P_file.write((char*)&plyr,sizeof(plyr));
Just to make it short, The last line of the code is not doing anything the file already exists and of size 0kb
however,
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary)
P_file.write((char*)&plyr,sizeof(plyr));
is saving the file. Please help me.
EDIT 1.1
finally found this line is problematic
P_file.seekp(0,ios::end);
Its working for code, i.e correctly saving objects
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
P_file.seekp(0,ios::end);
while removing
P_file.seekp(0,ios::end);
making the code look like,
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
after the while loop, is not saving the file
again does not save the object
this line is making problem, are there any alternatives or solutions?
Compile with all warnings and debug info (g++ -Wall -Wextra -g with GCC) then use the debugger (e.g. gdb).
Read carefully documentation of C++ IO functions.
Check that your file has been opened correctly; after:
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
add
if (!P_file) { std::cerr << "failed to open PLAYER_DATA.DAT" << std::endl; };
On some systems (e.g. Linux), you might also display strerror(errno).
After some intermediate call to write, consider using flush.
Be sure that your program is started in the correct working directory.
On Linux, you might also use strace(1) to understand the system calls done by your program.
At last, did you consider using some simple database, e.g. with sqlite, or some indexed file, e.g. with gdbm ?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm familiar with the Visual Studio IDE on Windows, and I wanted to give VSCode a shot. For my current situation, I am running VSCode on Ubuntu 64-bit. I installed VSCode, and then installed the C/C++ Extension. Next, I wrote a very simple C++ program that outputs "Hello World", and then asks the user to input their name. The program would then say/output hello to that user. Here's the issue: I am having trouble figuring out how to actually provide the user input to the program. During runtime, I see the cursor blinking in the output panel after the "Hello World" output, but when I press any keys on my keyboard to provide user input, nothing appears, and nothing happens. Any help would be greatly appreciated.
#include<iostream>
#include<string>
using namespace std;
int main() {
string name = "";
cout << "HELLO WORLD" << endl << endl;
cin >> name;
cout << "Hello " << name;
return 0;
}
Better use terminal then the Visual Studio Code Run feature
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
What is the easiest way to repeat a simple system command on multiple CMD's in C++? For instance, how could I repeat this code on multiple terminal windows from my C++ code?
system( ("ping "+ ip +" -t -l 32").c_str() );
From a networking perspective, I don't think that pinging a single system from multiple terminals will be as effective as pinging that system from multiple remote systems.
Anyways, in regards to the easiest way to ping a system from multiple processes... just use the shell directly. Something like:
target=s4
for remotehost in s1 s2 s3; do (ssh -n $remotehost ping $target -t -l 32 & ) ; done
The "remotehost" doesn't have to really be a remote host either. You can just use "localhost" multiple times (instead of multiple names of remote hosts).
Alternatively if you really want to use C++ from a single host:
#include <cstdlib>
#include <string>
int main()
{
const std::string ip = "foo";
for (int i = 0; i < 3; ++i)
{
std::system(("ping " + ip + " -t -l 32 & ").c_str());
}
}
Note the usage of the ampersand (&) character in the input string to the system function. That instructs the shell to run the given task in the background. That way system returns immediately and the ping command basically runs at the same time as the two other instances of the command.
Hope this helps answer your question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to only write the last 50 lines of console output to a file automatically?
So that the file is always overwritten by the latest 50 lines.
You can use shell to modify and redirect the output of your program :
my_program | tail -n 50 > my_file
use simple redirection > if you want to truncate the file or double redirection >> if you want to append it.
Note :
This method only redirect stdout if you need to redirect stderr put 2>&1 after my_program
If you really want to do this in C++, you could store your console output line by line in a container while your program is running and write the last 50 lines to your file when needed.
Write a wrapper for your output function and each time you print a line to the console, add it to a std::queue. If that makes your queue larger than 50 elements, pop the oldest one (just call pop()).
class Logger {
static std::queue<string> lastFifty;
public static void log(const std::string& str) {
lastFifty.push(str);
if (lastFifty.size() > 50) {
lastFifty.pop();
}
std::cout << str;
}
public static void dumpToFile(std::ofstream& file) {
while (!lastFifty.empty()) {
file << lastFifty.pop();
}
}
}