Thread: Need Help with fgetws() stream issue

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    29

    Need Help with fgetws() stream issue

    hi guys. While writing a program I need to use the function fgetws() which has following parameters
    Code:
    wchar_t *fgetws(wchar_t *WS, size_t n, FILE *stream);

    Now the problem is, I need to pass a 'String' or 'char pointer' instead of stream as the third parameter which is obviously not possible directly.

    Kindly someone suggest me a method to convert a string into the appropriate stream which I can pass in that function or an alternative to fgetws().

    Thanks in advance.



  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Can you elaborate?

    Why do you want this? (There may be a better way to do what you want.)

    What is the string you want to pass as the 3rd parameter? Is it a filename?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Indeed, if that "'String' or 'char pointer'" is a filename, then the obvious solution is to use it to err... open the output stream then pass the file handle as the third argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Why do you want this? (There may be a better way to do what you want.)
    I'm writing a project where the input stream is needed to pass through fgetws() for generating its UNICODE which is later sent to a .dll function. But Now I want to provide that stream from the code itself instead of taking input by stdin. That's why I need to convert a string into a stream because fgetws() accepts only stream.
    What is the string you want to pass as the 3rd parameter? Is it a filename?
    it could be anything probably a name.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Is "swscanf" what you are looking for?
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by laserlight View Post
    Indeed, if that "'String' or 'char pointer'" is a filename, then the obvious solution is to use it to err... open the output stream then pass the file handle as the third argument.
    Here, I mean a simple 'string' or 'char pointer'.....
    Like
    Code:
    char *p;
    string s="anything";

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It looks like Click_here may have decoded your request, although the proffered function may not be exactly what you're after (though it may well be).

    You're basically just looking for a way to convert a non-unicode string into unicode, right?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Linked_List
    I'm writing a project where the input stream is needed to pass through fgetws() for generating its UNICODE which is later sent to a .dll function. But Now I want to provide that stream from the code itself instead of taking input by stdin. That's why I need to convert a string into a stream because fgetws() accepts only stream.
    What do you mean by "generating its UNICODE"? My knowledge of Unicode still sucks, but it seems to me that fgetws will just read into an array of wchar_t. If you already have an array of wchar_t, then wanting to use it does not make sense. If you have an array of char, then you want to convert it to an array of wchar_t... you probably need to elaborate on what exactly it is that you are trying to do, but fgetws is likely to be a wrong approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by Click_here View Post
    Is "swscanf" what you are looking for?
    Not working.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    You're basically just looking for a way to convert a non-unicode string into unicode, right?
    you get it right. ^_^

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by laserlight View Post
    What do you mean by "generating its UNICODE"? My knowledge of Unicode still sucks, but it seems to me that fgetws will just read into an array of wchar_t. If you already have an array of wchar_t, then wanting to use it does not make sense. If you have an array of char, then you want to convert it to an array of wchar_t... you probably need to elaborate on what exactly it is that you are trying to do, but fgetws is likely to be a wrong approach.
    ok. Let me try to elaborate a little...
    Code:
    wchar_t*fgetws(wchar_t*WS, size_tn, FILE*stream);
    The fgetws() function shall read characters from the stream, convert these to the corresponding wide-character codes, place them in the wchar_t array pointed to by ws, until n-1 characters are read, or a <newline> is read, converted, and transferred to ws, or an end-of-file condition is encountered. The wide-character string, ws, shall then be terminated with a null wide-character code.

    Now I hope you get it what I'm trying to accomplish.. As of now, I only want to pass a string instead of stream in this function.. however if I get my hands on another method which can perform the similar task then it'll also be acceptable.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you just want to create a "wide char" string you can do this:
    Code:
    wchar_t *str = L"whatever";  // note the L
    and just pass that.

    You can also look into mbstowcs and wcstombs or the windows-specific functions MultiByteToWideChar and WideCharToMultiByte.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by oogabooga
    If you just want to create a "wide char" string you can do this:
    Code:
    wchar_t *str = L"whatever";  // note the L
    or rather:
    Code:
    const wchar_t *str = L"whatever";
    You don't want to accidentally attempt to change the wide string literal without some warning by the compiler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    or rather:
    Code:
    const wchar_t *str = L"whatever";
    You don't want to accidentally attempt to change the wide string literal without some warning by the compiler.
    I gotta learn my const correctness!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Linked_List View Post
    Not working.
    When you read up on the function (which I'm assuming you did before you tried it), did it sound like the correct function?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue overloading stream Operator/Manipulator
    By Jaken Veina in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2011, 12:48 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. Bit Stream
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 11-26-2009, 10:04 PM
  4. Extracting from stream issue
    By keira in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2008, 08:56 PM
  5. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM