首页 > 其他分享 >C语言文件操作(续写)

C语言文件操作(续写)

时间:2024-09-30 19:21:11浏览次数:3  
标签:fp 文件 pFile int ptf C语言 fopen 续写

文章目录

C语言文件操作(续写)

文件的随机读写

fseek

根据文件指针的位置和偏移量来定位文件指针。

 #include <stdio.h>
 int main ()
 {
  FILE * pFile;
  pFile = fopen ( "example.txt" , "wb" );
  fputs ( "This is an apple." , pFile );
  fseek ( pFile , 9 , SEEK_SET );
  fputs ( " sam" , pFile );
  fclose ( pFile );
  return 0;
 }
#include<stdio.h>
int main() {
	FILE* ptf = fopen("test.txt", "w");
	if (ptf == NULL) {
		perror("fopen");
	}
	fputs("zhaoyong", ptf);
	fclose(ptf);
	char ch[20] = { 0 };
	ptf = fopen("test.txt", "r");
	if (ptf == NULL) {
		perror("fopen");
	}
	fgets(ch, 6, ptf);
	printf("%s\n", ch);
	//当前指针所指向的位置是y,向后偏移了两个字节
	fseek(ptf, -2, SEEK_CUR);
	fgets(ch, 6, ptf);
	printf("%s\n", ch);
	//找到文件最后的位置
	fseek(ptf, -2,SEEK_END);
	fgets(ch, 6, ptf);
	printf("%s", ch);
}

ftell

返回文件指针相对于起始位置的偏移量

long int ftell ( FILE * stream );
#include <stdio.h>
int main()
{
	FILE* pFile;
	long size;
	pFile = fopen("test.txt", "rb");
	if (pFile == NULL) perror("Error opening file");
	else
	{
		fseek(pFile, 0, SEEK_END);
		size = ftell(pFile);
		
		fclose(pFile);
		printf("%d", size);
	}
	return 0;
}

rewind

让文件指针的位置回到文件的起始位置

void rewind ( FILE * stream );

例子:

#include<stdio.h>
int main() {
	int n;
	FILE* pf;
	pf = fopen("test.txt", "wb");
	if (pf == NULL) {
		perror("fopen");
	}
	char arr[] = "abcdef";
	fwrite(arr, sizeof(char), strlen(arr), pf);
	rewind(pf);
	fclose(pf);
	
	pf = fopen("test.txt", "rb");
	//如果要存放'\0'的话,要存放7个字符
	char arr1[7] = { 0 };

	fread(arr1, sizeof(char), 7, pf);
	fclose(pf);
	printf("%s\n", arr1);
	printf("%d", strlen(arr1));
	return 0;
}

文件结束判定

被错误使用的 feof

牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。

而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。

  1. 文本文件读取是否结束,判断返回值是否为EOF (fgetc),或者NULL(fgets)

    例如:

    • fgetc判断是否为EOF
    • fgets判断返回值是否为NULL.
  2. . 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。

    例如:

    • fread判断返回值是否小于实际要读的个数。
    • 正确的使用:
    • 文本文件的例子:
    #include <stdio.h>
     #include <stdlib.h>
     int main(void)
     {
     int c; // 注意:int,非char,要求处理EOF
     FILE* fp = fopen("test.txt", "r");
     if(!fp) {
     perror("File opening failed");
     return EXIT_FAILURE;
        }
     //fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
     while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
        { 
    putchar(c);
        }
     //判断是什么原因结束的
    if (ferror(fp))
     puts("I/O error when reading");
     else if (feof(fp))
     puts("End of file reached successfully");
     }
     fclose(fp);
    

二进制文件的例子:

#include <stdio.h>
enum { SIZE = 5 };
 int main(void)
 {
 double a[SIZE] = {1.0,2.0,3.0,4.0,5.0};
 double b = 0.0;
 size_t ret_code = 0;
 FILE *fp = fopen("test.bin", "wb"); // 必须用二进制模式
fwrite(a, sizeof(*a), SIZE, fp); // 写 double 的数组
fclose(fp);
 }
 fp = fopen("test.bin","rb");
 // 读 double 的数组
while((ret_code = fread(&b, sizeof(double), 1, fp))>=1)
    {
 printf("%lf\n",b);
    }
 if (feof(fp))
 printf("Error reading test.bin: unexpected end of file\n");
 else if (ferror(fp)) {
 perror("Error reading test.bin");
    }
 fclose(fp);
 fp = NULL;
  • 检查是否设置了与 stream 关联的 end-of-File 指示器,如果设置了,则返回一个不为零的值。
  • 此指示器通常由流上的先前操作设置,该操作尝试在文件末尾或文件末尾之后读取。
  • 请注意,stream 的内部位置指示器可能指向下一个操作的文件结尾,但是,在操作尝试在该点读取之前,仍可能无法设置文件结尾指示器。

简单来说,就是通过文件结尾的判断来判断一个文件有没有到结尾

举个例子

#include<stdio.h>
int main() {
	char arr[] = "abcdefg";
	char buffer[10] = { 0 };
	FILE* ptf;
	ptf = fopen("test.txt", "w");
	if (ptf == NULL) {
		perror("fopen");
	}
	sprintf(buffer, "%s", arr);
	fputs(buffer, ptf);
	
	//用ascll码值的方式获取
	fclose(ptf);
	
	ptf = fopen("test.txt", "r");
	fseek(ptf, 0, SEEK_END);
	int c = fgetc(ptf);
	if (c == EOF) {
		if (feof(ptf)) {
			printf("end the file");
		}
		else if(ferror(ptf)) {
			puts("I/O error when reading");
		}
	}
	else {
		putchar(c);
	}
}
st.txt", "r");
	fseek(ptf, 0, SEEK_END);
	int c = fgetc(ptf);
	if (c == EOF) {
		if (feof(ptf)) {
			printf("end the file");
		}
		else if(ferror(ptf)) {
			puts("I/O error when reading");
		}
	}
	else {
		putchar(c);
	}
}

标签:fp,文件,pFile,int,ptf,C语言,fopen,续写
From: https://blog.csdn.net/2301_81134141/article/details/142619667

相关文章