This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 2 years ago.
I want store approximate pi number from "approximate" file and use it afterwards, somehow it getting deleted and i can't print it after close method. Here's how i tried to.
string pi;
ifstream piFile;
piFile.open("pi_approximate");
while(piFile){
getline(piFile,pi);
}
piFile.close();
cout << pi << endl;
This will keep reading from the file until it gets a failed read. If the last character read from the file was a newline then that failed read will be immedaitely after clearing the string pi. So after the loop exits, pi will be empty.
Related
This question already has answers here:
"std::endl" vs "\n"
(10 answers)
Closed 2 years ago.
If I have opened an output stream like this:
ofstream to(output_file);
How may I print new line (Looking to support different os)?
to << "\n";
to << "" <<endl;
End of line notation, '\n' is used in most of the systems I know except Windows (MS Dos) which use '\r\n'. What is different in between those two is that '\n' in most systems will return cursor to the beggining of new line, where in MS DOS it will go to the same character as previous line, so if you have something like this:
Hello\nWorld!!!
in most systems it will output:
Hello
World!!!
where as in MS DOS it will output:
Hello
World!!!
So to overcome this issue of different systems treating newline differently we have std::endl, which will place correct notation for the correct system. In my code I might be bad, but I am mostly using '\n', but on the other hand I have not been using Windows as my dev machine that much.
One more point, printing out "" is useless.
This question already has answers here:
Rewinding std::cout to go back to the beginning of a line
(3 answers)
Closed 4 years ago.
How can I rewind std::cout back to beginning of line and insert text without overwriting exiting one ? Can it be done using just standard c++ functions, or do I need low-level OS functions for console to do this ?
EDIT: I'm writing a simple telnet client. So when a message is received it should be appended at the top and user imput should not be overwritten.
No, you can't do this, and it's considered useless in console.
There is a function named std::seekp for all basic_ostream based class. But when you apply this to cout, no effect at all but failbit is set.
Use std::cout << '\xd' this line will output a carriage return which will fulfill your requirement. This this line will overlap your previous entry.
This question already has answers here:
GDB: How to remove a variable from the auto display
(2 answers)
Closed 5 years ago.
I am working on a c++ program on linux ubuntu 16.04 and I've done a tutorial on gnu debugger.
I am having some problems with my code and as I step through it it's easier for me to compare two variables at each part fullPath and argv[1]
But once I get past that particular segment I want to remove argv[1].
I called them with the following:
display argv[1]
display fullPath
But when I try to remove argv[1] with undisplay argv[1] I get an error that reads the following:
warning: bad display number at or near 'argv[1]'
It still continues to display argv[1] unless I exit debugger and start it again without displaying it. Is there a way to fix this?
NOTE
I've also tried delete argv[1] which also doesn't work.
The undisplay command is expecting a list number, not an expression. You can see the list numbers for all your auto-display expressions by typing:
info display
Let's say that argv[1] is assigned item 3 in that list. You would then remove it with:
undisplay 3
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to capture stdout from a system() command so it can be passed to another function
In linux to get the current status of a service I wrote this code segment::
char cmd[100];
sprintf(cmd,"service %s status",argv[1]);
system(cmd);
It is running fine and it shows the output on the console like : mysql is running OR mysql is stopped
But I need this console output in a string variable. How can I get 'mysql is running' in a string variable so that I can use this string variable later.
thankx.
If you want to capture output then use popen() rather than system().
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detect if stdin is a terminal or pipe in C/C++/Qt?
I'm writing a command line application that expects data as either a command line argument, or from cin.
Is there a way to check if the user piped some data in the application ($ ./myapp < test.txt), and only display a prompt for keyboard input if not?
If I'm checking for !cin.good() / cin.eof() etc., the prompt will never appear.
isatty(STDIN_FILENO)
will return whether standard input is a terminal (tty), i.e. interactive.
Perhaps you can do something with fstat(2) and S_ISFIFO?