当前位置:网站首页>Text reading end judgment
Text reading end judgment
2022-07-25 02:27:00 【Leg hair Ger】
1. Whether the reading of text file is finished , Determine whether the return value is EOF ( fgetc ), perhaps NULL ( fgets )
for example : fgetc Judge whether it is EOF .
fgets Determine whether the return value is NULL .
2. Judgment of reading end of binary file , Judge whether the return value is less than the actual number to be read . for example : fread Judge whether the return value is less than the actual number to be read
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c; // Be careful :int, Not char, Ask to deal with EOF
FILE* fp = fopen("test.txt", "r");
if(!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//fgetc When reading fails or the end of the file is encountered , Will return to EOF
while ((c = fgetc(fp)) != EOF) // standard C I/O Read file cycle
{
putchar(c);
}
// Judge why it ended
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
}
Examples of binary files :
#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{
double a[SIZE] = {1.,2.,3.,4.,5.};
FILE *fp = fopen("test.bin", "wb"); // Binary mode must be used
fwrite(a, sizeof *a, SIZE, fp); // Write double Array of
fclose(fp);
double b[SIZE];
fp = fopen("test.bin","rb");
size_t ret_code = fread(b, sizeof *b, SIZE, fp); // read double Array of
if(ret_code == SIZE) {
puts("Array read successfully, contents: ");
for(int n = 0; n < SIZE; ++n) printf("%f ", b[n]);
putchar('\n');
} else { // error handling
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp)) {
perror("Error reading test.bin");
}
}
fclose(fp);
}
边栏推荐
- Computing network, AI first, shengteng AI helps operators' Digital Transformation -- work together to win-win Computing Era
- Standard transfer function
- [leetcode] 2. Add two numbers - go language problem solving
- Detailed explanation of the principles and differences between static pages and dynamic pages
- Deep understanding of string class
- Sword finger offer 11. rotate the minimum number of the array
- Peripherals: interrupt system of keys and CPU
- Data integration | what are the tools for data integration at home and abroad?
- DNA helped solve the outstanding case 30 years ago. The suspect strangled his girlfriend because he fell in love with his roommate. He was already the CEO of the technology company when he was arreste
- When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
猜你喜欢

Simulate the implementation of strstr

VRRP virtual redundancy protocol configuration

What are the important trends revealed by the release of "operator data viability index"?

DNA helped solve the outstanding case 30 years ago. The suspect strangled his girlfriend because he fell in love with his roommate. He was already the CEO of the technology company when he was arreste

Peripherals: interrupt system of keys and CPU

Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022

Unable to display spline in UE4 (unreal engine4) terrain editing tool

Inventory of well-known source code mall systems at home and abroad

Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem

Win10 configuring CUDA and cudnn
随机推荐
About the relationship between parent process and child process (UAC bypass idea)
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
Vs2019 configuring Qt5 development environment
Ecosystem long-term observation data product system
[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.3 - smart network card usage scenario - network acceleration implementation
Explorer TSSD 2019 software installation package download and installation tutorial
Scalar, vector, matrix calculus
Redis tutorial
DNA helped solve the outstanding case 30 years ago. The suspect strangled his girlfriend because he fell in love with his roommate. He was already the CEO of the technology company when he was arreste
In the post deep learning era, where is the recommendation system going?
Using the artifact typora+picgo, Youdao cloud can automatically upload pictures in markdown without members
String class
Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
How to upload files to the server
Use SAP ui5 application to consume create and delete operations of OData in business application studio
Talk about what's going on with C # backstage GC?
Open sharing of scientific data in the context of open science: the practice of the national Qinghai Tibet Plateau scientific data center
Summary thinking caused by the function of a SMS verification code [easy to understand]
High performance memory recovery technology for decrypting ark -- HPP GC
Today in history: the kotlin language was published for the first time; The father of the IMAP agreement was born; CT imaging achieves a new breakthrough