到目前为止,使用 C 程序的操作是在没有存储在任何地方的提示/终端上完成的。但是在软件行业,大多数程序都是为了存储从程序中获取的信息而编写的。一种这样的方法是将获取的信息存储在文件中。可以对文件执行的不同操作包括:
示例 2:打开文件、读取文件并关闭文件的程序
示例 3:使用 fwrite() 写入二进制文件的程序
示例 4:使用 fread() 从二进制文件中读取的程序
- 创建一个新文件(fopen,属性为“a”或“a+”或“w”或“w++”)
- 打开现有文件 ( fopen )
- 从文件中读取(fscanf 或 fgets)
- 写入文件(fprintf 或 fputs)
- 移动到文件中的特定位置(fseek、rewind)
- 关闭文件 ( fclose )
- “r”——搜索文件。如果文件成功打开,fopen() 将其加载到内存中并设置一个指向其中第一个字符的指针。如果文件无法打开,fopen() 返回 NULL。
- “rb”——打开以二进制模式读取。如果文件不存在,fopen() 返回 NULL。
- “w”——搜索文件。如果文件存在,则覆盖其内容。如果该文件不存在,则创建一个新文件。如果无法打开文件,则返回 NULL。
- “wb”——打开以二进制模式写入。如果文件存在,则覆盖其内容。如果文件不存在,则会创建它。
- “a”——搜索文件。如果文件成功打开,fopen() 将其加载到内存中并设置一个指向其中最后一个字符的指针。如果该文件不存在,则创建一个新文件。如果无法打开文件,则返回 NULL。
- “ab”——以二进制模式打开以追加。数据被添加到文件的末尾。如果文件不存在,则会创建它。
- “r+”——搜索文件。如果成功打开 fopen() 将其加载到内存中并设置一个指向其中第一个字符的指针。如果无法打开文件,则返回 NULL。
- “rb+” –以二进制模式读写。如果文件不存在,fopen() 返回 NULL。
- “w+”——搜索文件。如果文件存在,则覆盖其内容。如果该文件不存在,则创建一个新文件。如果无法打开文件,则返回 NULL。
- “wb+” –以二进制模式读写。如果文件存在,则覆盖其内容。如果文件不存在,则会创建它。
- “a+”——搜索文件。如果文件成功打开,fopen() 将其加载到内存中并设置一个指向其中最后一个字符的指针。如果该文件不存在,则创建一个新文件。如果无法打开文件,则返回 NULL。
- “ab+” -以二进制模式打开以供读取和附加。如果文件不存在,则会创建它。
FILE *filePointer; So, the file can be opened as filePointer = fopen(“fileName.txt”, “w”)第二个参数可以更改为包含上表中列出的所有属性。
- 从 文件中读取--:
FILE * filePointer; filePointer = fopen(“fileName.txt”, “r”); fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
- 写入文件 –: 文件写入操作可以由函数 fprintf 和 fputs 执行,与读取操作相似。写入文件的代码片段如下:
FILE *filePointer ; filePointer = fopen(“fileName.txt”, “w”); fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);
- 关闭文件 –: 每次成功的文件操作后,您必须始终关闭文件。要关闭文件,您必须使用 fclose 函数。关闭文件的代码片段如下:
FILE *filePointer ; filePointer= fopen(“fileName.txt”, “w”); ---------- Some file Operations ------- fclose(filePointer)示例 1:打开文件、写入文件和关闭文件的程序
// C program to Open a File,
// Write in it, And Close the File
# include <stdio.h>
# include <string.h>
int main( )
{
// Declare the file pointer
FILE *filePointer ;
// Get the data to be written in file
char dataToBeWritten[50]
= "GeeksforGeeks-A Computer Science Portal for Geeks";
// Open the existing file GfgTest.c using fopen()
// in write mode using "w" attribute
filePointer = fopen("GfgTest.c", "w") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "GfgTest.c file failed to open." ) ;
}
else
{
printf("The file is now opened.n") ;
// Write the dataToBeWritten into the file
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("n", filePointer) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file GfgTest.cn");
printf("The file is now closed.") ;
}
return 0;
}
// C program to Open a File,
// Read from it, And Close the File
# include <stdio.h>
# include <string.h>
int main( )
{
// Declare the file pointer
FILE *filePointer ;
// Declare the variable for the data to be read from file
char dataToBeRead[50];
// Open the existing file GfgTest.c using fopen()
// in read mode using "r" attribute
filePointer = fopen("GfgTest.c", "r") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "GfgTest.c file failed to open." ) ;
}
else
{
printf("The file is now opened.n") ;
// Read the dataToBeRead from the file
// using fgets() method
while( fgets ( dataToBeRead, 50, filePointer ) != NULL )
{
// Print the dataToBeRead
printf( "%s" , dataToBeRead ) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully read from file GfgTest.cn");
printf("The file is now closed.") ;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\program.bin","wb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n) //Program Code Submitted by Susobhan Akhuli
{
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\program.bin","rb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n) //Program Code Submitted by Susobhan Akhuli
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0;
}