See my code
char t[]= "{\n abcdeffgjejgjergnjkengkknkn \n";
printf("%s",t);
char t1[]= "{ abcdeffgjejgjergnjkengkknkn \n aaffdefa";
printf("%s",t1);
Actual Output:
{
{ abcdeffgjejgjergnjkengkknkn
Expected output:
{
abcdeffgjejgjergnjkengkknkn
{ abcdeffgjejgjergnjkengkknkn
aaffdefa
Can any one help me why string is not getting print after \n (LF)?
Compiler - arm-none-eabi
Library header - Newlib
IDE: MCUExpresso
By default stdout (where printf writes) is line buffered. That means the output buffer is flushed (actually written) either when it's full or when you print a newline.
That's why the second part of the output isn't printed, because it's not enough to fill the buffer and you have no newline to flush the buffer.
You can flush explicitly yourself by calling fflush:
printf(...);
fflush(stdout);
Related
1.i am trying to check whether the cin.get() leaves the end line character in stream and considered it for next input.
i have tried this code in code blocks but unable to provide input for next string,i am attaching code i have tried and the output .could anyone please explain.
#include<iostream>
using namespace std;
int main()
{
char s1[10];
char s2[10];
cout << "enter the first string: ";
cin.get(s1, 10);
cout << "enter the second string: ";
cin.getline(s2, 10);
cout << s1 << " " << s2;
return 0;
}
enter the first string: hello
enter the second string: hello
please explain the output
This get function reference says that your overload is
Same as get(s, count, widen('\n'))
And that overload of the function reads until (among other things)
the next available input character c equals delim, as determined by Traits::eq(c, delim). This character is not extracted (unlike basic_istream::getline())
[Emphasis mine]
So the newline is left in the input buffer, for the getline call to read as an "empty" line.
If you want to read lines, I suggest you use std::string and std::getline (which does read, and throw away, the newline).
cin.get() grabs the newline character by default. It will not leave the newline in the stream.
I am making a simple enque/deque program in kernel. I want to print message in kernel, and this is what I got:
[18594.595747] Enqueue 3
[18594.595748] queue :
[18594.595751] 2
[18594.595751] 1
[18594.595752] 3
But I want to print this without newline:
[8594.595747] Enqueue 3
[18594.595748] queue : 2 1 3
This is a part of my code:
printk(KERN_ALERT "Enqueue %d \n queue : ", a);
rear++;
for(i = front; i<rear; i++)
printk(KERN_ALERT "%d ", queue_test[i]);
In short, I want to print in kernel a message in one line. But if I use printk, it changes line automatically. How do I print a message in one line?
To prevent a new line from being started, use KERN_CONT:
printk(KERN_ALERT "self destruction commences in ");
printk(KERN_CONT "%d", time_remaining);
printk(KERN_CONT " minutes\n");
Debugging by printing
printk(KERN_ERR "Doing something was ");
/* <100 lines of whatever>*/
if (success)
printk(KERN_CONT "successful\n");
else
printk(KERN_CONT "NOT successful\n");
Logging prints should be safe: SFP vs single CPU.
string s;
while(getline(cin,s)){
cout << "---" << endl
for(auto c: s) cout << int(c) << endl;
}
cout << "Exiting";
If my input is Ctrl+Z, then I press enter once, and my program exits immediately.
^Z
Exiting
If I enter a character before pressing Ctrl+Z, then I have to press enter twice, and my program does not exit.
s^Z
---
115
26
I had always interpreted Ctrl+Z as the EOF character. getline would continue until it reaches this character, at which point getline tests false and my program would exit. I'm curious why my program interprets Ctrl+Z as the substitute character 26, depending on whether there is a preceding character or not, and why it was necessary for me to press Enter twice in the second example?
26 is code of ^Z on your platform , and ^Z is a EOF marker for terminal, that's true. Characters with codes less than 32 are control characters for ASCII -compatible platform, I hope you know that. 26 isn't a substitute character, it's actual control code, ^Z or some "bug" character are substitutes. getline reads input until EOL (end-of-line, designated as CR by ASCII) or EOF (end of file, end of stream, designated as SUB) is encountered in stream, so ^Z is read with the second call of getline. That behavior is absolutely correct.
It is defined by platform (or, more precisely, by terminal type) if characters are sent to input buffer immediately or after some flush command occurred. Usual cause of buffer flush is EOL character, that's your ENTER (CR - Carriage return). Tat's why program receives EOF after Enter in your case. Note that some platform use LF (line feed) as EOL, and some - a pair of LF+CR. C literal '\n' is to be correctly translated into particular EOL marker.
Note, that you can use different delimiter:
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline(
std::basic_istream<CharT,Traits>& input,
std::basic_string<CharT,Traits,Allocator>& str,
CharT delim );
ASCII table with substitute Control+ :
Suppose a file "a.txt" which contains just one line string as below:
hello world
I don't contain any new line symbol like "\n".
void loadFileStr(istream& stream, stringstream& strs){
str << stream.rdbuf();
}
I call loadFileStr and output its read string.
int main(){
stringstream strs;
ifstream ifs("a.txt",strs);
loadFileStr(ifs, strs)
ifs.close();
std::cout << strs.str();
}
However,
strs.str()
shows hello world with newline.
hello world
'\n'
(\n) is just a space actually.
Is this rdbuf() feature or how can i read this file without any "\n"?
I'm parsing output for a test I'm running as a Gradle JavaExec task. I want to filter the output so that only errors are shown in stdout (I'm running the task from the Windows command line).
In a doLast{} block, I've parsed the OutputStream to a list of strings where each entry is a line:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
String log = baos.toString()
List<String> logLines
logLines = new ArrayList<String>(Arrays.asList(log.split('\n')))
then iterated through the list, printing each line that starts with 'E' (for ERROR):
for (String entry : logLines){
if (entry.charAt(0) == 'E'){
println "First char == E"
println (entry)
}
}
println "Execution complete!"
My output prints the first two errors, but prints empty strings for all the rest:
First char == E
ERROR More info here
First char == E
ERROR More info here
First char == E
First char == E
First char == E
Execution complete!
Why does println(entry) print empty strings? Obviously entry is not an empty string, because it wouldn't get past the boolean condition if it's first character was empty.
#Roman had the answer: println(groovy.json.JsonOutput.toJson(entry)) revealed that carriage returns \r were being used in addition to new lines \n. Therefore the solution is to split by the regex '[\r\n]+' which will separate into an array of strings at each \n or \r\n