简体   繁体   English

从C中的文本文件读取

[英]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. 然后,我读取了3个16字节的数据块。 Then 1 20-byte block and then I would like to read 1 byte at the end as an integer. 然后是1个20字节的块,然后我想以整数读取最后的1个字节。 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. 我的问题是,当我从文件中读取它而不是整数值15时,它是49。我在ACII表中进行了检查,它不是十六进制或八进制值1或5。我认为是正确的read语句为read(inF, pad, 1) 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. 我确实知道一个整数变量是4个字节,但是文件中只剩下一个字节的数据,所以我只读了最后一个字节。
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? 总结一下我的问题,当我从文本文件中读取存储15的字节时,为什么不能从整数表示中将其视为15? Any help would be very appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

You're reading a first byte of int (4 bytes), and then print it as a whole. 您正在读取int的第一个字节(4个字节),然后将其整体打印。 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:

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 . 请注意,这仅适用于具有32位整数的平台,并且还取决于平台的字节顺序

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. 基于读取功能,我认为它正在读取整数的4个字节的第一个字节中的第一个字节,并且该字节未放置在最低字节中。 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). 这意味着即使将其初始化为零,填充中其他3个字节的内容仍将存在(然后在其他字节中为零)。 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: 我将读取一个字节,然后将其转换为整数(如果出于某种原因需要4字节整数),如下所示:

/* 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. 否则,您可以将声明更改为无符号字符,这将处理其他3个字节。

The read function system call has a declaration like: read函数系统调用具有如下声明:

 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. 因此,您应该传递要在其中读取内容的int变量的地址。 ie use 即使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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