Getting junk characters while using fgets to read from file

May 25, 2009 at 10:16am
Dear All,

I am using fgets to read line by line from file.
There are some special characters in the file like degrees and micro symbols.
After reading using fgets am trying to print it in terminal but am getting junk character before these symbol where ever it comes.
What is the reason for this .. How can i overcome this...

Thanks in advance,
Vishwa
May 25, 2009 at 11:02am
Hi,


The usage of fgets is as follows, please cross check it.


// crt_fgets.c
/* This program uses fgets to display
* a line from a file on the screen.
*/

#include <stdio.h>

int main( void )
{
FILE *stream;
char line[100];

if( (stream = fopen( "crt_fgets.txt", "r" )) != NULL )
{
if( fgets( line, 100, stream ) == NULL)
printf( "fgets error\n" );
else
printf( "%s", line);
fclose( stream );
}
}
May 25, 2009 at 11:34am
C doesn't understand things like Unicode/UTF-8/etc or terminal code pages, etc.

Save a file in Notepad with special characters and it will not print out the same to the console (unless you take special care to set up the console first).

Unfortunately, the console simply isn't designed to handle Unicode... so your application must do some special work to display the characters you want.

Can you give us a little more information on what exactly you are trying to do?
May 26, 2009 at 5:52am
Yeah sure...

My text file has the following details...

P 54°
amplitudes in µV, areas in µV...


Using fgets am getting this junk character "A^"(the exponent symbol placed just above the 'A').

Not only in console when i try to place this string in a bitmap using my application the same junk character is there...
Last edited on May 26, 2009 at 5:56am
May 26, 2009 at 6:28am
It isn't a junk character -- it is a code page mismatch.

MS has a bunch of functions to convert characters... but it is late for me now. I'll look it up for you tomorrow...
Topic archived. No new replies allowed.