Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Can a "buffer" be returned by a function?

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
bjw5d6
Asked:
bjw5d6
  • 2
1 Solution
 
jhshuklaCommented:
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
 
jhshuklaCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now