C程序读取整个文件的内容
C 编程语言支持四个预定义函数从文件中读取内容,定义在 stdio.h 头文件中:
- fgetc() -此函数用于从文件中读取单个字符。
- fgets() -此函数用于从文件中读取字符串。
- fscanf() -此函数用于从文件中读取原始字节块。这用于读取二进制文件。
- fread() -此函数用于从文件中读取格式化输入。
读取文件的步骤:
- 使用函数fopen() 打开文件并将文件的引用存储在 FILE 指针中。
- 使用这些函数 fgetc()、fgets()、fscanf() 或 fread() 读取文件的内容。
- 文件 使用函数fclose() 关闭文件。
让我们开始详细讨论这些功能中的每一个。
fgetc()
fgetc() 读取当时函数指针指向的字符。每次成功读取时,它都会返回从流中读取的字符(ASCII 值)并将读取位置推进到下一个字符。当没有要读取的内容或读取不成功时,此函数返回一个常量 EOF (-1)。
句法:
int fgetc(FILE *ptr);
方法:
- 该程序读取文件的全部内容,使用此函数逐个读取字符。
- 将使用 Do-While 循环,该循环将读取字符,直到它到达文件。
- 当它到达结束时,它返回 EOF字符(-1)。
使用 EOF:
下面是实现上述方法的C程序-
C
// C program to implement
// the above approach
#include
#include
#include
// Driver code
int main()
{
FILE* ptr;
char ch;
// Opening file in reading mode
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
// Printing what is written in file
// character by character using loop.
do {
ch = fgetc(ptr);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop eading.
} while (ch != EOF);
// Closing the file
fclose(ptr);
return 0;
}
C
// C program to implement
// the above approach
#include
#include
#include
// Driver code
int main()
{
FILE* ptr;
char ch;
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
while (!feof(ptr)) {
ch = fgetc(ptr);
printf("%c", ch);
}
fclose(ptr);
return 0;
}
C
// C program to implement
// the above approach
#include
#include
#include
// Driver code
int main()
{
FILE* ptr;
char str[50];
ptr = fopen("test.txt", "a+");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
while (fgets(str, 50, ptr) != NULL) {
printf("%s", str);
}
fclose(ptr);
return 0;
}
C++
// C program to implement
// the above approach
#include
// Driver code
int main()
{
FILE* ptr = fopen("abc.txt", "r");
if (ptr == NULL) {
printf("no such file.");
return 0;
}
/* Assuming that test.txt has content
in below format
NAME AGE CITY
abc 12 hyderbad
bef 25 delhi
cce 65 bangalore */
char buf[100];
while (fscanf(ptr, "%*s %*s %s ",
buf)
== 1)
printf("%s\n", buf);
return 0;
}
C++
// C program to implement
// the above approach
#include
#include
#include
// Structure to store
// course details
struct Course {
char cname[30];
char sdate[30];
};
// Driver code
int main()
{
FILE* of;
of = fopen("test.txt", "w");
if (of == NULL) {
fprintf(stderr,
"\nError to open the file\n");
exit(1);
}
struct Course inp1 = { "Algorithms",
"30OCT" };
struct Course inp2 = { "DataStructures",
"28SEPT" };
struct Course inp3 = { "Programming",
"1NOV" };
fwrite(&inp1, sizeof(struct Course),
1, of);
fwrite(&inp2, sizeof(struct Course),
1, of);
fwrite(&inp3, sizeof(struct Course),
1, of);
if (fwrite != 0)
printf("Contents to file written successfully !\n");
else
printf("Error writing file !\n");
fclose(of);
// File pointer to read from file
FILE* inf;
struct Course inp;
inf = fopen("test.txt", "r");
if (inf == NULL) {
fprintf(stderr,
"\nError to open the file\n");
exit(1);
}
while (fread(&inp, sizeof(struct Course),
1, inf))
printf("Course Name = %s Started = %s\n",
inp.cname, inp.sdate);
fclose(inf);
}
输入文件:
GeeksforGeeks | A computer science portal for geeks
输出:
在上面的代码中,方法是从文件中读取一个字符并检查它是否不是EOF,如果不是则打印它,如果是则停止读取。
使用 feof():
feof()函数将文件指针作为参数,如果指针到达文件末尾,则返回 true。
句法:
int feof(FILE *ptr);
方法:
- 在这种方法中,使用 fgetc() 读取字符。
- 使用 feof()函数检查文件结尾。因为 feof() 到达末尾后返回 true。
- 使用逻辑非运算符(!),以便在达到结束条件时变为假并停止循环。
下面是实现上述方法的 C 程序:
C
// C program to implement
// the above approach
#include
#include
#include
// Driver code
int main()
{
FILE* ptr;
char ch;
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
while (!feof(ptr)) {
ch = fgetc(ptr);
printf("%c", ch);
}
fclose(ptr);
return 0;
}
输入文件:
GeeksforGeeks | A computer science portal for geeks
输出:
fgets()
fgets() 从文件中一次读取一个字符串。 fgets() 如果被函数成功读取,则返回一个字符串,如果无法读取,则返回 NULL。
句法:
char * fgets(char *str, int size, FILE * ptr);
Here,
str: It is string in which fgets() store string after reading it from file.
size: It is maximum characters to read from stream.
ptr: It is file pointer.
方法:
- 在这种方法中,文件的内容一次读取一个字符,直到我们到达文件末尾。
- 当我们到达文件的末尾时 fgets() 无法读取并返回 NULL 并且程序将停止读取。
下面是实现上述方法的 C 程序:
C
// C program to implement
// the above approach
#include
#include
#include
// Driver code
int main()
{
FILE* ptr;
char str[50];
ptr = fopen("test.txt", "a+");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
while (fgets(str, 50, ptr) != NULL) {
printf("%s", str);
}
fclose(ptr);
return 0;
}
输入文件:
GeeksforGeeks | A computer science portal for geeks
输出:
fscanf()
fscanf() 从流中读取格式化的输入。
句法:
int fscanf(FILE *ptr, const char *format, …)
方法:
- fscanf 从文件中读取格式化数据并将其存储在变量中。
- 缓冲区中的数据会打印在控制台上,直到到达文件末尾。
C++
// C program to implement
// the above approach
#include
// Driver code
int main()
{
FILE* ptr = fopen("abc.txt", "r");
if (ptr == NULL) {
printf("no such file.");
return 0;
}
/* Assuming that test.txt has content
in below format
NAME AGE CITY
abc 12 hyderbad
bef 25 delhi
cce 65 bangalore */
char buf[100];
while (fscanf(ptr, "%*s %*s %s ",
buf)
== 1)
printf("%s\n", buf);
return 0;
}
输出:
fread()
fread() 可以更轻松地从文件中读取数据块。例如,在从文件中读取结构的情况下,使用 fread 读取变得很容易。
句法:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
ptr: This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each element to be read.
nmemb: This is the number of elements, each one with a size of size bytes.
stream: This is the pointer to a FILE object that specifies an input stream.
方法:
- 它首先从给定的输入流中读取对象的计数,每个对象的大小为 size 字节。
- 如果成功,则读取的总字节数为 (size*count)。
- 根据编号。读取的字符数,指示器文件位置递增。
- 如果读取的对象不是一般可复制的,则行为未定义,如果 size 或 count 的值等于 0,则该程序将简单地返回 0。
C++
// C program to implement
// the above approach
#include
#include
#include
// Structure to store
// course details
struct Course {
char cname[30];
char sdate[30];
};
// Driver code
int main()
{
FILE* of;
of = fopen("test.txt", "w");
if (of == NULL) {
fprintf(stderr,
"\nError to open the file\n");
exit(1);
}
struct Course inp1 = { "Algorithms",
"30OCT" };
struct Course inp2 = { "DataStructures",
"28SEPT" };
struct Course inp3 = { "Programming",
"1NOV" };
fwrite(&inp1, sizeof(struct Course),
1, of);
fwrite(&inp2, sizeof(struct Course),
1, of);
fwrite(&inp3, sizeof(struct Course),
1, of);
if (fwrite != 0)
printf("Contents to file written successfully !\n");
else
printf("Error writing file !\n");
fclose(of);
// File pointer to read from file
FILE* inf;
struct Course inp;
inf = fopen("test.txt", "r");
if (inf == NULL) {
fprintf(stderr,
"\nError to open the file\n");
exit(1);
}
while (fread(&inp, sizeof(struct Course),
1, inf))
printf("Course Name = %s Started = %s\n",
inp.cname, inp.sdate);
fclose(inf);
}
输出: