代码及解释如下
可新建一个c++类来测试代码理解更加深刻
之后加载shader文件会用到c文件读写
//
// CFile.hpp
// algorithm_cpp
//
// Created by Mac on 2019/11/25.
// Copyright © 2019 Mac. All rights reserved.
//
#ifndef CFile_hpp
#define CFile_hpp
#include
#include
#include
using namespace std;
class CFile {
public:
char *fileName;
public:
CFile(){
this->fileName = "/Users/mac/Desktop/diaozhatian.sh";
}
/*
r 打开只读文件,文件必须存在,否则打开失败
r+ 打开可读写文件,文件必须存在,否则打开失败
w 打开只写文件,文件不存在会创建文件,文件存在,覆盖原内容
w+ 打开可读写文件,文件不存在会创建,文件存在会覆盖原文件内容
a 附件形式打开只写文件,文件不存在会创建文件,文件存在,文件指针移到末尾,每次都会往后面拼内容
a+ 附加形式打开可读写文件,文件不存在会创建文件,文件存在,文件指针移到末尾,每次都会往后面拼内容
b 加在上面这些模式之后,表示以二进制打开
文件只要打开,最后都要关闭,否则内存泄漏
*/
void openAndClose(){
FILE *pFile;
pFile = fopen(this->fileName, "a+b");
if (pFile != NULL) {
cout << "打开ok" << endl;
fputs("来点daigande", pFile);
fclose(pFile);
} else {
cout << "文件不存在" << endl;
}
}
/*
读取字符
c = fgetc(FILE *);
每次读取一个字符,返回这个字符的 asc11 码
*/
void get_file_char_component(){
FILE *pfile;
int c = 0;
pfile = fopen(this->fileName, "r");
if (pfile == NULL) {
cout << "read error" << endl;
fclose(pfile);
return;
}
while (c != EOF) {
//返回的是字符的 奥斯卡 编码
c = fgetc(pfile);
printf("%c",c);
cout << "=" << c <fileName, "r");
if (pfile == NULL) {
cout << "read error" << endl;
fclose(pfile);
return;
}
while (!feof(pfile)) {
memset(mystring, 0, sizeof(mystring));
fgets(mystring, sizeof(mystring), pfile);
cout << mystring << endl;
}
fclose(pfile);
}
/*
fread(void *__ptr, size_t __size, size_t __nitems, FILE *__stream)
参数
void *__ptr 保存读取内容的buf
size_t __size 每次读取的内容是多大
size_t __nitems 一共读取多少次
FILE *__stream 文件指针
*/
void read_some_size(){
FILE *pfile;
pfile = fopen(this->fileName, "r+");
if (pfile == NULL) {
cout << "read error" << endl;
fclose(pfile);
return;
}
char buf[100] = {0};
while (!feof(pfile)) {
memset(buf, 0, sizeof(buf));
fread(buf, sizeof(char), sizeof(buf), pfile);
cout << buf << endl;
}
fclose(pfile);
}
/*
每次写入一个字符
fputc(int, FILE *)
参数
int 写入文字的 asc11 码
FILE *文件指针
*/
void put_char_tofile(){
FILE *pfile;
pfile = fopen(this->fileName, "a+");
if (pfile == NULL) {
fclose(pfile);
return;
}
for (int i = 0; i < 9; i++) {
fputc(99, pfile);
}
fclose(pfile);
}
/*
成功放回证书,失败返回EOF
fputs(const char *, FILE *)
参数
const char * 写入的字符串
FILE * 文件指针
*/
void put_string_tofile(){
FILE *pfile;
pfile = fopen(this->fileName, "a+");
if (pfile == NULL) {
fclose(pfile);
return;
}
char *lihai = "数控刀具;发开始减肥贷款案件发";
int ret = fputs(lihai, pfile);
if (ret == EOF) {
cout << "写入失败" << endl;
}
fclose(pfile);
}
/*
fwrite(const void *__ptr, size_t __size, size_t __nitems, FILE *__stream)
const void *__ptr 要写入的buf
size_t __size 每次写入多大
size_t __nitems 写多少次
FILE * 文件指针
*/
void write_somesize(){
FILE *src_file;
src_file = fopen(this->fileName, "r+");
if (src_file == NULL) {
fclose(src_file);
return;
}
FILE *dst_file;
dst_file = fopen("/Users/mac/Desktop/niubi3.sh", "w+");
if (dst_file == NULL) {
fclose(dst_file);
return;
}
char buf[100] = {0};
while (!feof(src_file)) {
memset(buf, 0, sizeof(buf));
size_t len = fread(buf, sizeof(char), sizeof(buf), src_file);
fwrite(buf, sizeof(char), len, dst_file);
}
fclose(src_file);
fclose(dst_file);
}
/*
fseek(FILE *, long offset, int origin)
FILE * 文件指针
long offset 相对于 origin 偏移的位置
int origin ,文件指针的参考位置,
#define SEEK_CUR 1 // 当前位置
#define SEEK_END 2 // 末尾
#define SEEK_SET 0 // 开头
// 下面的例子,读取某个文件的内容的大小
*/
void seek_tosome_where(){
FILE *src_file;
src_file = fopen(this->fileName, "r+");
if (src_file == NULL) {
fclose(src_file);
return;
}
fseek(src_file, 10, 2);
size_t size = ftell(src_file);
cout << size << endl;
fclose(src_file);
}
/*
fflush(FILE *);
我们在写入文件时,并不是实时写入的,在调用了fclose时候才真的写入
但是为了断电等意外,可以使用这个函数来试试写入
*/
void write_fflush(){
FILE *src_file;
src_file = fopen(this->fileName, "r+");
if (src_file == NULL) {
fclose(src_file);
return;
}
FILE *dst_file;
dst_file = fopen("/Users/mac/Desktop/niubi3.sh", "a+");
if (dst_file == NULL) {
fclose(dst_file);
return;
}
char buf[100] = {0};
size_t len = 0;
while (!feof(src_file)) {
memset(buf, 0, sizeof(buf));
len = fread(buf, sizeof(char), sizeof(buf), src_file);
}
fwrite(buf, len, 1, dst_file);
int ret = fflush(dst_file);
if (ret == 0) {
cout << "写入ok" << endl;
}
fclose(src_file);
fclose(dst_file);
}
/*
int rename ( const char * oldname, const char * newname ); 重命名文件
*/
void renamefile(){
int ret = rename(this->fileName,"/Users/mac/Desktop/diaozhatian.sh");
if (ret == 0) {
cout << "success" << endl;
}
}
/*
int remove ( const char * filename ); 删除文件
*/
void removefile(){
int ret = remove(this->fileName);
if (ret == 0) {
cout << "success" << endl;
}
}
//rewind(FILE*); 将指针重新定位到起始位置
void rewind_file(){
char str[] = "This is runoob.com";
FILE *fp;
int ch;
/* 首先让我们在文件中写入一些内容 */
fp = fopen( "/Users/mac/Desktop/file.txt" , "w+" );
if (fp == NULL) {
return;
}
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
fp = fopen( "/Users/mac/Desktop/file.txt" , "r" );
while(1)
{
ch = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", ch);
}
rewind(fp);
printf("\n");
while(1)
{
ch = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", ch);
}
printf("\n");
fclose(fp);
}
};
#endif /* CFile_hpp */