简体   繁体   English

C语言中的字符阅读

[英]character reading in C

I am struggling to know the difference between these functions. 我正在努力了解这些功能之间的区别。 Which one of them can be used if i want to read one character at a time. 如果我想一次读取一个字符,则可以使用其中之一。

fread()

read()

getc()

Depending on how you want to do it you can use any of those functions. 根据您要执行的操作,可以使用任何这些功能。

The easier to use would probably be fgetc() . 更容易使用的可能是fgetc()

fread() : read a block of data from a stream ( documentation ) fread() :从流中读取数据块( 文档

read() : posix implementation of fread() ( documentation ) read()fread() posix实现( 文档

getc() : get a character from a stream ( documentation ). getc() :从流中获取字符( 文档 )。 Please consider using fgetc() ( doc )instead since it's kind of saffer. 请考虑使用fgetc()doc )代替,因为它有点麻烦。

fread() is a standard C function for reading blocks of binary data from a file. fread()是用于从文件读取二进制数据块的标准C函数。

read() is a POSIX function for doing the same. read()是用于执行此操作的POSIX函数。

getc() is a standard C function (a macro, actually) for reading a single character from a file - ie, it's what you are looking for. getc()是一个标准的C函数(实际上是一个宏),用于从文件中读取单个字符-也就是说,这就是您要查找的内容。

In addition to the other answers, also note that read is unbuffered method to read from a file. 除了其他答案外,还请注意read是从文件read的非缓冲方法。 fread provides an internal buffer and reading is buffered. fread提供一个内部缓冲区,并且读取被缓冲。 The buffer size is determined by you. 缓冲区大小由您决定。 Also each time you call read a system call occurs which reads the amount of bytes you told it to. 同样,每次调用read时,都会发生系统调用,该系统调用读取告诉您的字节数。 Where as with fread it will read a chunk in the internal buffer and return you only the bytes you need. fread ,它将读取内部缓冲区中的一个块,并仅返回所需的字节。 For each call on fread it will first check if it can provide you with more data from the buffer, if not it makes a system call ( read ) and gets a chunk more data and returns you only the portion you wanted. 对于fread每个调用,它将首先检查它是否可以从缓冲区为您提供更多数据,否则,将进行系统调用( read )并获取更多数据,并仅返回您想要的部分。 Also read directly handles the file descriptor number, where fread needs the file to be opened as a FILE pointer. read直接read来处理文件描述符号,其中fread需要将文件作为FILE指针打开。

The answer depends on what you mean by "one character at a time". 答案取决于您“一次一个字符”的意思。

If you want to ensure that only one character is consumed from the underlying file descriptor (which may refer to a non-seekable object like a pipe, socket, or terminal device) then the only solution is to use read with a length of 1. If you use strace (or similar) to monitor a shell script using the shell command read , you'll see that it repeatedly calls read with a length of 1. Otherwise it would risk reading too many bytes (past the newline it's looking for) and having subsequent processes fail to see the data on the "next line". 如果要确保仅从基础文件描述符(可能指向管道,套接字或终端设备等不可搜索的对象)使用一个字符,则唯一的解决方案是使用长度为1的read 。如果您使用strace (或类似方法)通过shell命令read来监视shell脚本,您将看到它重复调用长度为1的read 。否则,将冒读取过多字节的风险(粘贴正在查找的换行符)并且使后续进程无法在“下一行”中看到数据。

On the other hand, if the only program that should be performing further reads is your program itself, fread or getc will work just fine. 另一方面,如果应该执行进一步读取的唯一程序是您的程序本身,则freadgetc可以正常工作。 Note that getc should be a lot faster than fread if you're just reading a single byte. 需要注意的是getc应该比快很多 fread如果你只是读一个字节。

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

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