Solved

Can a "buffer" be returned by a function?

Posted on 2004-10-24
234 Views
Last Modified: 2010-04-17
I am doing a client/server program where the server checks a linked list for results.
How do I get (or is it even possible) one of the functions in the linked list to return
a buffer space?  Like if I wanted the function to search the list and return something like:

setw(30)<<title<<setw(30)<< artist<<endl;

for an undetermined amount of lines (titles and artists)...

Currently my linked list specification function is:
void linkedList::Title(string musicArtist)
{
        //create pointer called current
       //start at beginning of list ... search until end of list
       //if the current finds a node where artist == musicArtist
       cout<<current->title<<endl;  //print out title
       //so there could be zero to several lines printed out to the screen
}

But the server needs to take what this function finds and return it to the client.  How do I get it into a buffer instead of printing out to the screen?

Thanks...  

I guess I need to change this from "void" to "char" or "string" ...
 
0
Question by:bjw5d6
    2 Comments
     
    LVL 9

    Expert Comment

    by:jhshukla
    using stringstream you can "print" the output to string instead of to screen and then return the string to the client.
    http://www.cplusplus.com/ref/iostream/stringstream/
    0
     
    LVL 9

    Accepted Solution

    by:
    example:
    stringstream  ss;
    string str;
    ss << "this is a string" << flush;
    str = ss.str();
    return str;


    if you like to or have to use c-style strings you can wrap it in a buffer struct and return the struct. client can then get it's c-style string from the buffer.

    struct buffer{
      char str[BUF_SIZE];
    };
    struct buffer buf;
    strncpy(buf.str, ss.str().c_str(), BUF_SIZE);
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This is about my first experience with programming Arduino.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now