文件末尾eof_什么是EOF(文件末尾)? PHP,C ++,C,Python,Java的示例

文件末尾eof

文件末尾eof

Files contain different types of data like text, image, video, headers, graphics, etc. All these data are stored in different encoding and formatting techniques but every file has an end which is named End Of File which sets the last by of the given file. In this tutorial, we will learn the meaning of the End Of File and relation with the popular programming languages like C, C++, PHP, Java, Python.

文件包含不同类型的数据,例如文本,图像,视频,标题,图形等。所有这些数据均以不同的编码和格式设置技术存储,但是每个文件都有一个名为End Of File的末尾,该末尾设置了给定的最后一个文件。 在本教程中,我们将学习End Of File的含义以及与流行的编程语言(如C,C ++,PHP,Java,Python)的关系。

什么是文件结尾? (What Is End Of File?)

End Of File is the special data or delimiter which will set the end of file for a specific file. This file contains different types of data from text to image but the end of the file is the same for all of them. End Of File can be also expressed as EOF in a short form. EOF is also used in different programming languages in order to express and check the end of the file.

文件结尾是特殊数据或定界符,它将为特定文件设置文件结尾。 该文件包含从文本到图像的不同类型的数据,但是文件的结尾对于所有这些文件都是相同的。 文件结尾也可以用EOF的短形式表示。 EOF也用在不同的编程语言中,以表示和检查文件的结尾。

Checking the end of the file is important especially developing applications. While reading a file to process, print or view we need to check the end of file in some cases especially in low-level operations.

检查文件结尾很重要,尤其是在开发应用程序时。 在某些情况下,尤其是在低级操作中,在读取文件进行处理,打印或查看时,我们需要检查文件的末尾。

C和C ++中的文件结尾 (End Of File In C and C++)

C and C++ provide different file operation functions. We can use EOF value in order to check the end of the file which can be used to check different functions return value. EOF stores the -1 where a file operation function will return -1 when the end of file is reached.

C和C ++提供了不同的文件操作功能。 我们可以使用EOF值来检查文件的结尾,该文件可以用来检查不同函数的返回值。 EOF存储-1 ,到达文件末尾时文件操作函数将返回-1

In the following example, we will read the file named myfile.txt with the getc() function which will read a single character from a given file for each time. We will check the EOF after every read operation.

在下面的示例中,我们将使用getc()函数读取名为myfile.txt的文件,该函数将每次从给定文件中读取单个字符。 每次读取操作后,我们都会检查EOF。

#include <stdio.h> 

int main() 
{ 
   FILE *fp = fopen("myfile.txt", "r"); 
   int ch = getc(fp);

   //Check enf of file and if not end execute while block
   //File EOF reached end while loop 
   while (ch != EOF) 
   { 
    /* Print the file content with ch */
    putchar(ch); 
        /* Read one character from file */
    ch = getc(fp); 
   } 
    
   if (feof(fp)) 
    printf("\n End of file reached."); 
   else
    printf("\n Something went wrong."); 
   fclose(fp); 
    
   getchar(); 

   return 0; 
}

PHP中的文件结尾 (End Of File In PHP)

PHP provides feof() function in order to check the end of the file. When there are some bytes or not end of file the feof() function will return false and the provided iteration will continue till to end of the file.

PHP提供了feof()函数来检查文件的结尾。 当文件末尾有字节或没有字节时,feof()函数将返回false,并且所提供的迭代将持续到文件末尾。

<?php 

// We will open the myfile.txt and set to variable $check 
$check = fopen("myfile.txt", "r"); 

$seq = fgets($check); 

// Outputs a line of the file until 
// the end-of-file is reached 
while(! feof($check)) 
{ 
echo $seq ; 
$seq = fgets($check); 
} 

// We will close the file with fclose() function 
fclose($check); 

?>

Java中的文件结尾 (End Of File In Java)

Java programming language provides different functions in order to read, write files. In Java when a file is read the value generally stored in a variable like a String. If the end of the file is reached the returned value will be a null which is simply nothing. We can check the end of the file if the returned value is null like below.

Java编程语言提供了不同的功能以便读取和写入文件。 在Java中,当读取文件时,该值通常存储在诸如String之类的变量中。 如果到达文件的末尾,则返回的值将为null ,即为null 。 我们可以检查文件的结尾是否是返回的null,如下所示。

import java.io.*;
import java.util.*;

public class End_Of_File_Example{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String ab= scanner.nextLine();
        int a=0;

        while(ab != null){
            System.out.printf("%d %s\n",++a,ab);
            ab=scanner.nextLine();
        }
        scanner.close();
    }
}

Python中的文件结尾 (End Of File In Python)

In Python, there is no specific EOF function but we can use some techniques like checking line we read and determine the EOF. We will read the file line by line with while loop. If the end of the file is reached the returned line value will be null.

在Python中,没有特定的EOF函数,但是我们可以使用一些技术,例如检查读取的行并确定EOF。 我们将通过while循环逐行读取文件。 如果到达文件末尾,则返回的行值将为null。

# We will set the file name we want to read
filename = "myfile.txt" 

# We open file with open() function to only read
filehandle= open(filename, 'r') 

while True: 
   #Read single line   
   line = filehandle.readline() 
 
   #Check line if it is not null
   #If line is null this means EOF
   if not line: 
      break print(line) 

# Close the file handler 
filehandle.close()
LEARN MORE  Multiline Strings in Python
在Python中了解更多多行字符串

翻译自: https://www.poftut.com/what-is-eof-end-of-file-examples-with-php-c-cpp-python-java/

文件末尾eof

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值