473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Byte-26 Terror

Eik
Hey,

I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file.

Take this example:

---BEGIN PROGRAM---

#include <stdio.h>

main () {
int i;
unsigned char c;
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
printf ("%3d|", c);
}
}

---END PROGRAM---

The file chars.txt contains 256 bytes -> byte values: 0, 1, 2, ...,
253, 254, 255

When I run the program I get this:

0| 1| 2| ... 24| 25|255|255|255| ... etc. Til 'i' reaches 256 and
the program stops shitting '255' (EOF's) on my screen

What is wrong with the 26-byte?!?
When I remove the 26-byte from chars.txt, I get this:

0| 1| 2| ... 24| 25| 27| 28| ... 253|254|255|255|

Why is the 26-byte ruining my program? How should I change my program?

THX

Ps. I'm using Windows XP, and the C-compiler Miracle C

Ohhh... and does anybody know how you can get the size of an file
without checking where the EOF byte is (because in binairy file's
there are more EOF's)
Nov 14 '05 #1
8 2074
On 16 Jul 2004 11:39:14 -0700, st*****@hotmail.com (Eik) wrote:
Hey,

I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file.


This is in the FAQ here:
http://www.eskimo.com/~scs/C-faq/q12.38.html

Nick.

Nov 14 '05 #2
Eik wrote:
Hey,

I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file.
[...]
This is Question 12.38 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
Ohhh... and does anybody know how you can get the size of an file
without checking where the EOF byte is (because in binairy file's
there are more EOF's)


.... and this is Question 19.12. Two for two: Does that
suggest anything about the utility of reading the FAQ
before posting, hmmm?

--
Er*********@sun.com

Nov 14 '05 #3
Eik <st*****@hotmail.com> wrote:
Hey, I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file. Take this example: ---BEGIN PROGRAM--- #include <stdio.h> main () {
Better make that

int main()

to be ready for C99 compliant compilers that require it.
int i;
unsigned char c;
FILE *fp;

fp = fopen ("chars.txt", "r");
Open the file in binary mode, i.e. use "rb" instead of just "r", this
should cure the 0x1A problem. As far as I know in text mode Windows
interprets the 0x1A (aka ^Z) as an end-of-file marker and then seems
to have problems when you want to read more from the file;-)
for (i = 0; i < 256; i++) {
c = getc (fp);
getc() returns an int, so you better get used to storing its return
value into one, see more about why below.
printf ("%3d|", c);
}
You're missing the return value of the program - even if you
don't specify the return type of main() it's still supposed
to return an int. It's usually a ood idea to return EXIT_SUCCESS
on success (but if you do that don't forget to include <stdlib.h>).
} Ohhh... and does anybody know how you can get the size of an file
without checking where the EOF byte is (because in binairy file's
there are more EOF's)


In C there doesn't exist an EOF byte. EOF is a value that won't
fit into a char (and that's why you always should store the return
value of functions like getc() in an int, not a char, otherwise
you're unable to detect the end of the file).

But there are some OSes that take your much beloved character with
the value 0x1A (^Z) as an end-of-file marker when the file gets
read in text mode. But "end-of-file marker" is something different
from EOF. The end-of-file marker is a completely legal character
that can happen to be in every file, it just gets assigned a
special meaning when a file gets read in text mode on some systems.
In contrast, EOF is not a character at all but just a possible
return value of some functions (like getc()), indicating that the
end of the file has been reached. Often EOF if -1 and that explains
why you always got 255 after the ^Z: When you write -1 in binary
(on your system) all the bits of that number are set, and when you
now just take the lowest 8 and interpret them as an unsigned char
you end up with, voila, 255.

To see how to figure out the length of a file see the FAQ, section
19.12, but you will probably end up using some system specific
function if you really need that piece of information.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
st*****@hotmail.com (Eik) writes:
I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file.


Open the file in binary mode: "rb" instead of "r" on fopen().
Nov 14 '05 #5
Eik wrote:
[...]
#include <stdio.h>

main () {
int i;
unsigned char c;
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
printf ("%3d|", c);
}
}

[...]

Open the file in binary mode. If you are on a platform that treats
Ctrl-Z in a text file as EOF, then getc() will return EOF.

(Also, note that getc returns "int" not "unsigned char".)

Try the following (still using text mode) program:

=============

#include <stdio.h>

main () {
int i;
int c; /* <------------- change */
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
if ( c == EOF ) /* <-------------- insert */
{
printf("\nEnd of file reached.\n");
break;
}
printf ("%3d|", c);
}
}

=============

Then change the fopen to use "rb" instead of "r".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #6
[snips]

On Fri, 16 Jul 2004 11:39:14 -0700, Eik wrote:
fp = fopen ("chars.txt", "r"); Why is the 26-byte ruining my program? How should I change my program?


Simple: you're using a DOS-based system (DOS or Windows), in which
character value 26, in a text file, means EOF. You're opening the file in
text mode, so when it hits that character, you're at the end of the file.

Either lose the character 26 - the EOF - or open the file in binary mode,
which doesn't care what the values are.

On a related note, using a char type for the character read back is a bad
idea; if there _is_ an EOF returned - and you need to differentiate it
from real data - you can't. Those functions return an int for a reason.
Nov 14 '05 #7
Eik
Kenneth Brody <ke******@spamcop.net> wrote in message news:<40***************@spamcop.net>...
Eik wrote:
[...]
#include <stdio.h>

main () {
int i;
unsigned char c;
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
printf ("%3d|", c);
}
}

[...]

Open the file in binary mode. If you are on a platform that treats
Ctrl-Z in a text file as EOF, then getc() will return EOF.

(Also, note that getc returns "int" not "unsigned char".)

Try the following (still using text mode) program:

=============

#include <stdio.h>

main () {
int i;
int c; /* <------------- change */
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
if ( c == EOF ) /* <-------------- insert */
{
printf("\nEnd of file reached.\n");
break;
}
printf ("%3d|", c);
}
}

