文件处理是使用程序将数据存储在文件中。在C语言中,程序使用C语言中的文件处理功能将结果和程序的其他数据存储到文件中。而且,我们可以从文件中提取/获取数据以在程序中使用它。
您可以在C中的文件上执行的操作是-
创建一个新文件
打开现有文件
从现有文件读取数据
将数据写入文件
将数据移动到文件上的特定位置
关闭档案
fopen()该fopen()函数用于在C中创建新文件或打开现有文件。fopen函数在stdio.h头文件中定义。
现在,让我们看看创建新文件或打开文件的语法
file = fopen(“file_name”, “mode”)
这是在C中打开和创建文件的通用语法。
file_name-这是一个字符串,它指定要使用fopen方法打开或创建的文件的名称。模式:这是一个字符串(通常是一个字符),指定了打开文件的模式。有很多可用的模式可以在C中打开文件,我们将在本文的后面部分中介绍所有这些模式。
当fopen函数在指定位置找不到指定名称的任何文件时,它将创建一个新文件。否则,如果找到该文件,它将以指定的模式打开。
让我们看一下可以使概念更清楚的示例。假设我们正在使用fopen函数打开一个名为hello.txt的文件。以下是声明,
file = fopen(“hello.txt”, “w”)
这将在当前目录中搜索名为hello.txt的文件。如果文件存在,它将打开文件,否则将创建一个名为“ hello.txt”的新文件,并以写入模式(使用“ w”指定)打开。
现在,让我们看看所有可用的模式来供我们使用C读写文件,并查看代码片段,这些代码片段将显示示例代码运行。
模式=“ r” -打开以供读取,此模式将仅出于读取目的打开文件,即只能查看内容,不能对文件进行任何其他编辑操作。
如果我们尝试使用此模式创建新文件,则此模式无法创建新文件,并且open()返回NULL。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "r")){
printf("File opened successfully in read mode");
}
else
printf("The file is not present! cannot create a new file using r mode");
fclose(file);
return 0;
}输出结果
File opened successfully in read mode
我们在当前目录中创建了一个名为hello.txt的文件,但是如果尝试访问其他文件,则会得到“该文件不存在!无法使用r模式创建新文件”作为输出。
Mode =“ rb” -打开以二进制模式读取,此模式将仅以二进制模式打开文件读取,即只能查看内容,而不能像对其进行编辑一样。
如果我们尝试使用此模式创建新文件,则此模式无法创建新文件,并且open()返回NULL。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("program.txt", "rb")){
printf("File opened successfully in read mode");
}
else
printf("The file is not present! cannot create a new file using rb mode");
fclose(file);
return 0;
}输出结果
该文件不存在!无法使用rb模式创建新文件
模式=“ w” -仅用于写入打开,如果当前目录中存在该文件仅用于写入,则此模式将打开文件,即无法执行读取操作。如果当前目录中不存在该文件,则程序将创建一个新文件并打开以进行写入。
如果我们打开其中包含一些文本的文件,则内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("helo.txt", "w")){
printf("File opened successfully in write mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
File opened successfully in write mode or a new file is created
您可以在此处看到,我们试图打开目录中不存在的文件“ helo.txt”,但该函数仍返回成功消息,因为它已创建了一个名为“ helo.txt”的文件。
模式=“ wb” -打开以二进制模式写入,如果当前目录中存在文件,则以该模式打开文件以二进制模式写入,即无法执行读取操作。如果当前目录中不存在该文件,则程序将创建一个新文件并以二进制方式打开以进行写入。
如果我们打开其中包含一些文本的文件,则内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "wb")){
printf("File opened successfully in write in binary mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
以二进制模式写入成功打开文件或创建了新文件
模式=“ a” -仅打开用于追加,如果当前目录中存在该文件,则该模式将打开该文件仅用于写入,即无法执行读取操作。如果当前目录中不存在该文件,则程序将创建一个新文件并打开以进行写入。如果我们打开其中包含一些文本的文件,内容将不会被覆盖;而是将新文本添加到文件中现有文本之后。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "a")){
printf("File opened successfully in append mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
File opened successfully in append mode or a new file is created
模式=“ ab” -打开以二进制形式追加,如果当前目录中存在该文件,则该模式将打开该文件以仅以二进制形式写入,即无法执行读取操作。如果当前目录中不存在该文件,则程序将创建一个新文件并打开以二进制形式写入。
如果我们打开其中包含一些文本的文件,内容将不会被覆盖;而是将新文本添加到文件中现有文本之后。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "ab")){
printf("File opened successfully in append in binary mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
File opened successfully in append in binary mode or a new file is created
模式=“ r +” -打开以同时进行读取和写入,此模式将同时打开文件以进行读取和写入,即可以对文件执行读取和写入操作。
如果我们尝试使用此模式创建新文件,则此模式无法创建新文件,并且open()返回NULL。
如果我们打开一个包含一些文本的文件并写一些东西,内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "r+")){
printf("File opened successfully in read and write both");
}
else
printf("The file is not present! cannot create a new file using r+ mode");
fclose(file);
return 0;
}输出结果
File opened successfully in read and write both
我们在当前目录中创建了一个名为hello.txt的文件,但是如果尝试访问另一个文件,则会得到“该文件不存在!无法使用r +模式创建新文件”作为输出。
Mode =“ rb +” -打开以二进制模式读取,此模式将仅以二进制模式打开文件读取,即只能查看内容,而不能对其进行任何其他编辑。
如果我们尝试使用此模式创建新文件,则此模式无法创建新文件,并且open()返回NULL。
如果我们打开一个包含一些文本的文件并写一些东西,内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("program.txt", "rb+")){
printf("File opened successfully in read mode");
}
else
printf("The file is not present! cannot create a new file using rb+ mode");
fclose(file);
return 0;
}输出结果
The file is not present! cannot create a new file using rb+ mode
模式=“ w” -打开以进行写入和读取,如果当前目录中存在文件,则此模式将打开文件以进行写入和读取操作。如果当前目录中不存在该文件,则程序将创建一个新文件并打开该文件以进行读取和写入。
如果我们打开其中包含一些文本的文件,则内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("helo.txt", "w+")){
printf("File opened successfully in read-write mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
File opened successfully in read-write mode or a new file is created
您可以在此处看到,我们试图打开目录中不存在的文件“ helo.txt”,但该函数仍返回成功消息,因为它已创建了一个名为“ helo.txt”的文件。
Mode =“ wb +”:以二进制模式打开以进行读写,如果当前目录中存在该文件,则该模式将打开文件以供读写
二进制模式。如果当前目录中不存在该文件,则程序将创建一个新文件并打开该文件以二进制方式进行读写。如果我们打开其中包含一些文本的文件,则内容将被覆盖。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "wb+")){
printf("File opened successfully in read-write in binary mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
File opened successfully in read-write in binary mode or a new file is created
模式=“ a +” -打开以进行读取和追加,如果当前目录中存在该文件,则该模式将打开该文件以进行读取和写入。如果当前目录中不存在该文件,则程序将创建一个新文件并打开该文件以进行读取和写入。
如果我们打开其中包含一些文本的文件,内容将不会被覆盖;而是将新文本添加到文件中现有文本之后。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "a+")){
printf("File opened successfully in read-append mode or a new file is created");
}
else
printf("Error!");
fclose(file);
return 0;
}输出结果
在读取追加模式下成功打开文件或创建了新文件
模式=“ ab +” -打开以二进制形式读取和追加,此模式将打开文件(如果存在于当前目录中)以以二进制形式进行读取和写入。如果当前目录中不存在该文件,则程序将创建一个新文件并打开该文件以进行二进制读写。如果我们打开其中包含一些文本的文件,内容将不会被覆盖;而是将新文本添加到文件中现有文本之后。
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "ab+")){
printf("File opened successfully in read-append in binary mode or a new file is created");
}
else
printf("Error!”);
fclose(file);
return 0;
}输出结果
File opened successfully in read-append mode or a new file is created
我们可以使用fscanf()andfgets()和fgetc()函数读取c中文件的内容。全部用于读取文件的内容。让我们看看每个函数的工作原理-
该fscanf()方法用于读取字符集,即文件中的字符串。当文件的所有内容都被读取时,它将返回EOF。
int fscanf(FILE *stream, const char *charPointer[])
FILE *stream: the pointer to the opened file. const char *charPointer[]: string of character.
#include <stdio.h>
int main(){
FILE * file;
char str[500];
if (file = fopen("hello.txt", "r")){
while(fscanf(file,"%s", str)!=EOF){
printf("%s", str);
}
}
else
printf("Error!”);
fclose(file);
return 0;
}输出结果
Learnprogrammingatnhooo
fget()C语言中的函数用于从流中读取字符串。
char* fgets(char *string, int length, FILE *stream)
char *string: It is a string which will store the data from the string. int length: It is an int which gives the length of string to be considered. FILE *stream: It is the pointer to the opened file.
#include <stdio.h>
int main(){
FILE * file;
char str[500];
if (file = fopen("hello.txt", "r")){
printf("%s", fgets(str, 50, file));
}
fclose(file);
return 0;
}输出结果
Learn programming at nhooo.com
fgetc()C语言中的函数用于从文件中返回单个字符。它从文件中获取一个字符,并在文件末尾返回EOF。
char* fgetc(FILE *stream)
FILE *stream: It is the pointer to the opened file.
#include <stdio.h>
int main(){
FILE * file;
char str;
if (file = fopen("hello.txt", "r")){
while((str=fgetc(file))!=EOF)
printf("%c",str);
}
fclose(file);
return 0;
}输出结果
Learn programming at nhooo.com
我们可以用数据写入到在C文件fprintf(),fputs(),fputc()等功能。全部用于将内容写入文件。
让我们看看每个函数的工作原理-
该fprintf()方法用于将数据写入文件。它在文件中写入一组字符。
int fprintf(FILE *stream, char *string[])
FILE for *stream: It is the pointer to the opened file. char *string[]: It is the character array that we want to write in the file.
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "w")){
if(fprintf(file, "nhooo.com”) >= 0)
printf("Write operation successful");
}
fclose(file);
return 0;
}输出结果
Write operation successful
fputf()C中的函数可用于写入文件。它用于向文件写入一行(字符行)。
int fputs(const char *string, FILE *stream)
Constant char *string[]: It is the character array that we want to write in the file. FILE for *stream: It is the pointer to the opened file.
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "w")){
if(fputs("nhooo.com", file) >= 0)
printf("String written to the file successfully...");
}
fclose(file);
return 0;
}输出结果
String written to the file successfully…
该fputc()函数用于将单个字符写入文件。
int fputc(char character , FILE *stream)
char character : It is the character that we want to write in the file. FILE for *stream: It is the pointer to the opened file.
#include <stdio.h>
int main(){
FILE * file;
if (file = fopen("hello.txt", "w")){
fputc('T', file);
}
fclose(file);
return 0;
}输出结果
‘T’ is written to the file.
该fclose()方法用于关闭打开的文件。在对其执行操作后,我们应该关闭文件以保存已应用到该文件的操作。
fclose(FILE *stream)
FILE for *stream: It is the pointer to the opened file.
#include <stdio.h>
int main(){
FILE * file;
char string[300];
if (file = fopen("hello.txt", "a+")){
while(fscanf(file,"%s", string)!=EOF){
printf("%s", string);
}
fputs("Hello", file);
}
fclose(file);
return 0;
}输出结果
Learnprogrammingatnhooo
Learn programming at nhooo.comHello