Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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