=============

Then change the fopen to use "rb" instead of "r".


Thanx man
Nov 14 '05 #8
When you open a file in text mode, you are telling the compiler that the
expectation is that the contents of that file are one-byte ASCII codes and
that you want it to be aware of that and act accordingly. In addition to
reading them out of the file, your compiler will insert the code to do
certain things when certain values are read (which depend on your OS and
your compiler) because that is what that ASCII code tells it to do. 0x1A is
'Z' or 'z' with bits 5 and 6 cleared, which makes it Ctl-Z. In DOS text
files, that is used as the end-of-file marker. So your code is only doing
exactly what you told it to do.

If you are storing binary data in a file - read it as a binary file.

"Eik" <st*****@hotmail.com> wrote in message
news:75**************************@posting.google.c om...
Hey,

I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010:
SUB [Substitute]) from a file.

Take this example:

---BEGIN PROGRAM---

#include <stdio.h>

main () {
int i;
unsigned char c;
FILE *fp;

fp = fopen ("chars.txt", "r");
for (i = 0; i < 256; i++) {
c = getc (fp);
printf ("%3d|", c);
}
}

---END PROGRAM---

The file chars.txt contains 256 bytes -> byte values: 0, 1, 2, ...,
253, 254, 255

When I run the program I get this:

0| 1| 2| ... 24| 25|255|255|255| ... etc. Til 'i' reaches 256 and
the program stops shitting '255' (EOF's) on my screen

What is wrong with the 26-byte?!?
When I remove the 26-byte from chars.txt, I get this:

0| 1| 2| ... 24| 25| 27| 28| ... 253|254|255|255|

Why is the 26-byte ruining my program? How should I change my program?

THX

Ps. I'm using Windows XP, and the C-compiler Miracle C

Ohhh... and does anybody know how you can get the size of an file
without checking where the EOF byte is (because in binairy file's
there are more EOF's)

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.