Like Tree4Likes

Need Help with fgetws() stream issue

This is a discussion on Need Help with fgetws() stream issue within the C Programming forums, part of the General Programming Boards category; Originally Posted by Click_here When you read up on the function (which I'm assuming you did before you tried it), ...

  1. #16
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by Click_here View Post
    When you read up on the function (which I'm assuming you did before you tried it), did it sound like the correct function?
    Nothing seems to be working. :'(

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    854
    I'd use open_wmemstream() to create a wide-character memory stream, then fputws() a multi-line constant wide-character string into it (say static const wchar_t my_test_stream[] = L"First line\nSecond line\nThird line\n"; ). Then rewind(handle) and you can give the handle to fgetws(). This is POSIX.1-2008, so I don't know if you can use it in Windows.
    Last edited by Nominal Animal; 10-10-2012 at 08:37 AM.
    oogabooga likes this.

  3. #18
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by Nominal Animal View Post
    I'd use open_wmemstream() to create a wide-character memory stream, then fputws() a multi-line constant wide-character string into it (say static const wchar_t my_test_stream[] = L"First line\nSecond line\nThird line\n"; ). Then rewind(handle) and you can give the handle to fgetws(). This is POSIX.1-2008, so I don't know if you can use it in Windows.
    look at this thread for better understanding of the problem
    Explanation for working of (char *) type syntax

  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Linked_List View Post
    Nothing seems to be working. :'(
    This is getting ridiculous.
    Post your code so we can see exactly what you're attempting.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #20
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by oogabooga View Post
    This is getting ridiculous.
    Post your code so we can see exactly what you're attempting.
    check the above link.. I've already posted the piece of code.

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Linked_List View Post
    check the above link.. I've already posted the piece of code.
    That's the last straw for me.
    Good luck and goodbye.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #22
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Quote Originally Posted by oogabooga View Post
    That's the last straw for me.
    Good luck and goodbye.
    your wish.........

  8. #23
    Registered User
    Join Date
    Oct 2011
    Posts
    854
    Consider the following example code:
    Code:
    #define  _POSIX_C_SOURCE 200809L
    #include <stdlib.h>
    #include <stdio.h>
    #include <wchar.h>
    
    int main(void)
    {
    	FILE    *handle;
    
    	wchar_t *cache_ptr = NULL;
    	size_t   cache_len = 0;
    
    	wchar_t  line_buffer[1024];
    	wchar_t *line;
    
    	/* Create the memory stream */
    	handle = open_wmemstream(&cache_ptr, &cache_len);
    	if (!handle) {
    		fprintf(stderr, "Cannot create a wide-character memory stream.\n");
    		return 1;
    	}
    
    	/* Stuff data into the memory stream */
    	if (fputws(L"First line\nSecond line\nThird line is the last one\n", handle) == -1) {
    		fprintf(stderr, "Error writing to the wide-character memory stream.\n");
    		return 1;
    	}
    
    	/* Rewind the memory stream, so we can read from it */
    	rewind(handle);
    
    	/* Your fgetws() function goes here -- this reads each line */
    	while (1) {
    
    		line = fgetws(line_buffer, sizeof line_buffer, handle);
    		if (!line)
    			break;
    
    		printf("Read %d wide characters\n", (int)wcslen(line));
    	}
    
    	/* Close the handle normally */
    	if (fclose(handle)) {
    		fprintf(stderr, "Error closing wide-character memory stream.\n");
    		return 1;
    	}
    
    	/* Discard the buffer. */
    	free(cache_ptr);
    	cache_ptr = NULL;
    	cache_len = 0;
    
    	return 0;	
    }
    It shows how to create a FILE * handle to wide-character in-memory data. Then I stuff wide-character strings into it, the example data I wish to test. Then I rewind() the stream. Now I can use fgetws() to read the data back from the stream.

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    Quote Originally Posted by Linked_List


    Quote Originally Posted by click_here
    When you read up on the function (which I'm assuming you did before you tried it), did it sound like the correct function?

    Nothing seems to be working


    I didn't ask if you could get it working, I asked if it sounded like the function that you needed.


    Quote Originally Posted by Linked_List
    check the above link.. I've already posted the piece of code.

    Explanation for working of (char *) type syntax



    Where did you get this code from? I'm guessing that you didn't write it because you asked for an explanation of what it means in the other topic
    Fact - Beethoven wrote his first symphony in C

Page 2 of 2 FirstFirst 12
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, 09:04 PM
  4. Extracting from stream issue
    By keira in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2008, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21