Please Help: Reading & Writing from/to a Unicode File produces garbage in output file 
Author Message
 Please Help: Reading & Writing from/to a Unicode File produces garbage in output file

I am attempting to write a program that reads from a UNICODE file and then
writes out to a UNICODE file. The program reads from the UNICODE file, but
the output file contains garbage.

Any suggestions would be welcome.
Thanks in advance.
Brad

// Header files
#include <stdio.h>
#define _UNICODE // define UNICODE support
#include <tchar.h> // required for UNICODE support

// main()
void main()
{
// Declare local variables
TCHAR tcTemp = _T('\0'); // temporary tchar
FILE* fpSource = 0; // file pointer to source file
FILE* fpDestination = 0; // file pointer to destination file

// open the source file
if (!(fpSource = _tfopen(_T("test.uni"), _T("r"))))
{
_tprintf(_T("Error: Could not open source file."));
return;

Quote:
}

// open the destination file... close out the source file pointer if error
if (!(fpDestination = _tfopen(_T("myfile2.uni"), _T("w"))))
{
_tprintf(_T("Error: Could not open destination file."));
fclose(fpSource);
return;

Quote:
}

// get a character from source file, and check for EOF
while ((tcTemp = _fgettc(fpSource)) != _FEOF)
{
// if not EOF, copy it to the destination file
_fputtc(tcTemp, fpDestination);

Quote:
}

// be nice to your users
_tprintf(_T("Success."));

// if you open a pointer, close it.
fclose(fpSource);
fclose(fpDestination);

Quote:
}



Sun, 05 Jan 2003 03:00:00 GMT  
 Please Help: Reading & Writing from/to a Unicode File produces garbage in output file
On Wed, 19 Jul 2000 00:23:01 -0700, "Brad Williams"

Quote:
> I am attempting to write a program that reads from a UNICODE file and then
> writes out to a UNICODE file. The program reads from the UNICODE file, but
> the output file contains garbage.

> Any suggestions would be welcome.
> Thanks in advance.
> Brad

> // Header files
> #include <stdio.h>
> #define _UNICODE // define UNICODE support

I have no idea what this symbol means.

Quote:
> #include <tchar.h> // required for UNICODE support

This is not a standard header.

Quote:
> // main()
> void main()

The above is an illegal definition for main().  The C language
requires that main() be defined with a return type of int in a hosted
environment.

Quote:
> {
> // Declare local variables
> TCHAR tcTemp = _T('\0'); // temporary tchar
> FILE* fpSource = 0; // file pointer to source file
> FILE* fpDestination = 0; // file pointer to destination file

> // open the source file
> if (!(fpSource = _tfopen(_T("test.uni"), _T("r"))))
> {
> _tprintf(_T("Error: Could not open source file."));
> return;
> }

> // open the destination file... close out the source file pointer if error
> if (!(fpDestination = _tfopen(_T("myfile2.uni"), _T("w"))))
> {
> _tprintf(_T("Error: Could not open destination file."));
> fclose(fpSource);
> return;
> }

> // get a character from source file, and check for EOF
> while ((tcTemp = _fgettc(fpSource)) != _FEOF)
> {
> // if not EOF, copy it to the destination file
> _fputtc(tcTemp, fpDestination);
> }

> // be nice to your users
> _tprintf(_T("Success."));

> // if you open a pointer, close it.
> fclose(fpSource);
> fclose(fpDestination);
> }

You are asking in the wrong place.  None of the following have
anything at all to do with the C language:

tchar.h
TCHAR
_T
_topen()
_tprintf()

Each and every one of these things is a compiler specific extension.
None of them are defined by or part of the C language, so there is
literally no C language answer to the question of how they should
work.  Ask this in a support group for the particular compiler that
supplies these non-standard extensions.

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://marshall-cline.home.att.net/cpp-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/



Mon, 06 Jan 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. read a file than produce 2 output files(HELP)

2. read a file than produce 2 output files(HELP)

3. File Reading/Writing (PLEASE HELP)

4. Help - Reading & Writing binary file

5. Help - Reading & Writing binary files with bitfields

6. problem reading from and writing to file - please help

7. about bitmap file read&show&write

8. Reading & Writing to Text Files

9. Reading & writing to a locked file

10. read & write to file

11. Reading & Writing to file

12. Read & write a string to binary files

 

 
Powered by phpBB® Forum Software