当前位置:网站首页>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);
}
边栏推荐
- Redux best practices "Redux toolkit"
- Gerrit statistics script
- Standard transfer function
- Details of C language compilation preprocessing and comparison of macros and functions
- Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
- Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
- In the post deep learning era, where is the recommendation system going?
- Nacos service discovery data model
- Jedispoolconfig parameter configuration from the perspective of source code
- Speed comparison between 64 bit width and 32 bit width of arm64 memory
猜你喜欢

An article explains unsupervised learning in images in detail

Redux best practices "Redux toolkit"

In the post deep learning era, where is the recommendation system going?

R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set

Simulate the implementation of StrCmp function

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

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Windows Server 2022 received a non security update in July: fix the sticking problem caused by defender

How to judge which star you look like?

Vs2019 configuring Qt5 development environment
随机推荐
Query the thread information of MySQL server (detailed explanation)
MySQL advanced (13) command line export import database
How to judge which star you look like?
Keepalivetime=0 description of ThreadPoolExecutor
"I gave up programming and wrote a 1.3 million word hard science fiction."
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!
Standard transfer function
Cloud native platform, let edge applications play out!
ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
On the economic model of open source ecology
Hongmeng harmonyos 3 official announcement: officially released on July 27; Apple slowed down the recruitment and expenditure of some teams in 2023; Russia fined Google 2.6 billion yuan | geek headlin
Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
Pycharm writes SQLite statements without code prompts
"No such plugin: cloudbees folder" solution appears when Jenkins selects the recommended plug-in for the first time
Nacos service discovery data model
Full analysis of new functions of report design tool FastReport online designer v2022.1
UDP message structure and precautions
Cloudrev deploy your own public cloud disk (pagoda installation method)
Redux best practices "Redux toolkit"