output stream in C++ [duplicate] - c++

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.

Related

C++ console output not switching to a new line when the program ends [duplicate]

This question already has answers here:
Most efficient way to output a newline
(7 answers)
Closed 6 months ago.
So I'm just starting to learn C++, and I'm just trying to get everything set up. When I run the compiled binary for my hello world code, it display's the console output, but doesn't switch to a new line afterwards. Here's an example:
Hello, World!michaela#michaela-HP-Laptop-17-by4xxx: $
I tried researching, but I can't find a good solution. I'm on Ubuntu 20.04, and using the BASH shell. I will provide the code if it's any help, though I doubt it as it literally just outputs "Hello, World!".
#include <iostream>
int main(){
std::cout << "Hello, World!";
return 0;
}
This is completely normal for the majority of programming languages, especially in a Unix environment. Even shell scripting will print without a newline if you use printf or echo -n. And thanks to the terminal emulating an old teletype-like printer-based system the cursor won't reset just because a program exitted.
If you want a newline add a '\n' to your string or output a std::endl after your text.
try this :
std::cout << "Hello, World!"<<std::endl;
or you can use this one :
std::cout << "Hello, World!\n";
The only difference between std::endl and \n is that std::endl adds std::flush (to flush the output stream), which is usually totally unnecessary and makes the program slower.

How to print a non ascii-char in c++ [duplicate]

This question already has answers here:
Output Unicode to console Using C++, in Windows
(5 answers)
Closed 3 years ago.
I'm learning C++ and I cannot figure out how how to print special characters in C++. Even when I've seen others post related to this issue, any of them solves this:
I just want to print out this chars -> '♦', '♥', '♣', '♠';
And when I do this ->
std::cout << '♦' << std::endl;
It prints out a number such as 14850470, but when I pass the char to a function, such as ->
char foo(char a)
{
return a;
}
int main()
{
std::cout << foo('♦') << std::endl;
}
It prints out 'ª' instead, any ideas?
(I'm writing this in VSCode with the MSVC compiler on Windows.)
EDIT:
The answers solved my problem (I executed chcp 65001 on the CL). But I have to change this std::cout << '♠' << std::endl; to this std::cout << "♠" << std::endl; in order to work, since printing as char prints nothing on the console.
There is no portable way.
On Linux/mac, the terminal recently adopted UTF-8 as default. So, when you output a UTF-8 binary to the standard output, you can see the '♦' character.
On Windows 10 1607 or later, chcp 65001 will work fine except when printing color emoji. Since the broken font config dialog is fixed and you can use a TrueType font for chcp 65001 console. In your program, you should output a UTF-8 binary to the standard output. Before you run your program, run chcp 65001 and configure the font.
On windows before Windows 10 1607, you should give up trying to print Unicode.
On C++20, the C++ standard committee adopt char8_t that's assumed to hold UTF-8. In the future, C++ (C++26? C++29?), when <locale> and <iostream> is recreated and support char8_t, you can printf a Unicode character portably.
In my opinion, you should give up trying to print Unicode characters. Create a GUI using some library which supports TrueType fonts and OpenType.
Assuming you're happy to have your code only work in Windows I think the answer to your question can be found here:
How to use unicode characters in Windows command line?
The answer doesn't go into a lot of detail, this should help: https://learn.microsoft.com/en-us/windows/console/low-level-console-output-functions
This way might be quicker and easier though: How to print Unicode character in C++?

C++ console insert text into displayed text [duplicate]

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.

C++ FileIO weird behaviour

While writing a program I came accross a strange behaviour of std::ofstream. Please refer to the code below
ofstream dire;
dire.open("dir.txt", std::ios::out);
// some other code
for(int i=0;i<dir.size();i++)
{
dire << dir[i] << "\t"; // dir is integer vector containing values between 0-9
}
Now when I open dir.txt contents are:
ऴऴऴऴवववववववववशशशशशशशशशशषषषषषषषषरररररररऱऱऱऱऱऱऱऱऱललललललललललललळ.. and so on
if I give a space and then tab(\t) then it works correctly or for that matter \n also works correctly. dire << dir[i] << " \t";
And now the output is:
4 4 4 4 5 5 5 5 5 5.. and so on
I also tried dire.flush() to flush the output buffer to file, but still the same result.
I can definitely get away by using \t but I would like to learn why this is happening.
If you are using Notepad to look at the file then the bug Bush hid the facts can be the problem.
The bug occurs when the string is passed to the Win32 charset detection function IsTextUnicode with no other characters. IsTextUnicode sees what it thinks is valid UTF-16LE Chinese and returns true, and the application then incorrectly interprets the text as UTF-16LE.

How do I rewrite a line of text in a console project? c++

I'm working on a c++ console project and i would like to show a percentage without making a new line each time (so that the window doesn't get clogged with thousands of lines).
Is there a way of removing the last line that was printed or something to say that the next time that i output a line it should replace the current line?
You can use a \r (carriage return) to return the cursor to the beginning of the line:
This works on windows and Linux.
From: Erase the current printed console line
You could alternatively use a series of backspaces.
string str="Hello!";
cout << str;
cout << string(str.length(),'\b');
cout << "Hello again!";
From: http://www.cplusplus.com/forum/unices/25744/
Maybe mark as duplicate? I am really not sure how.
A simple example that I tested on Linux would be:
std::cout << "Some text to display..." << "\t\r" << std::flush;
Here the \t adds a tabulation to handle slightly varying string lengths and \r sends the cursor back at the start of the line (as mentioned in other answers).
std::flush is required to guarantee that the line is displayed without jumping to the next line.
This is very platform-dependent and terminal-dependent. But, you may want to look at ncurses for a start: http://linux.die.net/man/3/ncurses
For Windows: How can I overwrite the same portion of the console in a Windows native C++ console app, without using a 3rd Party library?
For Linux: https://unix.stackexchange.com/questions/43075/how-to-change-the-contents-of-a-line-on-the-terminal-as-opposed-to-writing-a-new