0

I was wondering if i could get some help. I'm trying to port over some code as required in a lab assignment given to us. Basically I've created some program in C, and part of the requirement now is to port over the printf and snprintf statements into C++. printf I think I can manage, as it seems to be standard using cout << "whatever"<< endl. The problem is with snprintf, and replacing it using ostream oss(). Here is a partition of the code I am trying to resolve.

char* formatTime(
                struct timeval* tv,     // Pointer to timeval struct
                char* buf,              // Pointer to char buf
                size_t len              // size of buffer
                )
{
    struct ExpandedTime etime2;         // Struct object declaration


    // Array containing strings for the months

    const char* month[] =
        {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
        "Aug", "Sep", "Oct", "Nov", "Dec"
        };


    if (len > 0)                        // If statement for 0 length buf error
    {
        localTime(tv, &etime2);

        // Printing to the supplied buffer in the main program
        char timebuf[80];               // Buffer to hold the time stamp
        ostream oss(timebuf, sizeof(timebuf));

        oss << etime2.et_year << ends;
        cout << timebuf << endl;
//      snprintf(buf, len, "%02i %s %02i %02i:%02i:%02i:%03i",
//      etime2.et_year, month[etime2.et_mon], etime2.et_day, etime2.et_hour,
//      etime2.et_min, etime2.et_sec, etime2.et_usec);
    }

Now i've commented out the original snprintf statement. And im just testing the "year" parameter which is store in some structure. However I keep getting the following errors.

rkim@l3055serv:~/plot$ make
g++ -g -Wno-deprecated -c fmttime.cc fmttime.o
fmttime.cc: In function 'char* formatTime(timeval*, char*, size_t)':
fmttime.cc:273: error: no matching function for call to 'std::basic_ostream<char, std::char_traits<char> >::basic_ostream(char [80], unsigned int)'
/usr/include/c++/4.4/ostream:361: note: candidates are: std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:82: note:                 std::basic_ostream<_CharT, _Traits>::basic_ostream(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:56: note:                 std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)
make: *** [fmttime.o] Error 1

I've made sure to include iostream and sstream as well as using namespace std; however I still get this error and am not able to resolve it. To be honest, we have just started learning C++ so part of the problem is the whole abstractness of C++. However this partition of code using osream and oss was given to us by our lab instructor and besides him using the wrong headers "strstream and oststream" to my knowledge of C++ thus far it seems to make sense....

3
  • Okay based on the comments here I am confused. At first i used the header strstream....people were telling me i needed sstream instead. However our instructor told us to us strstream and hes a pretty intelligent man. As well the use of oststream was implemented. I am confused by this. Commented Apr 14, 2013 at 20:26
  • Intelligent doesn't mean infallible. Commented Apr 14, 2013 at 20:29
  • True, and I didnt mean to imply that the man is some sort of C++ deity. Commented Apr 14, 2013 at 20:31

3 Answers 3

2

You're looking for std::ostringstream, not ostream.

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

Comments

1

I suggest you replace ostream with ostringstream.

The ostream is an abstract, generic class.

1 Comment

std::ostream is not an abstract class; it is instantiable and you can use any type of std::streambuf with it.
1

ostream doesn't have such a constructor, you probably wanted to use an ostringstream and something like:

std::ostringstream oss;
oss << etime2.et_year << ends;

std::cout << oss.str() << endl;

... but then there's not much point in using oss as you may as well stream straight to std::cout.

1 Comment

I see. I actually realized I dont need cout, because i need to write the string to a buffer. so that must be what oss << etime2.et_year.... must be doing. Its writing the string to a buffer specificed by oststream oss(). Is it possible to write multiple things into that buffer using one line? such as oss << etime2.et_year << month[etime2.et_mon] << ends; ?

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.