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
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'm working on a project and need to test one of my class' member variables to verify that the user did indeed enter a string.
I've also tried using (patronName == '') and (patronName == "") but have had no luck.
Edit: Using "\n" fixes the error but the program ends without allowing the user to enter a name.
std::string Restaurant::getPatronName()
{
bool controlFlag = true;
do
{
getline(std::cin,patronName);
if ((std::cin.fail()) || (patronName == '\n'))
{
std::cout << "You must enter a name!" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
controlFlag = false;
}
} while (controlFlag);
return patronName;
}
The function should read and store the name entered by the user into patronName. When trying to build, I get an error that says "no match for 'operator=='". Could this be because the object called in main is a pointer of type Restaurant?
Besides the type mismatch between the character '\n' and the std::string patronName, we can find at https://en.cppreference.com/w/cpp/string/basic_string/getline that std::getline(input, str, delim);
Extracts characters from input and appends them to str until […] the next available input character is delim, […], in which case the delimiter character is extracted from input, but is not appended to str.
So there won't be any '\n' character, if delim is the newline, in the first place.
You can use std::basic_string::empty() to check if a string is empty.
What happens with '\n' is you are comparing a string with a char, which, i suspect, there is no operator== defined for this case. If you are sure the string isn't empty, you can call operator[] formerName[0], which returns a char.
You have to write patronName == "\n" because you cannot compare string and character
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);
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"?
As the title says, I am trying to parse a file but ignore comments (started with #) or blank lines. I have tried to make a system for this, yet it always seems to ignore that it should be ignoring comments and/or blank lines.
lines := strings.Split(d, "\n")
var output map[string]bool = make(map[string]bool)
for _, line := range lines {
if strings.HasPrefix(line, "#") != true {
output[line] = true
} else if len(line) > 0 {
output[line] = true
}
}
When run (this is part of a function), it outputs the following
This is the input ('d' variable):
Minecraft
Zerg Rush
Pokemon
# Hello
This is the output when printed ('output' variable):
map[Minecraft:true Zerg Rush:true Pokemon:true :true # Hello:true]
My issue here is that it still keeps the "" and "# Hello" values, meaning that something failed, something I haven't been able to figure out.
So, what am I doing wrong that this keeps the improper values?
len(line) > 0 will be true for the "# Hello" line, so it will get added to output.
Currently, you are adding lines that either don't start with a # or are not empty. You need to only add lines that satisfy both conditions:
if !strings.HasPrefix(line, "#") && len(line) > 0 {
output[line] = true
}