CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: fwrite problem

  1. #1
    Join Date
    Apr 2013
    Posts
    77

    fwrite problem

    Iam trying to write a TCHAR array value to a file.But i am not getting it correctly.I should get the data continously.But iam getting Null between each character.
    Code:
    FILE * pFile;
      TCHAR buffer[] = {"INPUT"};
       fopen_s (&pFile,_T("myfile.#abc"), "w+");
      fwrite (buffer , wcslen(buffer)+1, sizeof(TCHAR),pFile);
      fclose (pFile);
    Output i should get
    HTML Code:
    INPUT
    Output iam getting
    HTML Code:
    I[NULL]N[NULL]P[NULL]U[NULL]T[NULL]

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: fwrite problem

    Is your build a UNICODE one?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    Re: fwrite problem

    Quote Originally Posted by VictorN View Post
    Is your build a UNICODE one?
    yes sir

  4. #4
    Join Date
    Apr 2013
    Posts
    77

    Re: fwrite problem

    So for getting desired output i should use char array instead of TCHAR array.Am i Correct?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: fwrite problem

    Quote Originally Posted by manjut19 View Post
    So for getting desired output i should use char array instead of TCHAR array.Am i Correct?
    This depends upon what is the desired output! As it stands, you are writing 16bit unicode chars to the file. So these can be read as 16bit unicode chars. If you just want a file of 8bit ASCII chars, then buffer should be of type char (8bit ASCII) as opposed to TCHAR (16bit UNICODE).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Apr 2013
    Posts
    77

    Re: fwrite problem

    Quote Originally Posted by 2kaud View Post
    This depends upon what is the desired output! As it stands, you are writing 16bit unicode chars to the file. So these can be read as 16bit unicode chars. If you just want a file of 8bit ASCII chars, then buffer should be of type char (8bit ASCII) as opposed to TCHAR (16bit UNICODE).
    Actually Buffer value i am getting throgh a system function call,Thats why that is aTCHAR variable.I canot change that variable.Then next option is Converting TCHAR * to char *How to do that conversion?can i use
    Code:
    widechartomultibyte()
    ?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: fwrite problem

    How to do that conversion?can i use widechartomultibyte()
    Yes. See

    http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured