Fgetc returns the wrong character (0a -> 0d)

G

Georg Troxler

I try to parse a file. When I read a int with fgetc(f) I get the wrong
result.
When I open the file with a Editor I see clearly the character should be 0d.
In the Memory of f (buffer) it is 0a. What the Hell is going wrong here. I
calculated the offset und figured the chars are the same position. Memory
Error is not possible since I look at the Memory just after the Buffersize
was 0. (Means the MSL just filled the Buffer again). When I change the 0d to
0a it does exactly the opposite.
 
A

Alexander Bartolich

begin followup to Georg Troxler:
I try to parse a file. When I read a int with fgetc(f) I get the
wrong result. When I open the file with a Editor I see clearly the
character should be 0d.

Unix uses \xA for a line feed.
DOS, Windows, NT and OS/2 use \xD\xA for a line feed.
MacOS <= 9 uses \xD for a line feed.

Some implementations on these platforms convert the OS specific line
feed into "\xA" on input. See your vendors documentation for the
keywords 'binary mode'. Probably specifing character 'b' to argument
mode of mode should do.
 
P

P.J. Plauger

I try to parse a file. When I read a int with fgetc(f) I get the wrong
result.
When I open the file with a Editor I see clearly the character should be 0d.
In the Memory of f (buffer) it is 0a. What the Hell is going wrong here. I
calculated the offset und figured the chars are the same position. Memory
Error is not possible since I look at the Memory just after the Buffersize
was 0. (Means the MSL just filled the Buffer again). When I change the 0d to
0a it does exactly the opposite.

Open the file as binary and you won't get this mapping that
seems to irritate you.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
D

Darrell Grainger

I try to parse a file. When I read a int with fgetc(f) I get the wrong
result.
When I open the file with a Editor I see clearly the character should be 0d.
In the Memory of f (buffer) it is 0a. What the Hell is going wrong here. I
calculated the offset und figured the chars are the same position. Memory
Error is not possible since I look at the Memory just after the Buffersize
was 0. (Means the MSL just filled the Buffer again). When I change the 0d to
0a it does exactly the opposite.

How was the file opened? If you open it as a text file, i.e. "r" rather
than "rb", then what is stored on the system and how it comes in the
stream can be different.

Try opening the file as binary and see if that solves your problem.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
I try to parse a file. When I read a int with fgetc(f) I get the wrong
result.
When I open the file with a Editor I see clearly the character should be
0d. In the Memory of f (buffer) it is 0a

Open the file in binary mode ("rw") for a raw process of the data.
 
A

August Derleth

Emmanuel said:
Open the file in binary mode ("rw") for a raw process of the data.

ITYM "rb". "rw" implies read/write, which I don't think is implemented
by the standard library.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
ITYM "rb". "rw" implies read/write, which I don't think is implemented
by the standard library.

Your are correct, and I was dumb. Apologies.
 
D

Dan Pop

In said:
ITYM "rb". "rw" implies read/write, which I don't think is implemented
by the standard library.

It is implemented, but it's spelled "r+" or "w+" (the former preserves
the contents of the file upon opening, the latter wipes it out).

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top