1

I am having trouble reading a specific integer from a file and I am not sure why. First I read through the entire file to find out how big it is, and then I reset the pointer to the beginning. I then read 3 16-byte blocks of data. Then 1 20-byte block and then I would like to read 1 byte at the end as an integer. However, I had to write into the file as a character but I do not think that should be a problem. My issue is that when I read it out of the file instead of being the integer value of 15 it is 49. I checked in the ACII table and it is not the hex or octal value of 1 or 5. I am thoroughly confused because my read statement is read(inF, pad, 1) which I believe is right. I do know that an integer variable is 4 bytes however, there is only one byte of data left in the file so I read in only the last byte.
My code is reproduced the function(it seems like a lot but it don't think it is)

the code is

#include<math.h>
#include<stdio.h>
#include<string.h>
#include <fcntl.h>


int main(int argc, char** argv)
{
char x;
int y;
int bytes = 0;
int num = 0;
int count = 0;



num = open ("a_file", O_RDONLY);

bytes = read(num, y, 1);

printf("y %d\n", y);

return 0;
}

To sum up my question, how come when I read the byte that stores 15 from the text file, I can't view it as 15 from the integer representation? Any help would be very appreciated. Thanks!

3
  • 1
    I can't quite find out what you're doing with the data, so you probably should reduce your code to something simpler, like "read a number from a file" and "write a number to a file" -- it's much easier to experiment with. Commented Mar 11, 2012 at 23:51
  • @che I changed the code to something similar but very simple, I still have the same problem though, do you have a suggestion? Commented Mar 12, 2012 at 1:44
  • 1
    Here's a clue: 49 is the decimal value of the ASCII character '1'. Commented Mar 12, 2012 at 2:01

4 Answers 4

1

You're reading a first byte of int (4 bytes), and then print it as a whole. If you want to read by one byte, you need also to use it as one byte, like this:

char temp; // one-byte signed integer
read(fd, &temp, 1); // read the integer from file
printf("%hhd\n", temp); // print one-byte signed integer

Or, you can use regular int:

int temp; // four byte signed integer
read(fd, &temp, 4); // read it from file
printf("%d\n", temp); // print four-byte signed integer

Note that this will work only on platforms with 32-bit integers, and also depends on platform's byte order.

What you're doing is:

int temp; // four byte signed integer
read(fd, &temp, 1); // read one byte from file into the integer
   // now first byte of four is from the file,
   // and the other three contain undefined garbage
printf("%d\n", temp); // print contents of mostly uninitialized memory
Sign up to request clarification or add additional context in comments.

Comments

0

The read function system call has a declaration like:

 ssize_t read(int fd, void* buf, size_t count);

So, you should pass address of the int variable in which you want to read the stuff. i.e use

 bytes = read(num, &y, 1);

Comments

0

You can see all the details of file I/O in C from that link

Comments

0

Based on the read function, I believe it is reading the first byte in the first byte of the 4 bytes of the integer, and that byte is not placed in the lowest byte. This means whatever is in pad for the other 3 bytes will still be there, even if you initialized it to zero (then it will have zeros in the other bytes). I would read in one byte and then cast it to an integer (if you need a 4 byte integer for some reason), as shown below:

/* declare at the top of the program */
char temp;

/* Note line to replace  read(inF,pad,1) */
read(inF,&temp,1);

/* Added to cast the value read in to an integer high order bit may be propagated to make a negative number */
pad = (int) temp;

/* Mask off the high order bits */
pad &= 0x000000FF;

Otherwise, you could change your declaration to be an unsigned char which would take care of the other 3 bytes.

1 Comment

You are correct. I forgot to indicate the function wants the address of temp. Changed the code to reflect the correction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.