9

What's the most straightforward way to write to stdout using a character array? I want to output a slice of a much larger array, and the slice is not null-terminated. I want to avoid copying the slice to a "proper" null-terminated c-string.

1
  • 2
    std::copy is appropriate. Commented Aug 28, 2013 at 18:08

5 Answers 5

14

There is a pretty obvious solution I didn't find at first. std::cout is an instance of ostream.

void WriteChunk(char *buffer, size_t startpos, size_t length) {
    std::cout.write(buffer + startpos, length);
}

so std::cout.write does the trick.

Sign up to request clarification or add additional context in comments.

8 Comments

You could do it that way ... or you could just use std::copy: std::copy(buffer, buffer + however_many_characters_you_want, std::ostream_iterator<char><std::cout, ""));
how is that simpler than std::cout.write(buffer + startpos, length)?
Note that write is an unformatted output function; it's very unusual to use unformatted output functions with std::cout. That's why @ZacHowland's example is almost certainly better.
"just" because 1) you are not writing your own function to reinvent the wheel, and 2) it is a very simple function call.
@ZacHowland the function is not needed, all he does is std::cout.write(buffer + startpos, length);.
|
4

std::copy seems to exactly do what you want :

#include <iostream>    // for std::cout
#include <algorithm>   // for std::copy
#include <iterator>    // for std::ostream_iterator

//...
char arr[] = "abcdefij";

std::copy(arr + 2, arr + 5, std::ostream_iterator<char>(std::cout, ""));

This example will write on the standard output: cde.

Here is a live example.

Comments

1

If you know the bounds of the character array then you could write:

char* arr = new char[N];
for(size_t i = min_indx; i < max_indx; ++i) {
  cout << arr[i];
}

You just have to make sure min_indx is between 0 and N-1 and max_indx is between 0 and N.

Since we all like library functions to do things, here is the way to do it using std::copy:

copy(arr + min_indx, arr + max_indx, ostream_iterator<char>(cout, ""));

Comments

1

ostream::write should work.

#include <iostream>
int main() {
    char a[] = "ABCDEFGHIJ";
    std::ostream out(std::cout.rdbuf());
    out.write(a, 2);
}

Edit

Creating a separate ostream object is not required, as std::cout is a ostream object itself. So std::cout.write is sufficient.

Comments

1

Ok, this is the "department for silly ideas":

 class partial_print_wrapper
 {
   private:
     const char *str;
     int start;
     int end;
   public:
     partial_print_wrapper(const char *s, int st, int en) : str(s), start(st), end(en) {}
     friend ostream& operator <<(ostream &os, const partial_print_wrapper& pw);
 };

 ostream& operator <<(ostream &os, const partial_print_wrapper& pw)
 {
     for(int i = pw.start; i < pw.end; i++)
     {
         os << pw.str[i];
     }
     return os;
 }

 char *s = "Something quite long";

 cout << print_string_wrapper(s, 3, 8) << endl;

4 Comments

This won't actually compile.
No, it's missing a semicolon ;)
And operator<< needs to be a non-member because ostream& needs to be the first parameter.
@Simple: Ok, so edited. Of course, like I said, it's a rather silly idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.