简体   繁体   中英

Reading from a text file in C

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!

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

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.

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. ie use

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

您可以从该链接查看 C中文件I / O的所有详细信息

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM