吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C 中文件处理的基础知识

C 中文件处理的基础知识

来源:网络 | 更新时间:2022-01-24 21:17:05
到目前为止,使用 C 程序的操作是在没有存储在任何地方的提示/终端上完成的。但是在软件行业,大多数程序都是为了存储从程序中获取的信息而编写的。一种这样的方法是将获取的信息存储在文件中。可以对文件执行的不同操作包括: 
  1. 创建一个新文件(fopen,属性为“a”或“a+”或“w”或“w++”)
  2. 打开现有文件 ( fopen )
  3. 从文件中读取(fscanf 或 fgets
  4. 写入文件(fprintf 或 fputs
  5. 移动到文件中的特定位置(fseek、rewind
  6. 关闭文件 ( fclose )
括号中的文字表示用于执行这些操作的函数。 文件操作中的功能: 打开或创建文件 为了打开文件,fopen 函数与所需的访问模式一起使用。下面介绍一些常用的文件访问模式。 C中的文件打开模式:
  • “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+” -以二进制模式打开以供读取和附加。如果文件不存在,则会创建它。
如上所述,如果要对二进制文件执行操作,则必须在最后附加“b”。例如,你必须使用“wb”而不是“w”,而不是“a+”,你必须使用“a+b”。为了对文件执行操作,使用了一个称为文件指针的特殊指针,它被声明为 
FILE *filePointer; 

So, the file can be opened as 

filePointer = fopen(“fileName.txt”, “w”)
第二个参数可以更改为包含上表中列出的所有属性。
  • 文件中读取--:
          文件读取操作可以使用函数 fscanf 或 fgets 来执行。这两个函数执行的操作与 scanf 和 get 的操作相同,但带有一个附加参数,即文件指针。因此,这取决于您是要逐行读取文件还是逐字符读取文件。           读取文件的代码片段如下: 
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; 
}
 示例 2:打开文件、读取文件并关闭文件的程序
// 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; 
}
      示例 3:使用 fwrite() 写入二进制文件的程序
#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;
}
    示例 4:使用 fread() 从二进制文件中读取的程序
#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;
}
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com